Bash脚本:使用Expect将文件发送到SFTP [英] Bash Script: Send files to SFTP using Expect
问题描述
我必须从本地服务器向SFTP服务器发送一些压缩文件.
I have to send few gzipped files from local server to SFTP server.
我的服务器信息
发行人ID:Ubuntu 说明:Ubuntu 12.04.4 LTS 发行:12.04 代号:precise
Distributor ID: Ubuntu Description: Ubuntu 12.04.4 LTS Release: 12.04 Codename: precise
创建了一个bash脚本,能够将文件发送到sftp,但是如果传输成功,我想发送电子邮件.
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.
请帮助!
谢谢.
推荐答案
您可以将错误信息从脚本传递到文件,然后使用grep通过第二个脚本传递错误信息.第二个脚本甚至可以运行第一个脚本,并且可能类似于以下内容:
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.
我不能与我为文件传输编写脚本的三个不同的公司一起使用rsync或密钥身份验证,因此我对那些在回答问题时得到不要那样做"答案的人表示同情.当您寻找答案时,情况会更糟,因为您已经知道人们的建议反而行不通.
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
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
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
这篇关于Bash脚本:使用Expect将文件发送到SFTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!