Difference between revisions of "2009-SEC830-mkusers.py"
(New page: <pre> #!/usr/bin/python # mkusers - created by J. Selmys - January 2009 import sys, re, os, random, smtplib, string if len(sys.argv) < 2: print 'Usage: mkusers [user...]' s...) |
|||
Line 32: | Line 32: | ||
if flag == 1: | if flag == 1: | ||
FROM = 'john.selmys@senecac.on.ca' | FROM = 'john.selmys@senecac.on.ca' | ||
− | TO = 'selmys@ | + | TO = 'selmys@xxxxxxxxx.ca' |
subject = 'Linux Account' | subject = 'Linux Account' | ||
body ='Hello\n' +\ | body ='Hello\n' +\ |
Latest revision as of 06:35, 24 January 2009
#!/usr/bin/python # mkusers - created by J. Selmys - January 2009 import sys, re, os, random, smtplib, string if len(sys.argv) < 2: print 'Usage: mkusers [user...]' sys.exit(1) for i in range(1, len(sys.argv)): # check password file to see if user already exists f = open('/etc/passwd','r') p = re.compile(sys.argv[i]+':') ok = 1 for line in f: if p.match(line) != None: ok = 0 f.close() if ok == 1: r = os.system('/usr/sbin/useradd -m ' + sys.argv[i]) if r == 0: print 'User ' + sys.argv[i] + ' created....\n' # now make a random password pw = '' for j in range(8): pw = pw + random.choice('abcdefghijklmnopqrstuvwxyz') # now set the user's password os.system('echo ' + sys.argv[i] + ':' + pw + '| chpasswd') pwf = open('/root/passwords','a') pwf.write(sys.argv[i] + ':' + pw + '\n') pwf.close() # email password to the user if flag is set to one flag = 1 if flag == 1: FROM = 'john.selmys@senecac.on.ca' TO = 'selmys@xxxxxxxxx.ca' subject = 'Linux Account' body ='Hello\n' +\ ' An account on Linux was created for you.\n' +\ ' Your username is ' + sys.argv[i] + ' and your password is ' + pw + '\n' +\ ' Use it wisely.\n' +\ 'John\n' BODY = string.join(( "From: %s" % FROM, "To: %s" % TO, "Subject: %s" % subject, "", body ), "\r\n") server = smtplib.SMTP('lotuspond.ca') server.sendmail(FROM, [TO], BODY) server.quit() else: print 'Account ' + sys.argv[i] + ' created but no email sent.' else: print 'Error creating account!' sys.exit(2) else: print 'User ' + sys.argv[i] + ' exists and will not ne added.\n' sys.exit(0)