Notify via email of a failed script using expect interpreter - linux

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?

Related

EOF in expect script spawning sftp

I have a script that is transferring files from a linux server to a windows server. I want to log the data related to the transfers but EOF is giving me error in the HEREDOC construct. Can anyone show me the way forward for this.
My script is:
#!/usr/bin/expect
spawn sftp XXXX#XXXXXX <<EOF>> log.file
expect "password:"
send "ABC\n"
expect "sftp>"
send "cd /FIRST\r"
expect "sftp>"
send "lcd /home\r"
expect "sftp>"
send "mput /home/*First*\r"
send "bye\r"
interact
Or
#!/usr/bin/expect
log_file -a log.file
spawn sftp XXXX#XXXXXX
# ... the rest is all the same.
If you're not actually interacting (as a human) with the sftp process, you could use this as the last line
expect eof
Use a shell script instead and call expect passing to it "-" to make it read from its standard input which will be the HEREDOC (i.e. <<EOF ... EOF):
#!/bin/sh
/usr/bin/expect - <<EOF >> /tmp/log
spawn sftp XXXX#XXXXXX
expect "password:"
send "ABC\n"
expect "sftp>"
send "cd /FIRST\r"
expect "sftp>"
send "lcd /home\r"
expect "sftp>"
send "mput /home/*First*\r"
send "bye\r"
EOF

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

Unable to mput all the files through sftp in the remote server using expect

I'm trying to mput all files present in the directory : /Test/XML/ into a remote sftp server with the help of expect utility.
I've around 320 files in the directory: /Test/XML/.
The size of each file is around 0.1 MB.
There's no error observed.
Here's my code:
cd /Test/XML/
/usr/bin/expect <<EOF
spawn /usr/bin/sftp ${user_name}#${HOSTNAME}
expect "password:"
send "${passwd}\r"
expect "sftp>"
send "cd /test\r"
expect "sftp>"
send "mkdir XML\r"
expect "sftp>"
send "cd /test/XML\r"
expect "sftp>"
send "mput *\r"
expect "sftp>"
send "bye\r"
EOF
But the problem here is, mput * is transferring only 4 files instead of transferring all the 320 files.
Not sure, why it's not able to transfer all the 320 files in the remote server.
Any help is most welcome.
Thanks #ThiruShetty for the hint of using set timeout -1 in the expect utility.
Actually, i had a lot of files(~320-350) to be transferred(sftp) to a remote server.
With the normal execution of sftp using expect utility, it was able to transfer only a few files, not all of them which i wanted.
After inserting set timeout -1 inside expect, it solved the problem of timeout.
Here's the final code:
cd /Test/XML/
/usr/bin/expect <<EOF
set timeout -1
spawn /usr/bin/sftp ${user_name}#${HOSTNAME}
expect "password:"
send "${passwd}\r"
expect "sftp>"
send "cd /test\r"
expect "sftp>"
send "mkdir XML\r"
expect "sftp>"
send "cd /test/XML\r"
expect "sftp>"
send "mput *\r"
expect "sftp>"
send "bye\r"
EOF

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

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

SFTP using Expect

I have tried the script below to SFTP using bash script. But it does not work. Always error at password.
/usr/local/bin/expect <<EOF
spawn sftp PG1#dev1.dummy.com
expect "Password:"
send "abc123\r"
expect "sftp>"
send "cd /tmp\r"
send "get Data.dat\r"
send "get List.dat\r"
send "bye\r"
EOF
Here's the log file when I run the script above, after taking in all the suggestions from responders.
######### StartJob #########
Sun Apr 19 09:59:08 MYT 2015
spawn sftp PG1#dev1.dummy.com^M
Connecting to dev1.dummy.com...^M
Password: ^M
sftp> ERROR: Data file Data.dat not successfully extracted!
Sun Apr 19 09:59:12 MYT 2015
########## EndJob #########
Note that you are expecting password worth lower case p,but the log shows a capital P
Thank you for your attempts to solve the problem above. I found the solution. Here's what I did:
/usr/local/bin/expect <<EOF
spawn sftp PG1#dev1.dummy.com
expect "Password:"
send "abc123\r"
expect "sftp>"
send "cd /tmp\r"
expect "sftp>"
send "get Data.dat\r"
expect "sftp>"
send "get List.dat\r"
expect "sftp>"
send "bye\r"
EOF

Resources