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

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 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" }
Output:
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"

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 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"
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)

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

Related

How to automate ctrl+d action in the ssh expect script in bash?

I have a usecase where I need to execute a command after connecting to the host through ssh. After the command execution, I need to perform Ctrl-D and Ctrl-M so that I can issue other commands.
I tried with using EOF but It is completely closing the session.
expect << EOF
spawn ssh -o StrictHostKeyChecking=no LocalCOMUser#$nodeIp -p $ssh_port
expect {
"password:" {}
timeout { send_user "Timed out in ssh connection" ;exit 1}
}
send "p#ssword\r"
expect {
">" {}
timeout { send_user "Timed out in ssh connection" ;exit 1}
}
set timeout 120
send "mml\r"
expect {
"<" {}
timeout { send_user "Timed out in ssh connection" ;exit 1}
}
send "$command1\r"
expect {
"<" {}
}
send "exit;\r"
expect {
">" {}
timeout { send_user "Timed out in ssh connection" ;exit 1}
}
send "exit\r"
EOF
Need a command that does action as Ctrl-d.
You can simulate pressing Ctrl-D by sending the ^D/␄ character:
send "\x04"

Blocked when i run this shell script

#!/bin/bash
SERVERS="master slave1 slave2 slave3"
for SERVER in $SERVERS
do
expect -c "set timeout -1;
spawn ssh hadoop#$SERVER;
expect {
*(yes/no)* {send -- yes\r;exp_continue}
*assword:* {send -- hadoop\r;exp_continue}
*$* {send -- \"echo 'ssss'\r\";exp_continue}
*$* {send -- exit\r;exp_continue}
eof {exit;0}
}";
done
when i run this script,it's will be block and killed.
‘SERVER’ is hostname list;
Please help me see why,what's wrong with my code.
ERROR MESSAGE AS BELOW:
spawn ssh hadoop#master
./gather_pub_key.sh: line 5: 2695 killed expect -c "set timeout -1;
spawn ssh hadoop#$SERVER;
expect {
*(yes/no)* {send -- yes\r;exp_continue}
*assword:* {send -- hadoop\r;exp_continue}
*$* {send -- \"echo 'ssss'\r\";exp_continue}
*$* {send -- exit\r;exp_continue}
eof {exit;0}
}"

Execute sudo using expect inside ssh from bash

I want to create a script that automates a installation on multiple linux hosts.
I login to the hosts using ssh keys and inside the login I want to do a sudo, I am trying to use expect, which I have on the stations but I don't have on the server which runs the script.
How do I do this, this is my try, but no luck with it:
#!/bin/bash
ssh user#station04 <<EOF
expect -d -c "
send \"sudo ls\"
expect {
\"password:\" { send '1234'; exp_continue }
\"$user#\"{
send "exit\r"
}
default {exit 1}
}"
EOF
exit
The result:
send: sending "sudo ls" to { exp0 }
expect: does "" (spawn_id exp0) match glob pattern "password:"? no
expect: read eof
expect: set expect_out(spawn_id) "exp0"
expect: set expect_out(buffer) ""
argv[0] = expect argv[1] = -d argv[2] = -c argv[3] =
send "sudo ls\r"
expect {
"password:" { send '1234'; exp_continue }
"#"{
send exitr
}
default {exit 1}
}
set argc 0
set argv0 "expect"
set argv ""
A.K
What about this? <- just make sure of the expected prompts.
#!/bin/bash
expect <<'END'
spawn ssh user#station04
expect "password:"
send "$pw\r"
expect "#"
send "sudo ls\r"
END
I suggest you would use public key authentication for the ssh part, then just use something like:
ssh -t username#server-ip -C "echo sudo-password | /usr/bin/sudo -S ls"
You got the usage of expect not quite right - don't send a command; rather spawn the command and send just its input. So, your script becomes:
ssh … <<EOF
expect -d -c "
spawn sudo ls
expect -nocase password: { send 1234\r }
expect eof
"
exit
EOF

Is it possible to set exit code of 'expect'

The following bash script doesn't work because command 'expect' always return 0 regardless which exit code of the remote script /tmp/my.sh returns.
any idea to make it work? thanks.
#!/usr/bash
user=root
passwd=123456abcd
host=10.58.33.21
expect -c "
spawn ssh -o StrictHostKeyChecking=no -l $user $host bash -x /tmp/my.sh
expect {
\"assword:\" {send \"$passwd\r\"}
eof {exit $?}
}
"
case "$?" in
0) echo "Password successfully changed on $host by $user" ;;
1) echo "Failure, password unchanged" ;;
2) echo "Failure, new and old passwords are too similar" ;;
3) echo "Failure, password must be longer" ;;
*) echo "Password failed to change on $host" ;;
esac
Edited at 10:23 AM 11/27/2013
Thanks for the comments. Let me emphasis the problem once again,
The main script is supposed to run on linux server A silently, during which it invokes another script my.sh on server B unattended. The question is how to get exit code of my.sh?
That's why I cannot leverage ssl_key approach in my case, which requires at least one time configuration.
#!/usr/bin/expect
set user root
set passwd 123456abcd
set host 10.58.33.21
set result_code 255
# exp_internal 1 to see internal processing
exp_internal 0
spawn ssh -o StrictHostKeyChecking=no -l $user $host bash -x /tmp/my.sh && echo aaa0bbb || echo aaa$?bbb
expect {
"assword:" {send "$passwd\r"; exp_continue}
-re "aaa(.*)bbb" {set result_code $expect_out(1,string)}
eof {}
timeout {set result_code -1}
}
switch $result_code {
0 { puts "Password successfully changed on $host by $user" }
1 { puts "Failure, password unchanged" }
2 { puts "Failure, new and old passwords are too similar" }
3 { puts "Failure, password must be longer" }
-1 { puts "Failure, timeout" }
default { puts "Password failed to change on $host" }
}
exit $result_code

Transfer environment variables with expect

I am writing an expect script and need to transfer environment variables over a telnet session (which the man page proudly touts as a feature but provides no other mention).
So something like this:
#!/usr/bin/expect -c
spawn telnet 1.2.3.4
set rpath ""
expect "#" { set rpath $PATH }
where $PATH is in the environment of the remote system..any ideas?
You could easily do this by spawning bash and then issue telnet to the remote system.
I presume you want to set the path variable from the local machine to the remote machine.
#!/bin/sh
# the next line restarts using tclsh \
exec expect "$0" "$#"
set prompt "~$"
set hostname "anyhost"
spawn bash
send "echo $PATH\r"
expect {
$prompt {
set pathVariable $expect_out(buffer)
}
timeout {
send_user "path hasn't been set - exiting\n"
close
exit 1
}
}
send "telnet $hostname\r"
expect {
"Login:" {}
"telnet: " {
send_error "$argv0 couldn't login to $hostname\n"
exit 1;
}
timeout {
send_error "$argv0 couldn't login to $hostname, timeout of $timeout passed\n"
exit 1;
}
}
send "$username\r"
expect "Password:"
send "$password\r"
expect $remotePrompt
send "bash\r"
send "export PATH=$pathVariable\r"
# continue with whatever you want.

Resources