2014-06-27 39 views
0

我試圖通過telnet連接到它通過期望重新啓動D-Link路由器。 問題是我無法執行(通過期望腳本)路由器支持的任何命令。Bash和expect:無法通過telnet重新啓動路由器

首先,我告訴你一個短Telnet會話使用我的路由器:

telnet 192.168.1.1 
Trying 192.168.1.1... 
Connected to 192.168.1.1. 
Escape character is '^]'. 
BCM96338 ADSL Router 
Login: admin 
Password: 
> swversion show 
EU_3-12-01-1R00.A2pB026.d20m 
> logout 

Bye bye. Have a nice day!!! 
Connection closed by foreign host. 

現在我試圖讓它自動與期待劇本,但我不能使它發揮作用。這裏的腳本:

#!/usr/bin/expect -f 

#exp_internal 1 
set timeout 30 

#router username 
set name admin 

#command to execute 
set routercmd "swversion show" 

#router password 
set pass mypassword 

#router IP address 
set routerip 192.168.1.1 

spawn telnet $routerip 
# send username & password 
expect "Login: " 
send "$name\r" 
expect "Password: " 
send "$pass\r" 
expect "> " 
send "$routercmd\r" 
expect "> " 

當我執行該腳本,它被卡在密碼提示:

./reboot_dut.sh 
spawn telnet 192.168.1.1 
Trying 192.168.1.1... 
Connected to 192.168.1.1. 
Escape character is '^]'. 
BCM96338 ADSL Router 
Login: admin 
Password: 

如果我取消了#exp_internal 1號線我得到:

./reboot_dut.sh 
spawn telnet 192.168.1.1 
parent: waiting for sync byte 
parent: telling child to go ahead 
parent: now unsynchronized from child 
spawn: returns {6398} 

expect: does "" (spawn_id exp7) match glob pattern "Login: "? no 
Trying 192.168.1.1... 
expect: does "Trying 192.168.1.1..." (spawn_id exp7) match glob pattern "Login: "? no 


expect: does "Trying 192.168.1.1...\r\n" (spawn_id exp7) match glob pattern "Login: "? no 
Connected to 192.168.1.1. 
expect: does "Trying 192.168.1.1...\r\nConnected to 192.168.1.1." (spawn_id exp7) match glob pattern "Login: "? no 


expect: does "Trying 192.168.1.1...\r\nConnected to 192.168.1.1.\r\n" (spawn_id exp7) match glob pattern "Login: "? no 
Escape character is '^]'. 
expect: does "Trying 192.168.1.1...\r\nConnected to 192.168.1.1.\r\nEscape character is '^]'." (spawn_id exp7) match glob pattern "Login: "? no 


expect: does "Trying 192.168.1.1...\r\nConnected to 192.168.1.1.\r\nEscape character is '^]'.\r\n" (spawn_id exp7) match glob pattern "Login: "? no 
BCM96338 ADSL Router 

expect: does "Trying 192.168.1.1...\r\nConnected to 192.168.1.1.\r\nEscape character is '^]'.\r\nBCM96338 ADSL Router\r\n" (spawn_id exp7) match glob pattern "Login: "? no 
Login: 
expect: does "Trying 192.168.1.1...\r\nConnected to 192.168.1.1.\r\nEscape character is '^]'.\r\nBCM96338 ADSL Router\r\nLogin: " (spawn_id exp7) match glob pattern "Login: "? yes 
expect: set expect_out(0,string) "Login: " 
expect: set expect_out(spawn_id) "exp7" 
expect: set expect_out(buffer) "Trying 192.168.1.1...\r\nConnected to 192.168.1.1.\r\nEscape character is '^]'.\r\nBCM96338 ADSL Router\r\nLogin: " 
send: sending "admin\r" to { exp7 } 

expect: does "" (spawn_id exp7) match glob pattern "Password: "? no 
a 
expect: does "a" (spawn_id exp7) match glob pattern "Password: "? no 
dmin 
Password: 
expect: does "admin\r\nPassword: " (spawn_id exp7) match glob pattern "Password: "? yes 
expect: set expect_out(0,string) "Password: " 
expect: set expect_out(spawn_id) "exp7" 
expect: set expect_out(buffer) "admin\r\nPassword: " 
send: sending "mypassword\r" to { exp7 } 

expect: does "" (spawn_id exp7) match glob pattern "> "? no 
expect: timed out 
send: sending "swversion show\r" to { exp7 } 

expect: does "" (spawn_id exp7) match glob pattern "> "? no 

> swversion show 

expect: does "\r\n> swversion show\r\n" (spawn_id exp7) match glob pattern "> "? yes 
expect: set expect_out(0,string) "> " 
expect: set expect_out(spawn_id) "exp7" 
expect: set expect_out(buffer) "\r\n> " 
+0

呀,看起來卡住。嘗試一個'autoexpect'會話並查看結果腳本的外觀。 –

+0

這是http://pastebin.com/fLVf2wL9。奇怪的事實是,如果我嘗試執行由autoexpect生成的期望腳本,它會再次卡住... – condorwasabi

回答

1

SOLUTION:我找到了解決問題的辦法。我試圖重啓的路由器是D-LINK 2640B。一個工作預計腳本該路由器爲:

#!/usr/bin/expect -f 

spawn telnet ROUTER_IP 
match_max 10000 
expect *login:* 
sleep 2 
send -- "USERNAME\r" 
expect *assword:* 
sleep 2 
send -- "PASSWORD\r" 
expect *>* 
send -- "\r" 
expect *>* 
send -- "COMMAND\r" 
expect *>* 
send -- "\r" 
expect *>* 
send -- "logout\r" 
0

我想說,你需要匹配你的提示符「>」,而不是「>」(即它之後沒有空格)。

+0

它不會打印命令的返回值。這裏是一個調試信息的打印與調試信息打印與腳本修改爲您建議http://pastebin.com/HsNFc2Ka – condorwasabi

+0

有趣的......它似乎D-LINK路由器只發送提示_after_你已經發送第一個命令。 那麼,如果您在提供密碼後發送_two_回車(而不是使用),會有幫助嗎?可以發送$ pass \ r \ r「',或者單獨發送命令。 – James

+0

還有一件事...如果我沒有記錯,期望只會將輸出打印到通過'send'發送的命令_下一個成功的匹配操作_。所以你沒有看到任何輸出的原因是因爲發送命令後沒有後續的匹配。如果我的上述建議(雙CR)有效,那麼你需要對你的命令(使用兩個\ r)進行相同的操作,這樣在收到命令輸出expect後就會成功匹配。說得通? – James