Expect script error send: spawn id exp4 not open while executing "send "password"" - linux

I'm trying to run an expect script but I have an error:
send: spawn id exp4 not open
while executing
"send "password"
My script is very simple:
#!/usr/bin/expect -f
#!/usr/bin/env expect
#!/usr/bin/env export
#!/bin/sh
#!/bin/bash
spawn sftp -o IdentityFile=/home/localUser/MyPrivKeys_open.ppk user#123.123.123.123
expect "sftp.gatewayEnter passphrase for key '/home2/localUser/MyPrivKeys_open.ppk':"
send "passphrase\r"
expect "sftp>"
send "cd /home/localUser/localPath \r"
expect "sftp>"
send "mget myfile.xml /home/localUser/localPath \r"
set timeout 5
interact
What I do is editing my expect script in notepad.exe but when i upload my script i have to run next commands:
dos2unix bash.sh -> converts dos file to a Unix file
chmod +x bash.sh -> gives execution permission
And finally i run my script:
./bash.sh
But after that it sends that error.
It is something really weird because i have another .sh file with the same structure and i am only changing paths where i get files
I think i need some help about it.
Thanks.

I have already fix it, it was an error in dos2unix command. I had to code it again over Ubuntu for avoiding to run dos2unix command.

Related

Expect script for copying file

I am trying to copy all files but using expect command
I have the following script with expect.dat file
I have the following error, what am I doing wrong or how do I have to build the command to copy the files
command
expect expect.dat
File expect.dat
#!/usr/bin/expect
set src_file "/process/source"
set dest_file "/process/destination"
# Run the cp command and wait for the response
spawn cp -r $src_file $dest_file
expect "*?(y/n)?"
send "y\r"
expect eof
Error
spawn cp -r /process/source /process/destination
send: spawn id exp6 not open
while executing
"send "y\r""
(file "expect.dat" line 8)
"spawn id not open" means the cp command has already exited so there's no process to send something to.
If the cp command might ask you a question, you'll want to expect either the pattern or eof
expect {
"*?(y/n)?" {send "y\r"; exp_continue}
eof
}
Are you sure you need expect for this at all?
cp -r -f /process/source /process/destination

sftp via bash script in a cronjob: 'get' and 'rm' commands not found?

I'm trying to do something real, real simple: download a file from a sftp site at a set time. However, I'm getting errors like 'command: rm does not exist' or 'command get does not exist' which is garbage, because these commands obviously exist.
#!/usr/bin/expect
#remove current version of file
rm -f /home/user/downloads/data/newdata.zip
spawn sftp user#ftpsite.com
expect "password:"
send "PaSsWoRd\n"
expect "sftp>"
get /mycompany/myproject/data/newdata.zip /home/user/downloads/data
expect "sftp>"
send "exit\n"
interact
(base) root#ubuntu:~# ./shellscript.sh
invalid command name "rm"
while executing
"rm -f /home/user/downloads/data/newdata.zip"
or without the removal
(base) root#ubuntu:~# sudo ./shellscript.sh
spawn sftp user#sftpsite.com
EFT Server Login - %DATE% %TIME% - Please enter valid credentials to continueEnter password:
Connected to ftpsite.com
sftp> invalid command name "get"
while executing
"get /mycompany/myproject/data/newdata.zip /home/user/downloads/data"
What gives? Why can't I get real error messages - I'm sure get and rm a perfectly fine commands and I can verify this by running them directly from the command line, where they work fine.
You are writing TCL code in an Expect script. You are not writing Bash in a shell script.
You have to send the commands you want to write to the sftp tool:
send "get /mycompany/myproject/data/newdata.zip /home/user/downloads/data\n"
And exec any commands you want to run non-interactively:
exec rm -f /home/user/downloads/data/newdata.zip

Two commands or command pipe command - Spawn Expect

I'm trying to execute a expect script into bash script, but when I try to execute the shell without "| tee -a xxx.log" (where xxx.log is a file than I want to write to record the ssh session).
This code works fine:
comlog="ssh $USR#192.168.228.20"
expect -c"
spawn \"$comlog\"
expect \"Password:\"
send \"$PASS\r\"
interact
"
But when I try to add the "tee -a" command to save the ssh session the issue is
invalid command name "ssh"
while executing
This is the complete command where I obtain the error message
comlog="ssh $USR#192.168.228.20 | tee -a /home/xxx.log"
expect -c"
spawn \"$comlog\"
expect \"Password:\"
send \"$PASS\r\" #Already Obteined
interact
"
I tried to change the "comlog" var as this ways but doesn't work :(
cssh $USR#192.168.228.20 \| tee -a /home/xxx.log
Does anyone know another way to save the ssh session started from expect? Or how can I send those two commands in same spawn command.
You can capture the expect session output this way:
comlog="ssh $USR#192.168.228.20"
expect -c"
spawn \"$comlog\"
expect \"Password:\"
send \"$PASS\r\" #Already Obteined
interact
" >/home/xxx.log
If you want to put shell metacharacters like the pipe, you'll have to spawn a shell to handle them. Also using a here-doc can help a lot with quoting
comlog="ssh $USR#192.168.228.20 | tee -a /home/xxx.log"
expect <<"END_EXPECT"
spawn sh -c "$comlog"
... rest of expect script
END_EXPECT

Why spawn scp does not work correctly?

Below is my script containing spawn scp related script.
spawn scp -r /usr/local/clo/Jenkins.zip root#xu.domain.com:/usr/local/clo
expect "password:"
send "111111\r"
expect "*\r"
expect "\r"
But when I execute it, it output the following log and it can not transfer the file correctly.
**
spawn scp -r /usr/local/clo/Jenkins.zip root#xu.domain.com:/usr/local/clo
root#xu.domain.com's password:
Jenkins.zip 0% 0 0.0KB/s --:-- ETA
**
I was wondering how does the '0%' happen? If I manually execute the scp script from console, it can send the file completely.
I suspect this happens because the output of the spawned process includes \r (carriage return) characters when displaying the progress meter, try to add expect eof at the end of your Expect script.
But to deal with timeout problems, it should be better to detect the progress meter, using exp_continue to continue executing while the progress meter is displayed:
spawn scp -r /usr/local/clo/Jenkins.zip root#xu.domain.com:/usr/local/clo
expect "password:"
send "111111\r"
expect {
"ETA" {exp_continue}
"100%" {}
}
expect eof
You could also add exp_internal 1 at the beginning of the script (it reports internal activity of the expect command).

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

Resources