Bash Script: Send files to SFTP using Expect

╄→尐↘猪︶ㄣ 提交于 2020-01-15 07:30:27

问题


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.

 #!/bin/bash
 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.


回答1:


You could pipe error info from your script to a file and grep for an error with a second script. The second script could even run the first and might look something like this:

/path/firstscript.sh 2>&1 > results.txt
if [ -n "$(grep 'error expected' results.txt)" ]
 then 
  echo "Found error" | mail -S "Error" you@example.com
 else
  echo "No error" | mail -s "Okay" you@example.com
 fi

Personally, I find it better to check for the file on the target site by doing a second connection and listing the output there. Then I check that second connection log for the existence of the uploaded file.

I can't use rsync or key authentication with three different companies I script file transfers for, so I have some sympathy for people who get the answer "don't do that" when they ask a question. It's worse when you're searching for the answer because you already know that what people are recommending instead won't work.

Here's examples more closely based on what I actually do:

email_result.bash

#!/bin/bash
./uploadfile_example.bash 2>&1 > log.txt
if [ -n "$(grep "$(date +%b' '%d)" log.txt|grep "testfile")" ]
 then
  echo "File uploaded" |mail -s "Test success" you@example.com
 else
  echo "File not uploaded" |mail -s "Test fail" you@example.com
 fi

uploadfile_example.bash

#!/bin/bash
datestamp=$(date +%s)
/bin/cp -v testfile.txt testfile.${datestamp}.txt
HOST="localhost"
USER="testuser"
PASS="YourPasswordHere"

expect -c "
spawn sftp ${USER}@${HOST}
expect \"password: \"
send \"${PASS}\r\"
expect \"sftp>\"
send \"put testfile.${datestamp}.txt\r\"
expect \"sftp>\"
send \"ls -l testfile.${datestamp}.txt\r\"
expect \"sftp>\"
send \"bye\r\"
expect \"#\"
"

/bin/rm -vf testfile.${datestamp}.txt



回答2:


sftp is interactive shell, and when error occur, it prints error messages.

But It is hard to parse error message.

In your case, I recommand to use scp ( http://linux.die.net/man/1/scp ) command.

When fail to transfer file, scp will return error code to shell.




回答3:


sftp can use ssh key authentication, which should be preferable over password login through expect, as you're currently doing. Apart from passing password that way resembling a rube goldberg contraption, having plaintext passwords in a script would be a nightmare of every system admin. As alternatives, using the same authentication mechanism, but very scriptable, are rsync over ssh, and scp. scp and sftp are merely ssh subsystems, server side. rsync can use ssh as tunnel for data transfer, and offers several advantages over sftp or scp.



来源:https://stackoverflow.com/questions/24687754/bash-script-send-files-to-sftp-using-expect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!