Quote

Using Expect Command To Automate User Creation on RHEL 6 – Part II


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 clear understanding of expect command.

Below is how to add users to the server using a while loop & expect 🙂

Execute this as :

[shashank@server ~]$ sudo sh ~shashank/scripts/useradd.sh

Hers 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 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.
Advertisements

One thought on “Using Expect Command To Automate User Creation on RHEL 6 – Part II

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s