Hugo Jahir Munguia
Hugo Jahir Munguia

Reputation: 1

Conditional States in Expect

I'm trying to log in several devices trough SSH connection. I want to catch if the user that I have is able to log in or not. If yes proceed with some commands, if not exit from the script. Whether I have a permission or not I need to write in a log.

This is my code:

#!/usr/bin/expect -f

set timeout 10


set host [lindex $argv 0]
set user "username"
set pwd "blahblahblah"
set devicename [lindex $argv 1]

set DATE [exec date +%Y-%m-%d_%H:%M] 

set log_devicename [open log_$devicename.txt "a+"]


spawn ssh -p 20444 $user@$host
log_user 0

expect {
        "*assword: " {
                send "$pwd\r";
                expect "Permission denied, please try again." {puts $log_devicename "User $user does not have access to $host-$devicename at $DATE";exit 22} 
                expect ">" {send "display startup\r"} 
                expect ">" {send "quit\r";puts $log_devicename "User $user have successful access to $host-$devicename at $DATE"}
                expect "timeout" {puts $log_devicename "$host - $devicename it's not reachable at $DATE cannot execute StartUp Script";exit 111}
                exp_continue
                    }

                eof 
        }

Te problem that i have it's when i have access the first line which check if there's something wrong came up with error but finish of the expect it's the correct one, i can write in the log that' everything it's ok, but with this error.

expect: continuing expect after update expect: spawn id exp7 not open
    while executing
"expect {
        "*assword: " {
                send "$pwd\r";
                expect "Permission denied, please try again." {puts $log_devicename "Us..."
    (file "test.exp" line 19)

The LOG that i have either work or not it's this.

root@servidor:/var/www/RED_CENTRAL/Performance/IP_Engine# more LOG/LOG_DEVICES/log_HIPODR-MEX9039-X8-CA.txt User registrotpe have successful access to 10.XXX.XX.XX-HIPODR-MEX9039-X8-CA at 2016-02-15_14:24

root@servidor:/var/www/RED_CENTRAL/Performance/IP_Engine# more LOG/LOG_DEVICES/log_BENJUA-MEX9297-X8-CA.txt User registrotpe does not have access to 10.XXX.XX.XX-BENJUA-MEX9297-X8-CA at 2016-02-15_14:39

I've already tried to execute in Debug Mode but I'm not able to find the solution.

Could you help me please ?

Upvotes: 0

Views: 531

Answers (1)

miken32
miken32

Reputation: 42695

Your problem is right in the error message: "spawn id exp7 not open". You need to handle connection problems in your script. Here's one I use for telnet connections, the same timeout and eof keywords should work for SSH as well:

spawn /usr/bin/telnet $hostname

# Allow this script to handle connection issues
expect {
    timeout {
        send_user "\nTimeout - Check $hostname\n\n";
        exit 1
    }
    eof {
        send_user "\nConnection failed - Check $hostname\n\n";
        exit 1
    }
    "*#" {
    }
    "*sername:" {
        send "$username\n"
        expect "*assword:"
        send "$password\n"
    }
}

Upvotes: 1

Related Questions