问题是命令send" xxxxx"只是不发送。该命令似乎完全被忽略了。
在下面的脚本中,我可以登录服务器,但不再向服务器发送发送命令。
最后有一个输出示例。
超时陷阱用于调试。
#!/usr/bin/expect -f
## getver_t.sh - Use telnet to get ver from Cisco devices
## Could be asked for login ID or just pasword
set pass {Password1 Pass2}
set index 0
set timeout 5
set host [lindex $argv 0 ]
spawn ssh -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null admin@$host
expect {
"assword:" {
send_user "\n--- sending [lindex $pass $index] ---\n"
send "[lindex $pass $index]\r"
incr index
exp_continue
}
"\>" {
send_user "\n--- I see a prompt ---\n"
send -- "ter len 0\r"
sleep 3
send -- "show inv\r"
sleep 3
send -- "show ver\r"
send_user "\n--- Time to end ---\n"
sleep 3
send -- "exit\r"
sleep 3
exit
}
timeout {
send -- "\r"
set timeout 5
send_user "\n--- TIMED OUT 1 ---\n"
send -- "ter len 0\r"
sleep 3
send -- "show inv\r"
sleep 3
send -- "show ver\r"
sleep 3
send -- "exit\r"
sleep 3
exit
}
}
send_user "\n--- out side land ---\n"
send "\r"
expect {
timeout {
send_user "\n--- TIMED OUT 2 ---\n"
}
}
这就是我看到的运行脚本。
$ ./getver_s.sh server1
spawn ssh -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null admin@server1
Warning: Permanently added 'server1,10.1.1.3' (RSA) to the list of known hosts.
Password:
--- sending Password1 ---
Password:
--- sending Pass2 ---
------------------------------------------------------------
Welcome to server1
------------------------------------------------------------
server1>
--- I see a prompt ---
--- Time to end ---
$
我看到" ---我看到了一个提示---"和" ---时间结束---"消息 但send_user命令之间的send命令不会发送到服务器。
我已经重写了这个脚本改变了逻辑并将其从不同版本的服务器(Debian7& 8)中运行,但结果始终是相同的。
为什么那些发送命令没有被发送?
添加了期望-d输出
Login Banner
-------------- CUT ------------------
Password:"
--- sending Password1 ---
send: sending "Password1\r" to { exp7 }
expect: continuing expect
expect: does " " (spawn_id exp7) match glob pattern "assword:"? no
">"? no
expect: does " \r\n" (spawn_id exp7) match glob pattern "assword:"? no
">"? no
Password:
expect: does " \r\nPassword: " (spawn_id exp7) match glob pattern "assword:"? yes
expect: set expect_out(0,string) "assword:"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) " \r\nPassword:"
--- sending Pass2 ---
send: sending "Pass2\r" to { exp7 }
expect: continuing expect
expect: does " " (spawn_id exp7) match glob pattern "assword:"? no
">"? no
expect: does " \r\n" (spawn_id exp7) match glob pattern "assword:"? no
">"? no
-------------- CUT ------------------
motd
-------------- CUT ------------------
-\r\nServer1>"
--- I see a prompt ---
send: sending "ter len 0\r" to { exp7 }
send: sending "show inv\r" to { exp7 }
send: sending "show ver\r" to { exp7 }
send: sending "exit\r" to { exp7 }
Server1$