linux/unix脚本中使用sftp上传/下载文件

在shell脚本中,使用sftp方式自动上传/下载脚本
上传代码示例如下:
touch scp.BaseCheck
echo ’
#!/usr/bin/expect -f
set timeout 600
spawn sftp ‘UserName1’@‘ServName1’
expect {
“*yes/no” { send “yes\r”; exp_continue }
“*Password:” { send “‘PassWd1’\r” }
}
expect “>”
send “lcd ‘{DATA}’/PTPLDM/’{yy1}{mm1}{dd1}’/\r”
expect “>”
send “cd /backup/upload/\r”
expect “>”
send “mkdir ‘{yy1}{mm1}{dd1}’\r”
expect “>”
send “cd ‘{yy1}{mm1}{dd1}’\r”
expect “>”
send “mkdir pldm\r”
expect “>”
send “chmod 775 pldm\r”
expect “>”
send “cd /backup/upload/’{yy1}{mm1}{dd1}’/pldm/\r”
expect “>”
send “put ‘{MYDATA}’/PTPLDM/’{yy1}{mm1}{dd1}’/pldm_data_pl_’{yy1}{mm1}{dd1}‘000.tar\r"
expect “>”
send "put ‘{MYDATA}’/PTPLDM/’{yy1}{mm1}{dd1}’/pldm_data_pl
’{yy1}{mm1}{dd1}’_000.ok\r”
expect “>”
send “cd /home/gpadmin/ETL/DATA/receive/\r”
expect “>”
send “put ‘{MYDATA}’/PTPLDM/’{yy1}{mm1}{dd1}’/dir.PLDM_DATAFILE’{yy1}{mm1}${dd1}’\r”
expect “>”
send “bye\r”
expect eof
’ > scp.BaseCheck

#注意脚本执行路径
expect -f {MYDATA}/PTPLDM/{yy1}{mm1}{dd1}/scp.BaseCheck 2>&1 | tee $ferr

grep -E “100%” $ferr| wc -l | read res
if [ $res -ne 0 ]
then
echo “数据传输完毕!”
else
echo “数据传输失败!”
exit 1
fi

下载代码示例如下:
touch scp.BaseCheck
echo ’
#!/usr/bin/expect -f
spawn sftp ‘UserName1’@‘ServName1’
expect {
“*yes/no” { send “yes\r”; exp_continue }
“*Password:” { send “‘PassWd1’\r” }
}
expect “>”
send “get /backup/expdata/’{yy1}{mm1}{dd1}’/pld/exp_pldm_plms_’{yy1}{mm1}{dd1}’.tar\r”
expect “>”
send “get /backup/expdata/’{yy1}{mm1}{dd1}’/pld/exp_pldm_plms_’{yy1}{mm1}{dd1}’.ok\r”
expect “>”
send “bye\r”
expect eof
’ > scp.BaseCheck

expect -f {PLMSDATA}/PTPLDM/scp.BaseCheck 2>&1 | tee $ferr

你可能感兴趣的:(liunx/unix)