Solution not found for > send: spawn id exp6 not open while executing - cron

I am using the expect script to set up vpn and this script is working fine normally when executing it manually but when setting up it with cron job, it throws following error:
send: spawn id exp6 not open
while executing
"send "$syspass\r""
I tried my best to solve it but can't find any solution.I tried many solutions related it in the questions asked at Stack Overflow but none of it solved my problem.
Here is the code:
#!/usr/bin/expect --
set username "*****"
set password "*****"
set syspass "******"
spawn sudo killall openvpn
expect ".*for"
send "$syspass\r"
interact
spawn ./hma-vpn.sh -p tcp "UK"
expect ".*for"
send "$syspass\r"
expect "?sername:"
send "$username\r"
expect "?assword:"
send "$password\r"
expect ":~"
send "echo 'connected'\r"
interact

Related

sftp using expect to automate file transfer not working

I want automate below command so as to pass the password and proceed with file transfer.
#This command works well but it will require password
echo "put This_file_from_server_a.csv /TO/THIS/SERVER_B/PATH" | sftp remote#10.11.12.13
i have tried to use expect, so as to automate/ send password automatically, but it is not working i.e.
expect -c 'spawn "put This_file_from_server_a.csv /TO/THIS/SERVER_B/PATH" | sftp remote#10.11.12.13; expect "assword:"; send "THE_PASSWORD\r"; interact'
I get error
send: spawn id exp4 not open
while executing "send "THE_PASSWORD\r""
What could be the issue? without considering alternatives: such as sshpass, lftp, private keys...
Consider creating private keys on both servers that you are trying to do the transaction then try doing the same, it should work
in case it does not use expect as below.
expect <<'END_EXPECT'
set timeout -1
spawn sftp remote#10.11.12.13
expect "assword:"
send "THE_PASSWORD\r"
expect "sftp>"
send "put This_file_from_server_a.csv /TO/THIS/SERVER_B/PATH\r"
expect "sftp>"
send "quit\r"
expect eof
END_EXPECT

Notify via email of a failed script using expect interpreter

I am trying to have an email sent when any part of the script fails. This is the script:
#!/usr/bin/expect
spawn sftp -oPort=9999 ser_s_nm#mtg-fn-serv01
expect "password:"
send "password123\n"
expect "sftp>"
send "put /folder1/folder2/report.zip\n"
expect "sftp>"
send "exit\n"
interact
I tried using if statements and exit codes to accomplish this, but the expect interpreter does not recognize $? as a command. How can I do this with commands that will be recognized?

Unix - Expect Not Working While Executing Commands

I am trying to execute commands on a remote UNIX host using send and expect ssh module, but even if the script logs in to the server successfully it does not execute commands.
#!/usr/bin/expect
set timeout 60
spawn ssh xxxx#xxxxxx
expect "yes/no" {
send "yes\r"
expect "*?assword" { send "xxxxxx\r" }
} "*?assword" { send "xxxxxxx\r" }
expect "$ "
#sleep 5
send "ps -aef \r"
Output
[xxxxx#xxxxxx Scripts]$ ./TestExpect.sh
spawn ssh xxxxx#xxxxxx
xxxxxx#xxxxxx's password:
Last login: Wed May 9 02:05:47 2018 from xxxxxxxxx
Kickstarted on 2015-05-12
[xxxxx#xxxxx ~]$ [xxxxxx#xxxxx Scripts]$
The Prompt looks like below
[aacdd123#linprod345 ~]$
Issue may be because, you are not expecting anything after sending the ps -aef. Hence the expect spawn process has exited before printing the output.
Try adding few more commands after the sending ps -aef
send "ps -aef\r"
expect $prompt
send "echo hello\r"
expect $prompt
Try looking into the expect_out buffers too, which will give you the captured streams.
puts $expect_out(buffer)

send error while running expect code

I made an expect code:
#!/usr/bin/expect -f
spawn ls
expect "*]$*"
send "cd /to/some/path\n"
expect "*]$*"
send "sudo -u root ./program.sh"
expect "*:*"
send "i_am_password\n"
interact
while executing it I am getting the below error:
spawn ls
my_pgm.exp abc.sh axyz.zip all.zip test.exp
send: spawn id exp6 not open
while executing
"send "cd /to/some/path\n""
(file "./my_pgm.exp" line 5)
I am running this code on ubuntu. Please help.
I'm not sure to really understand why you need expect, but for a bash script, try this:
#!/usr/bin/expect -f
spawn bash -i
expect "*$ "
send "cd /to/some/path\n"
expect "*$ "
send "sudo -u root ./program.sh\n"
expect "*: "
send "i_am_password\n"
interact
Description
The spawn directive instruct expect which program are to be used to interact with. So if you want to interact with bash, you have to ask expectto spawn bash. The -i parameter to bash enforce them to start in interactive mode.
At all, this look like a very bad idea, but that's only my opinion ;)

How to automate telnet session using Expect?

I'm trying to write an expect script to automate telnet. This is what I have so far.
#!/usr/bin/expect
# Test expect script to telnet.
spawn telnet 10.62.136.252
expect "foobox login:"
send "foo1\r"
expect "Password:"
send "foo2\r"
send "echo HELLO WORLD\r"
# end of expect script.
Basically, what I want to do is telnet to the following IP address and then echo HELLO WORLD. However, it seems that the script fails after attempting to telnet...I'm not sure if it's able to accept login and password input, but it is not echoing HELLO WORLD. Instead, I just get this output:
cheungj#sfgpws30:~/justin> ./hpuxrama
spawn telnet 10.62.136.252
Trying 10.62.136.252...
Connected to 10.62.136.252.
Escape character is '^]'.
Welcome to openSUSE 11.1 - Kernel 2.6.27.7-9-pae (7).
foobox login: foo1
Password: foo2~/justin>
It's hard to tell, but from the output you're pasting it looks like:
Your script isn't waiting for login to complete before sending the next command.
Your script is exiting and closing the process before you can see any output.
There are no guarantees in life, but I'd try this as a first step:
#!/usr/bin/expect -f
spawn telnet 10.62.136.252
expect "foobox login:"
send "foo1\r"
expect "Password:"
send "foo2\r"
# Wait for a prompt. Adjust as needed to match the expected prompt.
expect "justin>"
send "echo HELLO WORLD\r"
# Wait 5 seconds before exiting script and closing all processes.
sleep 5
Alternatives
If you can't get your script to work by manually programming it, try the autoexpect script that comes with Expect. You can perform your commands manually, and autoexpect will generate an Expect typescript based on those commands, which you can then edit as needed.
It's a good way to find out what Expect actually sees, especially in cases where the problem is hard to pin down. It's saves me a lot of debugging time over the years, and is definitely worth a try if the solution above doesn't work for you.
You're sending the echo command without first expecting the prompt. Try:
# after sending the password
expect -re "> ?$"
send "echo HELLO WORLD\r"
expect eof
Have you seen this StackOverflow Question?
He seems to have got things working by using curly braces.
Here is a simplified version
#!/usr/bin/expect
# just do a chmod 755 one the script
# ./YOUR_SCRIPT_NAME.sh $YOUHOST $PORT
# if you get "Escape character is '^]'" as the output it means got connected otherwise it has failed
set ip [lindex $argv 0]
set port [lindex $argv 1]
set timeout 5
spawn telnet $ip $port
expect "'^]'."

Resources