linux sftp自动输入密码,使用autoexpect避免sftp输入密码
先是從數(shù)據(jù)庫中導出所需文件,再將此文件轉移至一個web server,然后通過此web server用sftp傳輸給第三方的server
所用到的腳本依次如下
先從數(shù)據(jù)庫導出文本,并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