[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