我试图运行此脚本,但在修改时遇到了不同的错误。这是代码和输出。请帮忙。
使用调试信息
在帖子末尾进行更新 #!/bin/bash
(( $# != 1 )) && { echo >&2 "Usage: $0 \"[COMMAND]\""; exit 1; }
servers_addresses=(10.10.10.10 )
for server_address in ${servers_addresses[@]}; do
expect <<EOF
spawn ssh -t root@$server_address "$*"
expect -timeout 2 "Are you sure you want to continue connecting (yes/no)?" { send "yes\n" }
expect "s password:" { send "Correct_Password\n" }
expect "s password:" { send "Wrong_Password_22222\n" }
expect "s password:" { send "Wrong_Password_33333\n" }
expect eof
EOF
done
输出如下:
goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/hosts"
spawn ssh -t root@10.10.10.10 sudo cat /etc/hosts
root@10.10.10.10's password:
# Do not remove the following line, or various programs
# that require network functionality will fail.
10.10.10.10 TEST-004 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
Connection to 10.10.10.10 closed.
expect: spawn id exp4 not open
while executing
"expect "s password:" { send "Wrong_Password_33333\n" }"
如果我像这样修改,那么输出会有点不同
expect "s password:" { send "Wrong_Password_11111\n" }
expect "s password:" { send "Correct_Password\n" }
expect "s password:" { send "Wrong_Password_33333\n" }
goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/hosts"
spawn ssh -t root@10.10.10.10 sudo cat /etc/hosts
root@10.10.10.10's password:
# Do not remove the following line, or various programs
# that require network functionality will fail.
10.10.10.10 TEST-004 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
Connection to 10.10.10.10 closed.
expect: spawn id exp4 not open
while executing
"expect eof"
如果第三行中的密码正确,则根本没有错误。这个工作正常。
expect "s password:" { send "Wrong_Password_11111\n" }
expect "s password:" { send "Wrong_Password_22222\n" }
expect "s password:" { send "Correct_Password\n" }
goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/hosts"
spawn ssh -t root@10.10.10.10 sudo cat /etc/hosts
root@10.10.10.10's password:
# Do not remove the following line, or various programs
# that require network functionality will fail.
10.10.10.10 TEST-004 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
Connection to 10.10.10.10 closed.
更新:调试信息 - 修改为
exp_internal 1
expect "s password:" { send "Wrong_Password_11111\n" }
expect "s password:" { send "Correct_Password\n" }
expect "s password:" { send "Wrong_Password_33333\n" }
输出:
goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/host"
spawn ssh -t root@10.10.10.10 sudo cat /etc/host
root@10.10.10.10's password:
expect: does "root@10.10.10.10's password: " (spawn_id exp4) match glob pattern "s password:"? yes
expect: set expect_out(0,string) "s password:"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "root@10.10.10.10's password:"
send: sending "Wrong_Password_11111\n" to { exp4 }
expect: does " " (spawn_id exp4) match glob pattern "s password:"? no
expect: does " \r\n" (spawn_id exp4) match glob pattern "s password:"? no
Permission denied, please try again.
root@10.10.10.10's password:
expect: does " \r\nPermission denied, please try again.\r\r\nroot@10.10.10.10's password: " (spawn_id exp4) match glob pattern "s password:"? yes
expect: set expect_out(0,string) "s password:"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " \r\nPermission denied, please try again.\r\r\nroot@10.10.10.10's password:"
send: sending "Correct_Password\n" to { exp4 }
expect: does " " (spawn_id exp4) match glob pattern "s password:"? no
expect: does " \r\n" (spawn_id exp4) match glob pattern "s password:"? no
cat: /etc/host: No such file or directory
Connection to 10.10.10.10 closed.
expect: does " \r\ncat: /etc/host: No such file or directory\r\r\nConnection to 10.10.10.10 closed.\r\r\n" (spawn_id exp4) match glob pattern "s password:"? no
expect: read eof
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " \r\ncat: /etc/host: No such file or directory\r\r\nConnection to 10.10.10.10 closed.\r\r\n"
expect: spawn id exp4 not open
while executing
"expect eof"
答案 0 :(得分:1)
根据您的代码,在将密码提供给ssh会话的几条路径之后,ssh
连接似乎已关闭。
每当使用spawn
命令生成新流程时,expect
会将该期望流程的spawn_id
保存到expect_out(spawn_id)
。
根据您的代码,期望在遇到
时生成spawn_id spawn ssh -t root@$server_address "$*"
您在下面看到的调试。
spawn ssh -t root@10.10.10.10 sudo cat /etc/host
root@10.10.10.10's password:
expect: does "root@10.10.10.10's password: " (spawn_id exp4) match glob pattern "s password:"? yes
expect: set expect_out(0,string) "s password:"
expect: set expect_out(spawn_id) "exp4"
正如您在调试信息中看到的那样,expect_out(spawn_id)
保留了spawn_id
,在您的案例中,exp4
保留了exp4
的值。
正如您所看到的,连接在几个错误的路径之后关闭,从而使进程spawn_id
不再在上下文中退出。由于spawn_id
保持对该引用的引用,因此期望将尝试期望该进程并且失败。
您可以参考this问题来了解这个{{1}}如何与标准输入一起使用(从控制台读取输入)
答案 1 :(得分:0)