EOF in expect script spawning sftp - linux

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

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

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?

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