Expect script for copying file - linux

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

Related

How to list a directory with expect via sftp connection

I have this expect script but the list of files does not present me correctly.
expect.dat
set timeout 10000
spawn sftp -o ConnectTimeout=3 user#192.1.6.6
expect "*?assword*"
send "tech\r"
expect "sftp>"
send "cd /global/scripts/log\r"
expect "sftp>"
send "ls 20220703*\r"
expect "sftp>"
send "bye\r"
Expect command
expect expect.dat
Result
spawn sftp -o ConnectTimeout=3 user#192.1.6.6
Password:
Connected to user#192.1.6.6.
sftp> cd /global/scripts/log
sftp> ls 20220703*
20220703_A.log 20220703_A.xls
20220703_E.log 20220703_E_r.log
sftp> You have new mail in /var/spool/mail/dm
how to get this result?
20220703_A.log
20220703_A.xls
20220703_E.log
20220703_E_r.log
First, use the sftp command ls -1 20220703* to produce output in a single column.
Next, use log_user 0 and spawn -noecho sftp ... to hide all the excess output.
Last, and the trickiest part, capture the ls output so that can be printed out cleanly: we have to handle the fact that the command we just "typed" will be given back to us.
set ls_cmd "ls -1 20220703*"
send "$ls_cmd\r"
# capture the command's output
expect -re "$ls_cmd\r\n(.*)\r\nsftp>"
puts $expect_out(1,string)
send "bye\r"
expect eof # wait for sftp to exit cleanly
expect gives you the process's output using \r\n line endings.

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

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

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

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.

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