[root@yong-01 sbin]# vim 4.expect #!/usr/bin/expect set passwd "20655739" spawn rsync -av root@192.168.181.135:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send $passwd\r} } expect eof 查看同步过程 [root@yong-01 sbin]# chmod a+x 4.expect [root@yong-01 sbin]# ./4.expect spawn rsync -av root@192.168.181.135:/tmp/12.txt /tmp/ The authenticity of host '192.168.181.135 (192.168.181.135)' can't be established. ECDSA key fingerprint is SHA256:oYqovve1b2BwHDBYcFasCiiFzZTHJvKDbTGZAjmlMXc. ECDSA key fingerprint is MD5:3d:f8:af:0d:85:48:db:2a:46:0e:68:5f:eb:43:3e:43. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.181.135' (ECDSA) to the list of known hosts. receiving incremental file list 12.txt sent 30 bytes received 84 bytes 228.00 bytes/sec total size is 5 speedup is 0.04 [root@yong-01 sbin]# ls /tmp/12.txt /tmp/12.txt [root@yong-01 sbin]# ll /tmp/12.txt -rw-r--r-- 1 root root 5 7月 21 00:48 /tmp/12.txt
[root@yong-01 sbin]# vim 5.expect #!/usr/bin/expect set passwd "20655739" 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
[root@yong-01 sbin]# ./5.expect 192.168.181.135 "/tmp/12.txt" spawn rsync -av /tmp/12.txt root@192.168.181.135:/tmp/12.txt sending incremental file list 12.txt sent 79 bytes received 31 bytes 220.00 bytes/sec total size is 5 speedup is 0.05 expect: spawn id exp6 not open while executing "expect eof" (file "./5.expect" line 10)
一、rsync.expect 内容shell
[root@yong-01 sbin]# vim rsync.expect #!/usr/bin/expect set passwd "20655739" 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
#!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip ./rsync.expect $ip /tmp/file.list done 执行过程 [root@yong-01 sbin]# sh -x rsync.sh ++ cat /tmp/ip.list + for ip in '`cat /tmp/ip.list`' + echo 192.168.181.135 192.168.181.135 + ./rsync.expect 192.168.181.135 /tmp/file.list spawn rsync -avR --files-from=/tmp/file.list / root@192.168.181.135:/ building file list ... done root/ sent 114 bytes received 15 bytes 258.00 bytes/sec total size is 1051 speedup is 8.15 expect: spawn id exp6 not open while executing "expect eof" (file "./rsync.expect" line 10) + for ip in '`cat /tmp/ip.list`' + echo 192.168.181.138 192.168.181.138 + ./rsync.expect 192.168.181.138 /tmp/file.list spawn rsync -avR --files-from=/tmp/file.list / root@192.168.181.138:/ root@192.168.181.138's password: building file list ... done root/ root/shell/ root/shell/case.sh tmp/ tmp/12.txt sent 1257 bytes received 59 bytes 2632.00 bytes/sec total size is 1051 speedup is 0.80
这个sh 的目的,就是遍历一下 ip列表文件中的 ip地址vim
最重要的,expect脚本 必须加入执行权限bash
文件不存在,会报错ssh
分发系统还有一个重要的关键是,确保同步的机器的密码一致,不然将不能实现同步;因此这就存在一个弊端,一旦脚本暴露,将会让别人知道如何登录你机器;固然也有对应的解决办法,那就是使用密钥认证,这样的话,天然在命令行业省去“输入密码< password:" { send "$passwd\r" } >''”和“定义密码< set passwd "123123a" >”的命令了网站
#!/usr/bin/expect set host [lindex $argv 0] set passwd "20655739" 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