Bash Script: Send files to SFTP using Expect


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Script: Send files to SFTP using Expect
# 1  
Old 07-11-2014
Bash Script: Send files to SFTP using Expect

I have to send few gzipped files from local server to SFTP server.

My Server Info

Distributor ID: Ubuntu Description: Ubuntu 12.04.4 LTS Release: 12.04 Codename: precise

Created a bash script and could able to send files to sftp, but i want to send email if transfer is successful.

Code:
#!/bin/bash</code>
 HOST=hostname.domain
 PORT=22
 USER=username
 PASSWORD=password
 SOURCE_FILE=path/filename
 TARGET_DIR=PATH

 /usr/bin/expect<<EOD

 spawn /usr/bin/sftp -o Port=$PORT $USER@$HOST
 expect "password:"
 send "$PASSWORD\r"
 expect "sftp>"
 send "put $SOURCE_FILE $TARGET_DIR\r"
 expect "sftp>"
 send "bye\r"
 EOD

Now i want if the above transfer is successfull send Success Email if not Failure email with error message.

Please help!!

Thanks.
# 2  
Old 07-11-2014
Code:
/usr/bin/expect<<EOD > output.log
spawn /usr/bin/sftp -o Port=$PORT $USER@$HOST
expect "password:"
send "$PASSWORD\r"
expect "sftp>"
send "put $SOURCE_FILE $TARGET_DIR\r"
expect "sftp>"
send "bye\r"
EOD
RC=$?
if [[ ${RC} -ne 0 ]]; then
  cat output.log | mail -s "Errors Received" "email@domain.com"
else
  echo "Success" | mail -s "Transfer Successful" "email@domain.com"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

To send ID and Password for each command using expect feature in bash script

Dear Tech Guys, I am trying to send some commands on the local server and it always asks for user name and password after each command. To serve the purpose I am using expect function as follows: #!/usr/bin/expect set timeout 20 spawn "./data1.sh" expect "Please Enter UserName: "... (6 Replies)
Discussion started by: Xtreme
6 Replies

2. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

3. Shell Programming and Scripting

Problems with expect and sftp in bash

I'm having trouble with some automated sftp pulls. I'm using expect inside bash scripts and spawning SFTP. Some times the expect seems bog down. I have tried to put sleeps in my code to give everything time to work before I move on to next step but I till continue to get issues. For example when... (2 Replies)
Discussion started by: gosteen
2 Replies

4. AIX

Send ctrl+C in expect script

Hi, Am trying to transfer file via FTP using expect script from server to client i need to interrupt the file transfer between server and client Please help what should used in expect code.. I used send "ctrl+c\r" expect "Aborted" but that didnt work.. I need what should... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

5. Shell Programming and Scripting

Expect Script for SFTP Upload

I am attempting to utilize an expect script (that is called from a parent bash script) to perform a file transfer over sftp. The script works except I cannot catch timeouts. I need to be able to tell in the parent bash script when the expect script has timed out, or completed successfully. It... (3 Replies)
Discussion started by: thaller
3 Replies

6. Shell Programming and Scripting

How to automate sftp without using expect script?

How to automate sftp with out using expect script? My batch file has the password but it is not taking. Please see below. I want to use this sftp connection in a loop for pushing new files in a directory one at a time. Hence I can not use an expect script. bash-2.05$... (5 Replies)
Discussion started by: Tuxidow
5 Replies

7. Solaris

Issue with Expect Script for SFTP.

Hi Experts, I am learning expect and wrote the below script for automatic sftp into a server: #!/usr/local/bin/expect -f -d spawn sftp -v test@mumux503 # logs into mumux503 as test user expect "password:" sleep 20 send "test\r"; # sending the password for test... (3 Replies)
Discussion started by: Hari_Ganesh
3 Replies

8. Shell Programming and Scripting

within shell script send expect and if else

Hi, I have written one shell script , using that i am able to connect to remote machine but i have to #!/usr/bin/expect -f set address set username set password set OOLpath set dbusername set dbpasswd set tnsname set recdbusername set recdbpasswd set rectnsname spawn ssh... (2 Replies)
Discussion started by: mnmonu
2 Replies

9. Shell Programming and Scripting

within shell script send expect and if else

Hi I have written one shell script , using that i am able to connect to remote machine but i have to #!/usr/bin/expect -f set address set username set password set OOLpath set dbusername set dbpasswd set tnsname set recdbusername set recdbpasswd set rectnsname spawn ssh... (1 Reply)
Discussion started by: mnmonu
1 Replies

10. Shell Programming and Scripting

Help with Expect SFTP script

Hi All, Here is my Expect script, I don't get any error message when I run it. But the file never goes to other system? I also paste the output screen below. When I run the script, the script runs so fast. But when I do it manually, it takes about 10 minutes for the file to transfer. ... (1 Reply)
Discussion started by: samnyc
1 Replies
Login or Register to Ask a Question