ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 使用autoexpect避免sftp输入密码

使用autoexpect避免sftp输入密码

原创 Linux操作系统 作者:myownstars 时间:2011-09-21 15:36:40 0 删除 编辑


先是从数据库中导出所需文件,再将此文件转移至一个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 deploy@*.*.*.*:/home/deploy
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
justin@*.*.*.*'s password: "
send -- "password\r"
expect eof

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15480802/viewspace-708026/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-03-18

  • 博文量
    375
  • 访问量
    3320723