Hi
First of all I tried lot of forums to create my first expect script. But I am totally stumped at the last step. I am no Linux Admin or ever trained in Linux. I just tried to create the script logically
History :
I need to to change my password across lot of servers in Linux over ssh
There can be following scenario
1. I have never logged in to the server ever and I am getting RSA Key confirmation to be added YES/NO for the one time
2. I have logged in to the server before and the password is still working and I should be able to issue passwd command to change my password
3. I have logged in to the server before and the password is not working and forcing me to change the password
My two scripts:
1. runip.sh - Simple shell script to run through all the IPs mentioned in iplist.txt
Quote:
#!/bin/bash
for ip in `cat iplist.txt` #All Ips are mentioned in iplist.txt
do
/home/user1/script/change.sh $ip # Calling change script and passing IP as parameter
done
Now comes the trouble script which is going in loops and not able to break out
Quote:
#!/usr/bin/expect -f
set ip [lindex $argv 0] #Catching the ip from shell script
spawn ssh user1@$ip
expect {
"sure you want to continue connecting (yes/no)?" {send "Yes\r" ; exp_continue} # In case of first time login you need to add the token YES/NO bla bla
"user1@$ip's password:" {send "Password\r" ; exp_continue} # IF getting password Prompt entering the current password
# Now two scenario 1. You get bash prompt as you have successfully logged in OR 2. You get change password prompt as your password has already expired so..
"~]$ " {send "passwd\r" ; exp_continue} # Get prompt that we have successfully logged in and sending passwd command to start initiating change of password
# In either scenario above the below code should get executed if I am getting change password prompt immidiately after login as password has expired OR I have started passwd command
"(current) UNIX password:" {send "Password\r" ; exp_continue} # Entering Current Password
"New UNIX password:" {send "P@ssw0rd\r" ; exp_continue} # Entering New password
"Retype new UNIX password:" {send "P@ssw0rd\r" ; exp_continue} # Re-entering New password
"~]$ " {send "exit\r"} # Now this is a trial logically after re-entering password I should get the same prompt back and can exit from here - doesn't work it seems it goes back to the initial prompt and issue passwd command again and goes in loop
eof {break} # Tried this thought it will break no go
sleep 5
exit 0
}
close $spawn_id
I have no clue how to break free and exit from the passwd loop and go to the next IP as in the script
Any help will be great and I am sure this is a common script in lot of environment helping users to do manually
Usually the bash prompt in environemnt looks like the below
[user1@server1 ~]$