linux sftp自动输入密码,使用autoexpect避免sftp输入密码 -尊龙游戏旗舰厅官网
先是从数据库中导出所需文件,再将此文件转移至一个web server,然后通过此web server用sftp传输给第三方的server
所用到的脚本依次如下
先从数据库导出文本,并scp到web server
--为避免输入密码时候人机交互,需要将两个server配置用户等价
[oracle@justin tools]$ more sftpchenying.sh
source /home/oracle/.bash_profile
date1=`date --date='1 day ago' %y%m%d`
/justin/./sqluldr2_linux64_10204.bin justin/**** sql=/justin/sftp.sql field=## charset="zhs16gbk" file=/justin/product"$date1".csv
scp /justin/product"$date1".csv
rm /justin/product"$date1".csv
在中转server上部署以下脚本
[deploy@justin ~]$ more sftp.sh
date1=`date --date='1 day ago' %y%m%d`
sftp -oport=3222 justin@*.*.*.* << end
cd /upload
lcd /home/deploy
mput product$date1.csv
end
rm /home/deploy/product$date1.csv
同样为了避免sftp输入密码的人机交互,使用expect命令
先通过root用户yum install expect安装expect,然后输入autoexpect sftp.sh,此时会产生一个名为script.exp的文件,每次只需调用./script.exp即可自动完成sftp到remote server并上传文件的操作
似乎只能通过./script.exp的方式调用该文件,设置crontab时候比较麻烦,可以新建一个文件
[deploy@justin ~]$ more sftpexec.sh
cd /home/deploy
./script.exp
crontab设置则如下
[deploy@justin ~]$ crontab -l
0 2 * * * sh /home/deploy/sftpexec.sh > /home/deploy/sftpexec.log 2>&1
而生成的script.exp内容如下
[deploy@justin ~]$ more script.exp
#!/usr/bin/expect -f
set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script. wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
set timeout -1
spawn ./sftp.sh
match_max 100000
expect -exact "connecting to *.*.*.*...\r
password: "
send -- "password\r"
expect eof
总结
以上是尊龙游戏旗舰厅官网为你收集整理的linux sftp自动输入密码,使用autoexpect避免sftp输入密码的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 管道半双工通信程序linux,linux
- 下一篇: linux 环境变量 term,linu