Adduser script for webmin

From LQWiki
Jump to navigation Jump to search

Webmin has the nifty module to add a plethora of users via a text file with the 13 fields... I'm modifying my (Huck's) python script to merely take a textfile with each user's full name, in the format 'firstname lastname', on new lines, and output them as a username in the format 1st 4 letters of the lastname + 1st 2 letters of the first name. Therefore John Smith = smitjo. Then you can just get the text file from your registrar and choose your 'username format' and create the file to upload to Webmin.

The code so far:

#start of script

#wanted menu to be able to use '1 letter hotkeys' but I'm not THAT advanced yet! a !=4 || !=x might work..but haven't had time to fiddle
def print_menu():
  print '1. Press 1 to (i)mport the namelist.txt file'
  print '2. Press 2 to (g)enerate usernames'
  print '3. Press 3 to (p)rint new usernames.txt file'
  print '4. Press 4 to E(x)it the Program'
numbers = {}
menu_choice = 0
while menu_choice != 4:
  menu_choice = input("Type in a number (1-4):")
  if menu_choice == 1:
     #Open the file "usernames.txt" a textfile with your user's FIRSTNAME LASTNAME on newlines
     in_file = open("c:\\usernames.txt","r")
     #variable "textfile" gets filled with info from usernames.txt
     textfile = in_file.readlines()
     #Close the file "usernames.txt"
      elif menu_choice == 2:         #The Loopy Loopy
     out_file = open("c:\\usernameswebmin.txt","w")
     for namenum,mystring in enumerate(textfile):
        print namenum
        #Manipulate the namelist into pieces we can handle
        newlist = mystring.split()
        fname = newlist[0]
        lname = newlist[1]
        fname = fname[:2]
        lname = lname[:4]
        #Concatenate and Write to File
        #create:USERNAME:PASSWD::::/home/username:/bin/bash:::::  format for webmin module
        print >> out_file, "create:"+lname+fname+":"+lname+fname+"::::/home/"+lname+fname+":/bin/bash:::::"
        print lname+fname+" has been written to file."
     print namenum
     #We're all done so lets close the file!
     namenum = 0
  elif menu_choice == 3:
     #Gimme some output baby! Lets see what we've added
     in_file = open("c:\\usernameswebmin.txt","r")
     funkytext = in_file.readlines()
     print funkytext
  elif menu_choice == 4:
print "Goodbye!"

#end of script