2016-02-15 44 views
0

我想通過SSH連接登錄幾個設備。我想抓住,如果我有用戶可以登錄或不。如果是,請繼續執行一些命令,如果沒有退出腳本。無論我是否有權限,我都需要在日誌中寫入。有條件的國家

這是我的代碼:

#!/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 [email protected]$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 
     } 

特的問題,我有,當我有機會獲得該檢查的第一行,如果有什麼問題想出了錯誤,但完成的預期是正確的它,我可以在日誌中寫下'一切都好,但是有這個錯誤。

 
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) 

我有工作或不工作的LOG就是這樣。

根@ servidor:在/ var/WWW/RED_CENTRAL /性能/ IP_Engine#更 LOG/LOG_DEVICES/log_HIPODR-MEX9039-X8-CA.txt用戶registrotpe有 到10.XXX.XX.XX成功訪問-HIPODR-MEX9039-X8-CA在 2016-02-15_14:24

根@ servidor:在/ var/WWW/RED_CENTRAL /性能/ IP_Engine#更 LOG/LOG_DEVICES/log_BENJUA-MEX9297-X8-CA。 txt用戶registrotpe does 無權訪問10.XXX.XX.XX-BENJUA-MEX9297-X8-CA 2016-02-15_14:39

我已經嘗試在調試模式下執行,但我無法找到解決方案。

請問您能幫我嗎?

回答

1

你的問題是在錯誤消息中:「spawn id exp7 not open」。您需要處理腳本中的連接問題。這裏是我用於telnet連接的一個,同樣的timeouteof關鍵字也應該適用於SSH:

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" 
    } 
}