#!/usr/bin/python import csv import sys import os import re import subprocess import pwd def createAccount(username, lastname, firstname): return subprocess.call(['useradd', '-m', username, '-c', '%s, %s' % (lastname, firstname), '-U', '-s', '/bin/bash']) if os.geteuid() != 0: print "You must run this program as root" exit(1) if len(sys.argv) != 2: print "Usage: %s " % s.argv[0]) exit(1) input = csv.reader(open(sys.argv[1])) for row in input: if len(row) != 3: print "Line %d: Not enough data" % ut.line_num continue (username, lastname, firstname) = row if not re.match('^[a-z][a-z0-9]+$', username): print "Line %d: Login name must start with a letter, and contain only letters and numbers" % input.line_num continue try: pwd.getpwnam(username) print "Line %d: Account '%s' already exists" % (input.line_num, username) continue; except KeyError: pass createAccount(username, lastname, firstname)