2013-03-14 30 views
0
spawn ssh "[email protected]$host" 
    match_max 10000000 

    expect { 
     timeout { send_user "\nFailed to get password prompt\n"; exit 1 } 
     eof { send_user "\nSSH failure for $host\n"; exit 1 } 
      "*yes/no*" 
      { send -- "yes\r" } 

      "*?assword:*" 
    } 

    send -- "[read [open "passwordfile" r]]\r" 
    expect { 
       timeout { send_user "\nLogin incorrect\n"; exit 1 } 
       eof { send_user "\nSSH failure for $host\n"; exit 1 } 

     -re "$prompt" 
      { send -- "\r" } 
      } 

這是我的代碼的一部分,但問題是有時我可能會得到一個肯定的提示,有時我可能不會那麼它應該是這樣的:預期正則表達式的條件匹配?

if { yes no prompt found } { 
    send yes 
    } 
    else 
    { 
    look for password prompt 
    } 

但上述線路不能正常工作。這是什麼樣的調試數據顯示

parent: waiting for sync byte 
parent: telling child to go ahead 
parent: now unsynchronized from child 
spawn: returns {13806} 

expect: does "" (spawn_id exp8) match glob pattern "*yes/no*"? no 
"*?assword:*"? no 

expect: does "The authenticity of host 'swes-elmpci-a-01.tellus (10.1.239.1)' can't be established.\r\nRSA key fingerprint is 1c:d8:f6:9b:2d:6d:4c:e5:d9:eb:27:ea:1f:2c:99:57.\r\nAre you sure you want to continue connecting (yes/no)? " (spawn_id exp8) match glob pattern "*yes/no*"? yes 
expect: set expect_out(0,string) "The authenticity of host 'swes-elmpci-a-01.tellus (10.1.239.1)' can't be established.\r\nRSA key fingerprint is 1c:d8:f6:9b:2d:6d:4c:e5:d9:eb:27:ea:1f:2c:99:57.\r\nAre you sure you want to continue connecting (yes/no)? " 
expect: set expect_out(spawn_id) "exp8" 
expect: set expect_out(buffer) "The authenticity of host 'swes-elmpci-a-01.tellus (10.1.239.1)' can't be established.\r\nRSA key fingerprint is 1c:d8:f6:9b:2d:6d:4c:e5:d9:eb:27:ea:1f:2c:99:57.\r\nAre you sure you want to continue connecting (yes/no)? " 
send: sending "yes\r" to { exp8 } 
send: sending "dblg\n\r" to { exp8 } 

expect: does "" (spawn_id exp8) match regular expression "([A-Z][A-Z][A-Z](s|x)-elmPCI-A-01.*#)$"? no 

expect: does "yes\r\ndblg\r\n\r\n" (spawn_id exp8) match regular expression "([A-Z][A-Z][A-Z](s|x)-elmPCI-A-01.*#)$"? no 

expect: does "yes\r\ndblg\r\n\r\nWarning: Permanently added 'swes-elmpci-a-01.tellus' (RSA) to the list of known hosts.\r\r\n" (spawn_id exp8) match regular expression "([A-Z][A-Z][A-Z](s|x)-elmPCI-A-01.*#)$"? no 

expect: does "yes\r\ndblg\r\n\r\nWarning: Permanently added 'swes-elmpci-a-01.tellus' (RSA) to the list of known hosts.\r\r\n\r\r\n##############################################\r\r\n#\r\r\n#  You are connected to:\r\r\n#  Sweden, elm, Cabinet A\r\r\n#  WS-C3750X-24PS - S/N FDO1640H0JQ\r\r\n#  WS-C3750v2-24PS - S/N <serial>\r\r\n#\r\r\n#\r\r\n#\r\r\n#  Unauthorized access prohibited\r\r\n#  Your actions are logged\r\r\n#\r\r\n##############################################\r\r\n" (spawn_id exp8) match regular expression "([A-Z][A-Z][A-Z](s|x)-elmPCI-A-01.*#)$"? no 

expect: does "yes\r\ndblg\r\n\r\nWarning: Permanently added 'swes-elmpci-a-01.tellus' (RSA) to the list of known hosts.\r\r\n\r\r\n##############################################\r\r\n#\r\r\n#  You are connected to:\r\r\n#  Sweden, elm, Cabinet A\r\r\n#  WS-C3750X-24PS - S/N FDO1640H0JQ\r\r\n#  WS-C3750v2-24PS - S/N <serial>\r\r\n#\r\r\n#\r\r\n#\r\r\n#  Unauthorized access prohibited\r\r\n#  Your actions are logged\r\r\n#\r\r\n##############################################\r\r\nPassword:" (spawn_id exp8) match regular expression "([A-Z][A-Z][A-Z](s|x)-elmPCI-A-01.*#)$"? no 

回答

2

我猜exp_continue是你在找什麼。例如:

expect { 
    -nocase "yes/no" { 
     send "yes\r" 
     exp_continue 
    } 

    -nocase "password:" { 
     send "password\r" 
    } 

    ... ... 
} 

這是從期望手冊:

命令exp_continue允許expect本身繼續執行,而 不是返回,因爲它通常會。

0

的事情是,經過「是/否」的提示,你仍然會得到一個密碼提示,所以你可以使用循環,例如。這樣的連接塊的東西會工作:

#!/usr/bin/expect 
set remote_server [lrange $argv 0 0] 
set timeout 10 
spawn ssh "[email protected]$host" 
while 1 { 
    expect { 
    "*yes/no*"  {send "yes\r"} 
    "denied" { 
       log_file /var/log/expect_msg.log 
       send_log "Can't login to $remote_server. Check username and password\n"; 
       exit 1 
      } 
    "failed" { 
       log_file /var/log/expect_msg.log 
       send_log "Host $remote_server exists. Check ssh_hosts file\n"; 
       exit 2 
      } 
    timeout { 
       log_file /var/log/expect_msg.log 
       send_log "Timeout problem. Host $remote_server doesn't respond\n"; 
       exit 3 
      } 
    "refused" { 
       log_file /var/log/expect_msg.log 
       send_log "Host $remote_server refused to SSH.\n" 
       exit 4 
       } 
    "#"  {break} 
    } 
} 

更改斷開符號爲您的系統,而這個塊後,你可以添加你的代碼。希望這可以幫助。

+0

這有效,但用'exp_continue'你可以刪除'while 1'循環和一級縮進。 :) – pynexj 2013-03-15 01:52:51