Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import csv
- import argparse
- import crypt
- import sys
- import random
- import os
- import grp
- import pwd
- import io
- import shutil
- import re
- saltchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- arg = 'randomstring'
- def copy_file(src, dest):
- try:
- shutil.copy(src, dest)
- # eg. src and dest are the same file
- except shutil.Error as e:
- print('Error: %s' % e)
- # eg. source or destination doesn't exist
- except IOError as e:
- print('Error: %s' % e.strerror)
- def salt():
- return random.choice(saltchars) + random.choice(saltchars)
- def hash(plain):
- return crypt.crypt(arg, salt())
- def main():
- with io.FileIO("handy_links.html", "w") as file:
- file.write("www.google.com\n")
- file.write("www.yahoo.com\n")
- file.write("www.bing.com\n")
- directory = "Handy Links"
- invalid_chars = re.compile(r"^[^<>/{}[\]~`]*$");
- parser = argparse.ArgumentParser(description='This program will create user directories plus more')
- parser.add_argument("csv", nargs='+', help='Specify a CSV file with users', type=str)
- args = parser.parse_args()
- #print args.csv[0]
- file = args.csv[0]
- users = open(file, 'rb')
- csv_f = csv.reader(users)
- #Define some variables
- #The program will assume there are no middle names or initials
- for row in csv_f:
- random.seed()
- full_name = row[0].split()
- #print full_name
- if not full_name:
- continue
- first_initial = full_name[0][0]
- first_name = full_name[0]
- last_name = full_name[1]
- username = (first_initial + last_name).lower()
- if not username:
- continue
- elif not last_name:
- continue
- elif not first_name:
- continue
- #Generate a password - alphanumeric charachers
- #This method is really for checking if the existing passwords are correct, however, generating them #would work as well
- password = hash(arg)
- try:
- pwd.getpwnam(username)
- except KeyError:
- #only create a user if it does not exist
- os.system("useradd -p "+password+" "+username)
- #print username
- #groups
- #check if group are not empty
- if row [1:]:
- for group in row[1:]:
- #print invalid_chars.match(group)
- if invalid_chars.match(group):
- #print ("group name " + group.strip() + " contains all good chars")
- try:
- grp.getgrnam(group.strip())
- except KeyError:
- #create a group if does not exist. why not :)
- os.system("groupadd "+group.strip())
- #add user to the group
- os.system("usermod -a -G "+group.strip()+" "+username)
- else:
- pass
- #print ("group name " + group.strip() + " DOES NOT contain all good char")
- #create a directory for handy links
- if not os.path.exists("/home/"+username+"/"+directory):
- os.makedirs("/home/"+username+"/"+directory)
- copy_file("handy_links.html", "/home/"+username+"/"+directory+"/handy_links.html")
- #script output
- print (first_name + " " + last_name + ", " + username + "/" + password)
- html_file_name = username + ".html"
- #html file created in the same directory where the script runs
- web_user_file = open(html_file_name, 'w+')
- web_user_file.write(first_name + " " + last_name + ", " + username + "/" + password)
- try:
- main()
- except KeyboardInterrupt:
- print "Bye"
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement