Tcl SSH登录的预期过程
我是个新来的脚本编写者。我正在尝试创建一个过程,该过程将通过SSH连接到路由器/交换机,如果失败,则返回到telnet。在我将代码放入过程之前,它可以正常工作。我确信关于程序是如何工作的,有些事情我还不了解。当我把它作为一个过程调用时,它确实是通过SSH连接的,SSH接收到“连接被拒绝”,并像预期的那样返回到telnet,它只是从不使用密码登录。当我启用调试模式时,我看到它发送密码,但由于某种原因,路由器似乎没有收到密码。它只是停留在Password:提示符处,直到超时Tcl SSH登录的预期过程,tcl,expect,Tcl,Expect,我是个新来的脚本编写者。我正在尝试创建一个过程,该过程将通过SSH连接到路由器/交换机,如果失败,则返回到telnet。在我将代码放入过程之前,它可以正常工作。我确信关于程序是如何工作的,有些事情我还不了解。当我把它作为一个过程调用时,它确实是通过SSH连接的,SSH接收到“连接被拒绝”,并像预期的那样返回到telnet,它只是从不使用密码登录。当我启用调试模式时,我看到它发送密码,但由于某种原因,路由器似乎没有收到密码。它只是停留在Password:提示符处,直到超时 proc connect
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
发送密码后,只需再添加一条
expect
语句
set prompt "#"
send "$password \r"
expect "$prompt"
除非我们明确告诉Expect
等待它,否则它不会从会话中得到任何东西
更新:
我不确定您是否错过了expect
语句,或者在调用程序中将其放错了位置。如果问题仍未解决,请更新您的问题
如果已将expect
语句放置在另一个进程中,则应将spawn_id
传递给它,以便expect
可以正确地等待它。否则,如果spawn\u id
为空,将恢复默认行为。i、 e.它将从stdin
中获得
从调试输出中可以明显看出这一点
expect: does "" (spawn_id exp0) match glob pattern "*#"? no
其中
exp0
只不过是stdin
。在该过程中,在生成telnet
后,您可以将spawn\u id
保存到另一个变量中,并将其返回给调用方过程,以便使用 发送密码后,只需再添加一条expect
语句
set prompt "#"
send "$password \r"
expect "$prompt"
除非我们明确告诉Expect
等待它,否则它不会从会话中得到任何东西
更新:
我不确定您是否错过了expect
语句,或者在调用程序中将其放错了位置。如果问题仍未解决,请更新您的问题
如果已将expect
语句放置在另一个进程中,则应将spawn_id
传递给它,以便expect
可以正确地等待它。否则,如果spawn\u id
为空,将恢复默认行为。i、 e.它将从stdin
中获得
从调试输出中可以明显看出这一点
expect: does "" (spawn_id exp0) match glob pattern "*#"? no
其中
exp0
只不过是stdin
。在该过程中,在生成telnet
后,您可以将spawn\u id
保存到另一个变量中,并将其返回给调用方过程,以便使用 几天前我在使用Expect,记得看到手册页面上说,当您想发送
时,您应该使用\r
而不是\n
。也许这能解决问题?我试过了,但没什么不同。谢谢你的建议。几天前我正在使用Expect,记得看到手册页面上说,当你想发送
时,你应该使用\r
而不是\n
。也许这能解决问题?我试过了,但没什么不同。不过,谢谢你的建议。这无法解释为什么代码在放入过程之前运行良好。我想OP只是粘贴了脚本的一部分。@whjm:我希望我现在就解决了。如果只有一个程序要spawn
ed,那么我们可以在proc
的开头添加global spawn\u id
,使其成为一个全局变量。这无法解释为什么代码在放入过程之前工作良好。我想OP只是粘贴了脚本的一部分。@whjm:我希望我现在就解决了。如果只有一个程序要spawn
ed,那么我们可以在proc
的开头添加global spawn\u id
,使其成为一个全局变量。