Generate unique Linux user names and random passwords
Top  Previous  Next


Task

·Create short user names (uid/cn) from first two letters in first and last name  
·Compare user names with existing Linux user names, add (or increase) number if name already exists  
·Generate random passwords  
·Create accounts  

Script


[Settings_Begin]
BatchSettings.Delimiter=TAB
BatchSettings.MarkerCol=5

// LDAP server, SSL must be enabled on port 636, a SSL certificate must be installed locally
SET LDServer=ldap4.my-domain.com
SET LDAdminAccount=%LDDomainPath%/cn=ldapman
SET LDAdminPW=password

// SSH server same as LDAP server
SET SSHserver=%LDServer%
SET SSHAdminAccount=root
SET SSHAdminPW=password

SET LDDomainPath=dc=com/dc=my-domain
SET LDOU=People
SET LDOUPath=%LDDomainPath%/ou=%LDOU%
SET LDGroup=MyGroup
SET LDGroupPath=%LDOUPath%/cn=%LDGroup%

SET DataMemFile=c:\L_data.txt
SET LinuxAccFile=c:\L_curr.txt

SET FirstName=%col1%
SET LastName=%col2%
SET FullName=%col1% %col2%
SET AccountNameColNo=3
SET AccountName=Col(%AccountNameColNo%)
SET Password=%col4%
SET LDAccountPath=%LDOUPath%/uid=%AccountName%

SET HomeRoot=/home
SET HomeDirectory=%HomeRoot%/%AccountName%

SET MailSuffix=mydomain.com

Generator.Password.DestCol=4
Generator.Password.Random.Len=8
Generator.Password.Random.Chars=0123456789abcdef
[Settings_End]

[Init_Batch_Begin]
// Log on to LDAP server (required in each section with Linux command)   
Lin.DS.Logon %LDServer%, %LDAdminAccount%, %LDAdminPW%   
// Save current data section   
Data.Save %DataMemFile%   
// Create file with all existing Linux-accounts   
Data.Get.LD.Find %LDDomainPath%, (objectClass=posixAccount), yes, uid§1§   
Data.Save %LinuxAccFile%   
// Reload current data section   
Data.Load %DataMemFile%   
// Create user name from first two letters in first name and last name   
Data.Loop   
 Data.Write %AccountNameColNo%,lowercase(Copy(%FirstName%,1,2)Copy(%LastName%,1,2))   
Data.Endloop   
// Make user names unique - compare [data] with existing Linux names, add number if same   
Data.Column.DupeCheck %AccountNameColNo%, TextFile, %LinuxAccFile%, AddNumber   
// Generate random passwords   
Data.Passwords.Generate Random   
[Init_Batch_End]

[Sub_InitGid_Begin]
// This $c subroutine makes sure that gidNumber is initialized even if script is interrupted in the middle and restarted   
//  gidNumber used by Lin.DS.Op & Chown   
If %gidNumber%= Then   
 SET gidNumber=LDValue (%LDGroupPath%, gidNumber)   
 // MessageBox %gidNumber%   
EndIf   
[Sub_InitGid_End]

[Sub_InitUid_Begin]
// This $c subroutine makes sure that uidNumber is initialized even if script is interrupted in the middle and restarted   
//  uidNumber used by Chown command   
SET uidNumber=LDValue (%LDAccountPath%, uidNumber)   
//MessageBox %uidNumber%   
[Sub_InitUid_End]

[Batch_Begin]   
// Log on to LDAP and SSH   
Lin.DS.Logon %LDServer%, %LDAdminAccount%, %LDAdminPW%   
Lin.SSH.Logon %SSHserver%, %SSHAdminAccount%, %SSHAdminPW%   
Data.Loop   
 // Create account   
 Lin.DS.Op.Init Create, %LDAccountPath%   
 Lin.DS.Op.SetProperty objectClass, inetOrgPerson§posixAccount§   
 // inetOrgPerson attrs   
 Lin.DS.Op.SetProperty givenName, %FirstName%   
 Lin.DS.Op.SetProperty sn, %LastName%   
 Lin.DS.Op.SetProperty mail, %AccountName%@%MailSuffix%   
 // posixAccount attrs   
 Lin.DS.Op.SetProperty uid, %AccountName%   
 Lin.DS.Op.SetProperty cn, %AccountName%   
 Lin.DS.Op.SetProperty userPassword, %Password%   
 $c InitGid Lin.DS.Op.SetProperty gidNumber, %gidNumber%   
 Lin.DS.Op.SetProperty uidNumber, Auto   
 Lin.DS.Op.SetProperty homeDirectory, %HomeDirectory%   
 Lin.DS.Op.SetProperty loginShell, /bin/bash      
 Lin.DS.Op.SetProperty gecos, %FullName%   
 Lin.DS.Op.Commit   
 // Create home directory and set permissions   
 Lin.SSH.Command 20, mkdir %HomeDirectory%   
 $c InitGid $c InitUid Lin.SSH.Command 20, chown -R %uidNumber%:%gidNumber% %HomeDirectory%   
 // (-R flag = recursively descends the specified directories)   
 Lin.SSH.Command 20, chmod 700 %HomeDirectory%   
Data.EndLoop   
[Batch_End]

[Undo_Batch_Begin]
// Log on to LDAP and SSH   
Lin.DS.Logon %LDServer%, %LDAdminAccount%, %LDAdminPW%   
Lin.SSH.Logon %SSHserver%, %SSHAdminAccount%, %SSHAdminPW%   
// Delete account and home directory   
Data.Loop   
 Lin.DS.Object.Delete %LDAccountPath%   
 Lin.SSH.Command 20, rmdir %HomeDirectory%   
Data.EndLoop   
[Undo_Batch_End]

[Data_Begin]
Steve   Smith         
Ellen   Jones         
John   Simon         
Lisa   Karush         
Anna   Hedin         
Roger   Palm         
[Data_End]

Result

Data section after first run of above script:

Steve   Smith   stsm   a0197997   
Ellen   Jones   eljo   4fe2528b   
John   Simon   josi2   5e0455ac   
Lisa   Karush   lika   c9383b35   
Anna   Hedin   anhe   1cb6a42c   
Roger   Palm   ropa   ed803ca9      

There was already an account called "josi" in the directory -> "josi" became "josi2".

[Data] section after second run of above script:

Steve   Smith   stsm2   1afadf97   
Ellen   Jones   eljo2   3c766ab0   
John   Simon   josi3   16e867ff   
Lisa   Karush   lika2   96cf4c8e   
Anna   Hedin   anhe2   e32b9ee6   
Roger   Palm   ropa2   98ca8c30   

Trailing number for all accounts increased by one.