gpt4 book ai didi

linux - 期望脚本错误发送 : Spawn id exp4 not open while executing

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:21 25 4
gpt4 key购买 nike

我正在尝试运行此脚本,但在修改时出现不同的错误。这是代码和输出。请帮忙。

在文章末尾更新调试信息

    #!/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"

最佳答案

根据您的代码,在为 ssh session 提供密码后,ssh 连接似乎关闭了。

每当使用 spawn 命令生成新进程时,expect 就会将该期望进程的 spawn_id 保存到 expect_out(spawn_id )

按照你的代码,expect的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 持有相同的引用,expect 将尝试从该进程中进行 expect 并失败。

可以引用this了解此 spawn_id 如何与标准输入(从控制台读取输入)一起使用的问题

关于linux - 期望脚本错误发送 : Spawn id exp4 not open while executing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26760913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com