我正在嘗試自動化一些用於設置打印機的telnet命令。如果我手動執行,終端的預期輸出如下:如何「期待」一個期望的變量?
Ebins-Mac-Mini:~ ebin$ telnet 192.168.4.104
Trying 192.168.4.104...
Connected to 192.168.4.104.
Escape character is '^]'.
Welcome to TSP100LAN TELNET Utility.
Copyright(C) 2008 Star Micronics co., Ltd.
<< Connected Device >>
Device Model : TSP143 (STR_T-001)
MAC Address : 00:11:62:08:5B:F6
login: root
password: ******
Hello root
=== Main Menu ===
1) IP Parameters Configuration
2) System Configuration
3) Change Password
96) Display Status
97) Reset Settings to Defaults
98) Save & Restart
99) Quit
Enter Selection: 1
=== IP Parameters Menu ===
1) Static
IP Address : 192.168.4.104
Subnet Mask : 255.255.255.0
Default Gateway : 192.168.4.1
2) Dynamic
DHCP : DISABLE
99) Back to Main Menu
Enter Selection: 1
=== Static IP Address ===
1) IP Address : 192.168.4.104
2) Subnet Mask : 255.255.255.0
3) Default Gateway : 192.168.4.1
99) Back to IP Address Menu
Enter Selection:
我設法完成它直到第二個選擇。但是,對於第二種選擇,IP地址:192.168.4.104應該是我作爲參數傳遞的變量。我如何「期待」這?以下是我的代碼。
#!/usr/bin/expect -f
set timeout 20
set IPaddress [lindex $argv 0]
set Username "root"
set Password "public"
spawn telnet $IPaddress
expect "login: "
send "$Username\r"
expect "password: "
send "$Password\r"
expect {"Hello root\n\n"
"=== Main Menu ===\n"
"1) IP Parameters Configuration\n"
"2) System Configuration\n"
"3) Change Password\n"
"96) Display Status\n"
"97) Reset Settings to Defaults\n"
"98) Save & Restart\n"
"99) Quit\n\n"
"Enter Selection:"
}
send 1
send "\r"
expect {"=== IP Parameters Menu ===\n"
" 1) Static\n"
" IP Address : "
"$IPaddress\n"
" Subnet Mask : 255.255.255.0\n"
" Default Gateway : 192.168.4.1\n"
" 2) Dynamic\n"
" DHCP : DISABLE\n"
" 99) Back to Main Menu\n\n"
" Enter Selection: "
}
send 1
send "\r"
看起來程序到達超時並剛剛退出。我懷疑我寫了錯誤的期望線。任何幫助,將不勝感激。
你並不需要匹配於預期腳本* *整體輸出。您只需要期望在腳本方面需要做更多工作時輸出信號。 –
意味着我只需要「期待」「輸入選擇」只? – tuxebin