我是Expect脚本的新手。我正在尝试创建一个通过SSH连接到路由器/交换机的过程,如果失败则回退到telnet。在将其放入过程之前,我的代码工作正常。我确信我不了解程序是如何工作的。当我把它称为一个程序时,它确实通过SSH接收“连接被拒绝”并且像它应该的那样回退到telnet,它只是从不用密码登录。当我启用调试模式时,我看到它发送密码但由于某种原因路由器似乎不接收它。它只是位于密码:提示,直到它超时。
proc connectToTerminal { username hostname password } {
# Run ssh, don't display key warning
spawn ssh -o StrictHostKeyChecking=no $username\@$hostname
# Allow this script to handle ssh connection issues, fall back to telnet
expect {
timeout { send_user "\nTimeout Exceeded - Check Host\n"; exit 1 }
eof { send_user "\nSSH Connection To $hostname Failed\n"; exit 1 }
# If we have the correct prompt, continue
"*#" {}
# If it's asking for the password, it can have it
"*assword:" { send "$password\n" }
# Fall back to telnet if connection refused
"Connection refused" {
spawn telnet $hostname
#expect "username:"
#send "$username\n"
expect "*assword:"
send "$password\n"
}
# Fall back to telnet if connection is closed by [ip address]
-re {closed by \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}} {
spawn telnet $hostname
#expect "username:"
#send "$username\n"
expect "*assword:"
send "$password\n"
}
# Use SSH v1 if the device is only accepting v1
"2 vs. 1" {
spawn ssh -1 -o StrictHostKeyChecking=no $username\@$hostname
expect {
"*assword:" { send "$password\n" }
timeout {
spawn telnet $hostname
#expect "username:"
#send "$username\n"
expect "*assword:"
send "$password\n"
}
}
}
}
}
connectToTerminal $username $hostname $password
以下是运行脚本时的调试输出
spawn ssh -o StrictHostKeyChecking=no wmh@172.16.1.195
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {7726}
Gate keeper glob pattern for 'closed by \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}' is 'closed by *'. Activating booster.
expect: does "" (spawn_id exp6) match glob pattern "*#"? no
"*assword:"? no
"Connection refused"? no
"closed by \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}"? Gate "closed by *"? gate=no
"2 vs. 1"? no
ssh: connect to host 172.16.1.195 port 22: Connection refused
expect: does "ssh: connect to host 172.16.1.195 port 22: Connection refused\r\r\n" (spawn_id exp6) match glob pattern "*#"? no
"*assword:"? no
"Connection refused"? yes
expect: set expect_out(0,string) "Connection refused"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "ssh: connect to host 172.16.1.195 port 22: Connection refused"
spawn telnet 172.16.1.195
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {7730}
expect: does "" (spawn_id exp7) match glob pattern "*assword:"? no
Trying 172.16.1.195...
expect: does "Trying 172.16.1.195...\r\n" (spawn_id exp7) match glob pattern "*assword:"? no
Connected to 172.16.1.195.
expect: does "Trying 172.16.1.195...\r\nConnected to 172.16.1.195." (spawn_id exp7) match glob pattern "*assword:"? no
expect: does "Trying 172.16.1.195...\r\nConnected to 172.16.1.195.\r\n" (spawn_id exp7) match glob pattern "*assword:"? no
Escape character is '^]'.
expect: does "Trying 172.16.1.195...\r\nConnected to 172.16.1.195.\r\nEscape character is '^]'." (spawn_id exp7) match glob pattern "*assword:"? no
expect: does "Trying 172.16.1.195...\r\nConnected to 172.16.1.195.\r\nEscape character is '^]'.\r\n" (spawn_id exp7) match glob pattern "*assword:"? no
C
*********************************************************************
* *
* *
* *
* THIS COMPUTER IS FOR PRIVATE USE ONLY *
* ------------------------------------- *
* *
* UNAUTHORIZED access to and/or use of this computer syst
expect: does "Trying 172.16.1.195...\r\nConnected to 172.16.1.195.\r\nEscape character is '^]'.\r\nC\r\n*********************************************************************\r\n* *\r\n* *\r\n* *\r\n* THIS COMPUTER IS FOR PRIVATE USE ONLY *\r\n* ------------------------------------- *\r\n* *\r\n* UNAUTHORIZED access to and/or use of this computer syst" (spawn_id exp7) match glob pattern "*assword:"? no
em *
* is a violation of law and is punishable under provisions of *
* 19 USC 1029 and 18 USC 1030, and applicable statutes. Use *
* of this system constitutes consent to security testing and *
* monitoring. *
* *
* 11-12-13 *
*********************************************************************
User Access Verification
Password:
expect: does "Trying 172.16.1.195...\r\nConnected to 172.16.1.195.\r\nEscape character is '^]'.\r\nC\r\n*********************************************************************\r\n* *\r\n* *\r\n* *\r\n* THIS COMPUTER IS FOR PRIVATE USE ONLY *\r\n* ------------------------------------- *\r\n* *\r\n* UNAUTHORIZED access to and/or use of this computer system *\r\n* is a violation of law and is punishable under provisions of *\r\n* 19 USC 1029 and 18 USC 1030, and applicable statutes. Use *\r\n* of this system constitutes consent to security testing and *\r\n* monitoring. *\r\n* *\r\n* 11-12-13 *\r\n*********************************************************************\r\n\r\n\r\nUser Access Verification\r\n\r\nPassword: " (spawn_id exp7) match glob pattern "*assword:"? yes
expect: set expect_out(0,string) "Trying 172.16.1.195...\r\nConnected to 172.16.1.195.\r\nEscape character is '^]'.\r\nC\r\n*********************************************************************\r\n* *\r\n* *\r\n* *\r\n* THIS COMPUTER IS FOR PRIVATE USE ONLY *\r\n* ------------------------------------- *\r\n* *\r\n* UNAUTHORIZED access to and/or use of this computer system *\r\n* is a violation of law and is punishable under provisions of *\r\n* 19 USC 1029 and 18 USC 1030, and applicable statutes. Use *\r\n* of this system constitutes consent to security testing and *\r\n* monitoring. *\r\n* *\r\n* 11-12-13 *\r\n*********************************************************************\r\n\r\n\r\nUser Access Verification\r\n\r\nPassword:"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "Trying 172.16.1.195...\r\nConnected to 172.16.1.195.\r\nEscape character is '^]'.\r\nC\r\n*********************************************************************\r\n* *\r\n* *\r\n* *\r\n* THIS COMPUTER IS FOR PRIVATE USE ONLY *\r\n* ------------------------------------- *\r\n* *\r\n* UNAUTHORIZED access to and/or use of this computer system *\r\n* is a violation of law and is punishable under provisions of *\r\n* 19 USC 1029 and 18 USC 1030, and applicable statutes. Use *\r\n* of this system constitutes consent to security testing and *\r\n* monitoring. *\r\n* *\r\n* 11-12-13 *\r\n*********************************************************************\r\n\r\n\r\nUser Access Verification\r\n\r\nPassword:"
send: sending "cisco\n" to { exp7 }
expect: does "" (spawn_id exp0) match glob pattern "*#"? no
"*>"? no
expect: timed out
答案 0 :(得分:0)
发送密码后,只需添加一个expect
语句。
set prompt "#"
send "$password \r"
expect "$prompt"
除非我们明确告诉Expect
等待它,否则它不会期待会话中的任何内容。
更新:
我不确定您是否错过了expect
声明或在调用程序中错放了它。如果仍未解决,请更新您的问题。
如果您已将expect
语句放在另一个proc中,则应将spawn_id
传递给它,以便Expect
可以正常等待它。否则,如果spawn_id
为空,则将恢复默认行为。即期望来自stdin
。
从调试输出中可以看出
expect: does "" (spawn_id exp0) match glob pattern "*#"? no
其中exp0
只是stdin
。在该过程中,在生成telnet
之后,您可以将spawn_id
保存到另一个变量中并将其返回到可以使用它的调用者过程。