预期发送脚本错误:执行期间未打开Spawn ID exp4 [英] Expect script error send: Spawn id exp4 not open while executing

查看:360
本文介绍了预期发送脚本错误:执行期间未打开Spawn ID exp4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to run this script but having different errors when modified. Here is the code and the output. Please help.

在帖子末尾使用调试信息进行更新

Updates at the end of the post with debug info

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

如果我这样修改,那么输出将有所不同

If I modify like this, then the output would be bit different

    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"

如果在第三行输入了正确的密码,则完全没有错误.在这个上工作正常.

And if the correct password in on the third line then no errors at all. Works fine on this one.

    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.

更新:调试信息-修改为

Update: Debug info - Modified to

    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会话提供密码的几步之后,看来ssh连接已关闭.

As per your code, it looks like the ssh connection got closed after the few trails of giving the passwords to ssh session.

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

Whenever a new process spawned with spawn command, then expect will save the spawn_id for that expect process into expect_out(spawn_id).

根据您的代码,exp的spawn_id在遇到

As per your code, expect's spawn_id is generated when it encounters

        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,您必须从中期望spawn_id的值.

As you can see in the debug information, the expect_out(spawn_id) holds the spawn_id from which it has to expect for values which is exp4 in your case.

如您所见,连接经过几次错误跟踪后就关闭了,从而使进程exp4在上下文中不再退出.由于spawn_id保持对它的引用,expect将尝试从该过程中获得期望,但失败了.

As you can see, the connection got closed after few wrong trails thereby making the process exp4 no longer exits in the context. Since the spawn_id holds the reference to the same, expect will try to expect from that process and failed.

您可以参考此问题,以了解此spawn_id如何与标准输入(正在读取输入)一起使用从控制台)

You can refer this question to know about how this spawn_id being used with standard input (which is reading input from console)

这篇关于预期发送脚本错误:执行期间未打开Spawn ID exp4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆