期望 telnet 到多个 cisco 设备并执行 show 运行
expect telnet to multiple cisco devices and execute show run
我有以下脚本,它们应该从文件 device-list.txt
中提取 IP 地址,然后远程登录到设备,登录并执行显示 运行。该脚本成功登录到设备,但之后给我一个错误。我哪里出错了?
for device in `cat device-list.txt`; do ./test4 $device;done
cat test4
#!/usr/bin/expect -f
set hostname [lindex $argv 0]
set user myusername
set pass mypassword
set timeout 10
log_file -a ~/results.log
send_user "\n"
send_user ">>>>> Working on $hostname @ [exec date] <<<<<\n"
send_user "\n"
spawn telnet $hostname
expect "Username:"
send "$user\n"
expect "Password:"
send "$pass\n"
expect "#"
send “term len 0\n”
send “show running-config\n”
expect “end\r”
send “\n”
send “exit\n”
User Access Verification
Username: myusername
Password:
ROUTER#usage: send [args] string
while executing
"send “term len 0\n”"
(file "./test4" line 26)
您的问题是由于双引号不正确造成的。您必须使用双引号 "
,而不是弯引号 “
send “term len 0\n” ; # Wrong
send "term len 0\r"; # Correct
注:
基本上,Expect
将使用两个可行的命令,例如 send
和 expect
。如果使用 send
,则之后必须有 expect
(在大多数情况下)。 (反之亦然不是强制性的)
这是因为如果没有它,我们将错过派生进程中发生的事情,因为 Expect
将假设您只需要发送一个字符串值而不期望会话中的任何其他内容。
因此,我们建议在每个 send
之后使用 expect
,以确保我们的命令实际发送到衍生进程。 (发送了一些指令如term len 0\r
后,你还没有使用expect
)。另外,使用 \r
而不是 \n
.
我有以下脚本,它们应该从文件 device-list.txt
中提取 IP 地址,然后远程登录到设备,登录并执行显示 运行。该脚本成功登录到设备,但之后给我一个错误。我哪里出错了?
for device in `cat device-list.txt`; do ./test4 $device;done
cat test4
#!/usr/bin/expect -f
set hostname [lindex $argv 0]
set user myusername
set pass mypassword
set timeout 10
log_file -a ~/results.log
send_user "\n"
send_user ">>>>> Working on $hostname @ [exec date] <<<<<\n"
send_user "\n"
spawn telnet $hostname
expect "Username:"
send "$user\n"
expect "Password:"
send "$pass\n"
expect "#"
send “term len 0\n”
send “show running-config\n”
expect “end\r”
send “\n”
send “exit\n”
User Access Verification
Username: myusername
Password:
ROUTER#usage: send [args] string
while executing
"send “term len 0\n”"
(file "./test4" line 26)
您的问题是由于双引号不正确造成的。您必须使用双引号 "
,而不是弯引号 “
send “term len 0\n” ; # Wrong
send "term len 0\r"; # Correct
注:
基本上,Expect
将使用两个可行的命令,例如 send
和 expect
。如果使用 send
,则之后必须有 expect
(在大多数情况下)。 (反之亦然不是强制性的)
这是因为如果没有它,我们将错过派生进程中发生的事情,因为 Expect
将假设您只需要发送一个字符串值而不期望会话中的任何其他内容。
因此,我们建议在每个 send
之后使用 expect
,以确保我们的命令实际发送到衍生进程。 (发送了一些指令如term len 0\r
后,你还没有使用expect
)。另外,使用 \r
而不是 \n
.