您的位置:首页

expect脚本同步文件 expect脚本指定host和要同步的文件 构建文件分发系统 批量远程执行命令

2018-04-29 23:14 736 查看

自动同步文件

#!/usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.133.132:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof


指定host和要同步的文件

#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file root@$host:$file
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

chmod +x ex4.sh
./ext4.sh  192.168.133.132 "/tmp/12.txt"


需求背景
对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。

实现思路
首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。

核心命令
rsync -av --files-from=list.txt / root@host:/

文件分发系统的实现

• rsync.expect 内容
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -avR --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof


如果不能保证对应机器上也有相同的文件路径加上-R 自动创建

list.txt内容 (保证文件列表路径对方机器也有)

/tmp/12.txt
/root/shell/1.sh
。。。。


ip.list内容

192.168.133.132
192.168.133.133
......


如果多台主机密码不同可以搞秘钥认证。

rsync.sh 内容

#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
./rsync.expect $ip list.txt
done


执行:chmoda+x rsync.sh

./rsync.sh

批量远程执行命令

exe.sh 内容

#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐