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

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

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

expect: couldn't execute "/usr/bin/zypper patch": no such file or directory

I wrote an expect script
vi /foo/force-zypper-patch.exp
cat /foo/force-zypper-patch.exp
#!/usr/bin/expect
set timeout 300
spawn "/usr/bin/zypper patch"
expect "Continue? [y/n/...? shows all options] (y): " { send "y\r" }
interact
but even though I have the zypper with full path, it fails:
SERVER1:~ # ssh SERVER2 "/usr/bin/expect -f /foo/force-zypper-patch.exp"
spawn /usr/bin/zypper patch
couldn't execute "/usr/bin/zypper patch": no such file or directory
while executing
"spawn "/usr/bin/zypper patch""
(file "/foo/force-zypper-patch.exp" line 4)
SERVER1:~ #
SERVER1:~ # ssh SERVER2 "which zypper"
/usr/bin/zypper
SERVER1:~ #
although I can reach my purpose with a "zypper patch -y --with-interactive" but I would be really interested, how to do this in expect!
Simply remove the quotes from the spawn line.
spawn /usr/bin/zypper patch
Currently, you are trying to execute the command zypper patch in the /usr/bin directory, and clearly no such file exists.

Run remote command from script

I need to run a shell script from within jenkins, to commit changes after making the build. Deploying build to remote server is not a problem, so the new build is there. All I need to do is just commit it.
For that I need to login with ssh to that remote server using shell script, and so far it is okay:
#!/user/bin/expect -f
spawn ssh myusername#url
expect "password:"
send "mypassword\r"
interact
So now when I am logged in, I want to run a few commands: cd /path/to/repository; svn commit -m "Some change log"
I tried something like:
#!/user/bin/expect -f
spawn ssh -o "LocalCommand cd /path/to/repository" myusername#url
expect "password:"
send "mypassword\r"
But it just don't work, as I have no idea how to do it.
If anyone know how to do it, please let me know.
The remote server is running on linux, and the jenkins on osx.
I found solution just with expect:
#!/user/bin/expect -f
spawn ssh myusername#url
expect "password:"
send "mypassword\r"
expect "some server prompt"
send "cd /path/to/repository\r"
send "svn commit -m 'Some change log'\r"
EDIT:
This solution seems to work from time to time only. I mean commiting changes.

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