【发布时间】:2014-06-26 13:54:49
【问题描述】:
我正在尝试登录 Cisco 交换机并运行命令列表。
使用以下代码,我可以登录设备、启用和配置终端:
# Connect to single host, enable, and configure
proc connect {host payload username password enablepassword} {
send_user "Connecting to: $host $payload $username $password $enablepassword\n"
spawn ssh -o "StrictHostKeyChecking no" -l $username $host
# # Pardon the rudeness; some switches are upper case, some are lower case
expect "assword:"
send "$password\r"
# Switch to enable mode
expect ">"
send "en\r"
expect "assword:"
send "$enablepassword\r"
expect "*#"
send -- "conf t\r"
expect "config*#"
}
但是,使用以下代码,我得到以下输出。 ($payload 包含一个文件,每行有一个 IOS 命令)
proc drop_payload {payload} {
set f [open "$payload"]
set payload [split [read $f] "\n"]
close $f
foreach pld $payload {
send -- "$pld\r"
expect "config*#"
sleep 2
}
}
我的期望是这个循环将遍历文件中的每一行,但是,Expect 调试(来自 exp_internal 1)如下:
HOST-0001#
expect: does " \r\HOST-0001#" (spawn_id exp7) match glob pattern "*#"? yes
expect: set expect_out(0,string) " \r\nHOST-0001#"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) " \r\nHOST-0001#"
send: sending "conf t\r" to { exp7 }
expect: does "" (spawn_id exp7) match glob pattern "config*#"? no
c
expect: does "c" (spawn_id exp7) match glob pattern "config*#"? no
o
expect: does "co" (spawn_id exp7) match glob pattern "config*#"? no
n
expect: does "con" (spawn_id exp7) match glob pattern "config*#"? no
f
expect: does "conf" (spawn_id exp7) match glob pattern "config*#"? no
expect: does "conf " (spawn_id exp7) match glob pattern "config*#"? no
t
expect: does "conf t" (spawn_id exp7) match glob pattern "config*#"? no
expect: does "conf t\r\n" (spawn_id exp7) match glob pattern "config*#"? no
Enter configuration commands, one per line. End with CNTL/Z.
HOST-0001(config)#
expect: does "conf t\r\nEnter configuration commands, one per line. End with CNTL/Z.\r\nHOST-0001(config)#" (spawn_id exp7) match glob pattern "config*#"? yes
expect: set expect_out(0,string) "configuration commands, one per line. End with CNTL/Z.\r\nHOST-0001(config)#"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "conf t\r\nEnter configuration commands, one per line. End with CNTL/Z.\r\nHOST-0001(config)#"
}end: sending "no logging 172.x.x.20\r" to { exp0 no logging 172.x.x.20
expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out
}end: sending "no logging 172.x.x.210\r" to { exp0 no logging 172.x.x.210
expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out
}end: sending "no logging 172.x.x.9\r" to { exp0 no logging 172.x.x.9
expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out
}end: sending "no logging 172.x.x.210\r" to { exp0 no logging 172.x.x.210
expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out
}end: sending "no logging 172.x.x.20\r" to { exp0 no logging 172.x.x.20
expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out
}end: sending "logging 172.x.x.50\r" to { exp0 logging 172.x.x.50
expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out
我很困惑为什么它试图期待正在发送到主机的“conf t”;没有收到。
我也很困惑,为什么在应用 conf t 后任何命令结束时都没有按下开关,而是超时。
【问题讨论】:
-
这里的问题是 ssh 会话在一个过程中打开,但随后尝试从另一个函数使用它被发送到另一个(不存在的)衍生会话。试图强制第二个过程使用初始 ssh 连接....