2009-SEC830-mkusers.py

From CDOT Wiki
Revision as of 11:25, 23 January 2009 by Selmys (talk | contribs) (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...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#!/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@lotuspond.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)