欢迎访问linux宝典,请记住本站唯一域名:www.linuxbaodian.com

scp+expect交叉,使推送变得更加简单

来源:原创    更新时间:2018-10-11 17:13:38    编辑:jason    浏览:3416


scp使用心得

我们经常会用到一些相同的配置之类的文件,所以会用到scp 来推送。下面是我写的一个小脚本,自己用着挺不错的,今天分享出来给各位看官。

expect环境安装

由于此脚本在使用中会用到pect 所以,这里需要安装pect环境,可以使用yum install 轻松安装

yum install expect expect-devel -y
#!/usr/bin/sh#auth:jason#date:2018-10-11export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binfile=$1for host in `echo 172.16.1.{31,41,51,61,7,8,9,10}`do /usr/bin/expect <<EOFspawn scp -rp $file root@$host:/etc/zabbix/expect {" (yes/no)? " {send "yes\r";exp_continue} "password:" {send "123456\r";} }expect eofEOFdone

因为在第一次使用ssh scp 到远程主机的时候  会有一个安全的提示,问你是否确定这次连接, 接下来会要求输入密码。 用pect 的作用 就是自动监控及输入对应的值 具体的可以查一下 pect 在使用的时候 带入参数 你要推送的文件即可如

代码测试

[root@backup zabbix]# sh scp.sh zabbix_agentd.conf spawn scp -rp zabbix_agentd.conf root@172.16.1.31:/etc/zabbix/root@172.16.1.31's password: zabbix_agentd.conf                                                        100%   10KB   7.3MB/s   00:00spawn scp -rp zabbix_agentd.conf root@172.16.1.41:/etc/zabbix/The authenticity of host '172.16.1.41 (172.16.1.41)' can't be established.ECDSA key fingerprint is SHA256:l9zUcI+eVsyHxG5M1C9b1hyXzoKyaZZeSU0DJlBExfs.ECDSA key fingerprint is MD5:a3:8f:79:ad:70:b3:7f:48:07:8d:8c:67:fc:4f:3e:00.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '172.16.1.41' (ECDSA) to the list of known hosts.root@172.16.1.41's password: zabbix_agentd.conf                                                        100%   10KB   7.6MB/s   00:00spawn scp -rp zabbix_agentd.conf root@172.16.1.51:/etc/zabbix/The authenticity of host '172.16.1.51 (172.16.1.51)' can't be established.ECDSA key fingerprint is SHA256:l9zUcI+eVsyHxG5M1C9b1hyXzoKyaZZeSU0DJlBExfs.ECDSA key fingerprint is MD5:a3:8f:79:ad:70:b3:7f:48:07:8d:8c:67:fc:4f:3e:00.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '172.16.1.51' (ECDSA) to the list of known hosts.root@172.16.1.51's password: zabbix_agentd.conf                                                        100%   10KB   5.0MB/s   00:00spawn scp -rp zabbix_agentd.conf root@172.16.1.61:/etc/zabbix/ssh: connect to host 172.16.1.61 port 22: Connection refusedlost connectionexpect: spawn id exp6 not openwhile executing"expect eof "spawn scp -rp zabbix_agentd.conf root@172.16.1.7:/etc/zabbix/The authenticity of host '172.16.1.7 (172.16.1.7)' can't be established.ECDSA key fingerprint is SHA256:l9zUcI+eVsyHxG5M1C9b1hyXzoKyaZZeSU0DJlBExfs.ECDSA key fingerprint is MD5:a3:8f:79:ad:70:b3:7f:48:07:8d:8c:67:fc:4f:3e:00.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '172.16.1.7' (ECDSA) to the list of known hosts.root@172.16.1.7's password: zabbix_agentd.conf                                                        100%   10KB   5.3MB/s   00:00spawn scp -rp zabbix_agentd.conf root@172.16.1.8:/etc/zabbix/The authenticity of host '172.16.1.8 (172.16.1.8)' can't be established.ECDSA key fingerprint is SHA256:l9zUcI+eVsyHxG5M1C9b1hyXzoKyaZZeSU0DJlBExfs.ECDSA key fingerprint is MD5:a3:8f:79:ad:70:b3:7f:48:07:8d:8c:67:fc:4f:3e:00.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '172.16.1.8' (ECDSA) to the list of known hosts.root@172.16.1.8's password: zabbix_agentd.conf                                                        100%   10KB   5.8MB/s   00:00spawn scp -rp zabbix_agentd.conf root@172.16.1.9:/etc/zabbix/The authenticity of host '172.16.1.9 (172.16.1.9)' can't be established.ECDSA key fingerprint is SHA256:l9zUcI+eVsyHxG5M1C9b1hyXzoKyaZZeSU0DJlBExfs.ECDSA key fingerprint is MD5:a3:8f:79:ad:70:b3:7f:48:07:8d:8c:67:fc:4f:3e:00.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '172.16.1.9' (ECDSA) to the list of known hosts.root@172.16.1.9's password: zabbix_agentd.conf                                                        100%   10KB   3.8MB/s   00:00spawn scp -rp zabbix_agentd.conf root@172.16.1.10:/etc/zabbix/The authenticity of host '172.16.1.10 (172.16.1.10)' can't be established.ECDSA key fingerprint is SHA256:l9zUcI+eVsyHxG5M1C9b1hyXzoKyaZZeSU0DJlBExfs.ECDSA key fingerprint is MD5:a3:8f:79:ad:70:b3:7f:48:07:8d:8c:67:fc:4f:3e:00.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '172.16.1.10' (ECDSA) to the list of known hosts.root@172.16.1.10's password: zabbix_agentd.conf

评论区

表情

共0条评论
  • 这篇文章还没有收到评论,赶紧来抢沙发吧~

相关内容

点击排行

随机新闻

评论排行榜