#!/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
#!/usr/bin/expect set passwd "123123a" 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
一、rsync.expect 内容shell
#!/usr/bin/expect set passwd "123123a" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -av --files-from=$file / root@$host:/ //这个地方定义了原目录和目标目录以跟目录开始 expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
#!/bin/bash for ip in cat ip.list
do echo $ip ./rsync.expect $ip list.txt donebash
这个sh 的目的,就是遍历一下 ip列表文件中的 ip地址多线程
最重要的,expect脚本 必须加入执行权限ssh
文件不存在,会报错网站
分发系统还有一个重要的关键是,确保同步的机器的密码一致,不然将不能实现同步;因此这就存在一个弊端,一旦脚本暴露,将会让别人知道如何登录你机器;固然也有对应的解决办法,那就是使用密钥认证,这样的话,天然在命令行业省去“输入密码< password:" { send "$passwd\r" } >''”和“定义密码< set passwd "123123a" >”的命令了spa
#!/usr/bin/expect set host [lindex $argv 0] set passwd "123456" set cm [lindex $argv 1] spawn ssh root@$host expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" expect "]*" send "exit\r"
#!/bin/bash for ip in `cat ip.list` do echo $ip ./exe.expect $ip "w;free -m;ls /tmp" done
shell多线程.net