欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 运维知识 > linux >内容正文

linux

linux sftp自动输入密码,使用autoexpect避免sftp输入密码 -凯发k8官方网

发布时间:2024/10/8 linux 0 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 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

总结

以上是凯发k8官方网为你收集整理的linux sftp自动输入密码,使用autoexpect避免sftp输入密码的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图