To add a new user on remote unix host
#!/usr/local/bin/expect -f
# ./add.sh adminuser adminpass IP/Host user_to_be_added userpass_for_new_user
# Manish Kumar Arya m@mka.in
set user [lrange $argv 0 0]
set password [lrange $argv 1 1]
set ipaddr [lrange $argv 2 2]
set username [lrange $argv 3 3]
set userpass [lrange $argv 4 4]
set timeout -1
spawn ssh $user@$ipaddr
match_max 100000
expect “*?assword:*”
send — “$password\r”
send — “\r”
expect “*#”
send — “useradd $username\r”
send — “\r”
expect “*#”
send — “passwd $username\r”
expect “?ew*?assword*”
send — “$userpass\n”
expect “?etype*?assword*”
send — “$userpass\r”
expect “#”
send — “logout\r”
expect eof
To modify user password on remote unix host
#!/usr/local/bin/expect -f
# ./mod.sh adminuser adminpassowrd IP/Host user_pass_to_be_changed newpassword
# Manish Kumar Arya m@mka.in
set user [lrange $argv 0 0]
set password [lrange $argv 1 1]
set ipaddr [lrange $argv 2 2]
set username [lrange $argv 3 3]
set userpass [lrange $argv 4 4]
set timeout -1
spawn ssh $user@$ipaddr
match_max 100000
expect “*?assword:*”
send — “$password\r”
send — “\r”
expect “*#”
send — “passwd $username\r”
expect “?ew*?assword*”
send — “$userpass\n”
expect “?etype*?assword*”
send — “$userpass\r”
expect “#”
send — “logout\r”
expect eof
To delete a user on remote unix host
#!/usr/local/bin/expect -f
# ./del.sh adminuser adminpassword IP/Host user_to_be_deleted
# Manish Kumar Arya m@mka.in
set user [lrange $argv 0 0]
set password [lrange $argv 1 1]
set ipaddr [lrange $argv 2 2]
set username [lrange $argv 3 3]
set timeout -1
spawn ssh $user@$ipaddr
match_max 100000
expect “*?assword:*”
send — “$password\r”
send — “\r”
expect “*#”
send — “userdel -r $username\r”
send — “\r”
expect “#”
send — “logout\r”
expect eof