#!/usr/bin/python import popen2, sys, string, optparse CR = '\r' LF = '\n' CRLF = CR+LF class CustomOptionParser(optparse.OptionParser): """ OptionParser is main class to parse options. """ def __init__(self): optparse.OptionParser.__init__(self, usage="%prog --help or %prog [options]", version="%prog 0.1", conflict_handler="resolve") self.add_option("--userprefix",action="store", type="string", dest="userprefix", default="", help="specify the user prefix ex: USCMS NOTE: must be specified if no passwd file is given") self.add_option("--number",action="store", type="int", dest="number", default=0, help="specify the number of accounts to create (defaults to 1)") self.add_option("--begin",action="store", type="int", dest="begin", default=0, help="specify the beginning number (defaults to 0)") self.add_option("--homeprefix", action="store", type="string", dest="homeprefix", default="/home", help="specify the home directory prefix ex: /storage/home (defaults to /home)") self.add_option("--gid", action="store", type="string", dest="gid", default="", help="specify the group id that the user belongs to") self.add_option("--shell", action="store", type="string", dest="shell", default="", help="specify the shell that is associated with the user") self.add_option("--userfile", action="store", type="string", dest="userfile", default="", help="specify a passwd file. NOTE: this will overide the previous options") self.add_option("--groupfile", action="store", type="string", dest="groupfile", default="", help="specify a group file. NOTE: this will overide the previous options") def getOpt(self): """ Returns parse list of options @type self: class object @param self: none @rtypei : none @return : list of options. """ return self.parse_args() class CreateUsers: def __init__(self, opts): self.userprefix = opts.userprefix self.number = opts.number self.begin = opts.begin self.homeprefix = opts.homeprefix self.gid = opts.gid self.shell = opts.shell self.passwdfile = opts.userfile self.groupfile = opts.groupfile def createusers(self): if len(self.passwdfile) > 0: # if a group file is specified, create the groups first if len(self.groupfile) > 0: self.createGroups(self.groupfile) # parse the passwd file and create users from the info in there self.createFromFile(self.passwdfile) else: # create users with given defaults self.createFromDefaults(self.userprefix, self.begin, self.number, self.gid, self.homeprefix, self.shell) def createGroups(self, groupfile): group = self.getFileContents(groupfile) for line in group: # line format: "Name:Password:UserID:PrincipleGroup:Gecos:HomeDirectory:Shell" groupItem = line.split(":") groupname = groupItem[0] Password = groupItem[1] # ignored... password won't be here anyways groupID = groupItem[2] # We will ignore the userlist since the user add will add them to the group command = "groupadd -g " + groupID + " " + groupname print "Adding Group/ID: " + groupname + "/" + groupID print "Command: " + command self.runCommand(command) def runCommand(self, command): pout,pin,perr = popen2.popen3(command) pout = pout.readlines() p = perr.readlines() if (p): for l in p: print l return pout def getFileContents(self, filename): f = open(filename) rawContent = f.read() extractedContent = rawContent.split(LF) # Get rid of any trailing blank lines if (len(extractedContent[len(extractedContent)-1]) == 0): extractedContent.pop() return extractedContent def createFromFile(self, passwdfile): passwd = self.getFileContents(passwdfile) for line in passwd: # line format: "Name:Password:UserID:PrincipleGroup:Gecos:HomeDirectory:Shell" passItem = line.split(":") username = passItem[0] Password = passItem[1] # ignored... password won't be here anyways UserID = passItem[2] # PrincipleGroup = passItem[3] # Gecos = passItem[4] # ignored HomeDirectory = passItem[5] # Shell = passItem[6] # command = "useradd -m -u " + UserID + " -g " + PrincipleGroup + " -d " + HomeDirectory + " -s " + Shell + " " + username print "Adding User/ID: " + username + "/" + UserID print "Group: " + PrincipleGroup print "Home Directory: " + HomeDirectory print "Shell: " + Shell print "Command: " + command self.runCommand(command) def createFromDefaults(self, userprefix, begin, number, gid, homeprefix, shell): options = "" if len(str(gid)) > 0: options = options + " -g " + str(gid) if len(shell) > 0: options = options + " -s " + shell x = begin r = range(begin, number+begin) for x in r: if len(str(x)) == 1: usernumber = "0" + str(x) else: usernumber = str(x) username = userprefix + usernumber homedir = homeprefix + "/" + username command = "useradd -m " + options + " -d " + homedir + " " + username print "Adding User: " + username + LF + "Home Directory: " + homedir + LF + "Command: " + comman if __name__ == "__main__": opts = {} args = [] optManager = CustomOptionParser() (opts,args) = optManager.getOpt() a = CreateUsers(opts) a.createusers()