【发布时间】:2016-01-17 16:12:38
【问题描述】:
我正在尝试自动化一些用于设置打印机的 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"
程序似乎超时并退出。我怀疑我写错了期望行。任何帮助,将不胜感激。
【问题讨论】:
-
您不需要匹配期望脚本中的 整个 输出。当您必须从脚本方面做更多事情时,您只需要期待输出信号。
-
意味着我只需要“期待”“输入选择”吗?