Re: Adding Users

Baishampayan Ghose <[email protected]>
Newsgroups gmane.org.region.india.fsug-kochi
Message-ID <[email protected]>
Hello Biju,
>  how can i add a bulk of users using script??
>  for eg:-user1 to some user100(suppose both user name and passwd are 
> same)
I have hacked up a small Python script for you. I am attaching it with
this mail.

USAGE: python useradd.py <username_prefix> <number_of_accounts>

Others can view the file here - http://pastebin.com/330149
Hope that helps,
Feel free to ask in case of any issues.
Regards,
-bg-

-- 
Baishampayan Ghose
b.ghose at gmail.com

_______________________________________________
FSUG-Kochi-Discuss mailing list
FSUG-Kochi-Discuss-8H24Gfgyrqprzt3WPRo2bV6hYfS7NtTn@public.gmane.org
http://puggy.symonds.net/cgi-bin/mailman/listinfo/fsug-kochi-discuss
useradd.py (text/x-python, 1.6 KB)
#!/usr/bin/python

# useradd.py -- A simple Python script to create a bunch of users at a time
# Copyright (c) 2005 Baishampayan Ghose <[email protected]>
# Licensed under GNU GPL v 2
# USAGE: useradd.py <username_prefix> <number_of_accounts>

import sys, crypt, os

__passwd__ = 'password' # The default password
__salt__ = '2a08d4d1e35cbb74c169e7e6a51fad44'

def validate():
    '''This function validates input and checks for root privileges'''
    if (len(sys.argv) < 3):
        sys.exit("USAGE: useradd.py prefix number")
    else:
        print "Prefix: ", sys.argv[1]
        print "Accounts: ", sys.argv[2]
        ch = raw_input('Continue (y/n)? ')
        if (ch.upper() != 'Y'):
            sys.exit('Bye')
        else:
            uid = os.getuid()
            if (uid != 0):
                sys.exit('ERROR: You need to be root for that')
            else:
                pass

def main():
    '''This is the main function and does the actual job'''
    validate()
    prefix = sys.argv[1]
    number = sys.argv[2]
    passwd = crypt.crypt(__passwd__, __salt__)

    for i in xrange(1, int(number) + 1):
        uname = prefix + str(i).zfill(2)
        homedir = '/home/' + uname # The home dir
        command = 'useradd -p ' + passwd + ' -d ' + homedir + ' ' + uname
        if (os.system(command) != 0):
            sys.exit('Oops! Some error ocurred')
        print "Created user: ", uname

# If the script is run directly call main() else quit
        
if (__name__ == '__main__'):
    main()
else:
    sys.exit('useradd.py: This script should not be used as a module!')
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.