Expect script error send: Spawn id exp4 not open while executing

39,751

Solution 1

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

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

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

        spawn ssh -t root@$server_address "$*"  

The debug which you have seen as below.

 spawn ssh -t [email protected] sudo cat /etc/host
    [email protected]'s password:
    expect: does "[email protected]'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"

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.

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.

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

Solution 2

This is fixed after I got some info from https://serverfault.com/questions/642129/expect-script-error-send-spawn-id-exp4-not-open-while-executing

Thanks to https://serverfault.com/users/30957/glenn-jackman

Share:
39,751
Diplayed name
Author by

Diplayed name

Updated on October 16, 2020

Comments

  • Diplayed name
    Diplayed name over 3 years

    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
    

    And the output is like:

        goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/hosts"
        spawn ssh -t [email protected] sudo cat /etc/hosts
        [email protected]'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 [email protected] sudo cat /etc/hosts
        [email protected]'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 [email protected] sudo cat /etc/hosts
        [email protected]'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" }
    

    Output:

        goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/host"
        spawn ssh -t [email protected] sudo cat /etc/host
        [email protected]'s password:
        expect: does "[email protected]'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) "[email protected]'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.
        [email protected]'s password:
        expect: does " \r\nPermission denied, please try again.\r\r\[email protected]'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\[email protected]'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"