I made a Bash script which uses an expect script to automate ssh logins.The script connects to multiple servers and runs some commands. The bash script prompts for login credentials once.
I want to incorporate a feature wherein the script terminates if the login fails for the first server to avoid the script checking for next servers resulting in the user account getting locked. The account lockout happens for 3 consecutive login failures and the number of server the script tries to connect is more than 3.
This is the snippet in the bash script which calls the expect script.
countu=0
for servername in $(cat $linux_host_list)
do
./script.expect $LUSERNAME $LPASS $servername Linux >> linux_log_file.txt & < /dev/null
let countl=countl+1
done
and here is the expect script (script.expect
) snippet
#!/usr/bin/expect -f
set timeout 30
set username [lindex $argv 0]
set SPASS [lindex $argv 1]
set servername [lindex $argv 2]
set case [lindex $argv 3]
set prompt "(%|#|\\$|%\]) $"
switch $case {
Linux {
log_user 0
spawn ssh -o StrictHostKeyChecking=no $username@$servername
expect {
"assword:" {
send "$SPASS\r"
expect -re "$prompt"
}
expect -re "$prompt"
}
send "sudo su -\r"
expect {
"assword:" { send "$SPASS\r" }
}
expect -re "$prompt"
log_user 1
send "opcagt -status ; opctemplate -l ; cat watch.cf 2> /dev/null\r"
expect -re "$prompt"
log_user 0
send "exit\r"
expect -re "$prompt"
log_user 1
}
I tried grabbing the bash command output ($?
) assuming that the bash command would return a non zero value if login fails for incorrect password in the expect script but that did not work out.
Any suggestions would be much appreciated.