Continuing with where I left off, today I finally managed to write a script that will read the names of users from a text file & add them to the server. The whole process is automated & requires absolutely no human intervention. 😉 If you are new to expect, please read this post to get a clear understanding of expect command.
Below is how to add users to the server using a while loop & expect 🙂
Execute this script as :
[shashank@server ~]$ sudo sh ~shashank/scripts/useradd.sh
Here is the script.
while read user; do { /usr/bin/expect << EOF spawn useradd $user puts "$user added" expect "?shashank \r" spawn passwd $user expect "?Changing password for user $user. \r" send "Password\r" expect "?Retype new password: \r" send "Password\r" expect eof puts "Password for $user set." EOF } done </home/shashank/userslist.txt
Below is the output with a list of 3 test users :-
spawn useradd test5 test5 added spawn passwd test5 Changing password for user test5. New password: BAD PASSWORD: it is based on a dictionary word Retype new password: passwd: all authentication tokens updated successfully. Password for test5 set. spawn useradd test6 test6 added spawn passwd test6 Changing password for user test6. New password: BAD PASSWORD: it is based on a dictionary word Retype new password: passwd: all authentication tokens updated successfully. Password for test6 set. spawn useradd test7 test7 added spawn passwd test7 Changing password for user test7. New password: BAD PASSWORD: it is based on a dictionary word Retype new password: passwd: all authentication tokens updated successfully. Password for test7 set.
One thought on “Using Expect Command To Automate User Creation on RHEL 6 – Part II”