I have an expect
script which performs an SSH to a list of servers with the credentials mentioned in the script. It is not throwing any error when it is unable to SSH to a particular server and continues with the next server in the list. I want the script to let me know when it is unable to SSH with the password given. What changes should be made to the script to get this done?
The first script is a shell script which calls the expect script
#!/usr/bin/ksh
for host_name in `cat list`
do
/home/user1/ssh_script $host_name
done
#!/usr/local/bin/expect -f
set timeout 1
set host_name [lindex $argv 0]
spawn ssh -q -o StrictHostKeyChecking=no "user1\@$host_name"
expect "*assword:"
send "abcd@123\r";
expect "$\r"
puts "user1 loggedin successfully"
exit
interact
proc
on the 2nd line when copying the program. I've edited the answer with this fix. – meuh Jan 17 '17 at 12:34"$\r"
is correct? Normally, a shell prompt would be something like" $"
or"$ "
or even just"$"
. – meuh Jan 18 '17 at 14:02Password:
prompt; I've edited my answer to show how. If you have a bad hostname the spawn will fail and expect with fail with exit code 1 on thesend
command. You can also get lots of debug by replacing-f
in the first line by-d
. – meuh Jan 18 '17 at 15:531.)The spawned process is exiting after throwing the message bad password when it encouters password: prompt for second time.
2.)combining two commands "send_user" and "exit" is done by using flower braces {}.
3.)For servers in which SSH works fine, does the expect line with password: (2nd time) is skipped?
– Su_scriptingbee Jan 19 '17 at 16:32"assword: "
, and the string"$\r"
. So when the ssh login works ok, it will match the"$\r"
, and so continue to the next command, as no action was specified for this match. – meuh Jan 19 '17 at 17:32