#!/usr/bin/expect set passwd "rootroot" spawn rsync -av root@192.168.0.132:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r"} } expect eof
執行vim
[root@qingyun-01 sbin]# ./4.expect spawn rsync -av root@192.168.0.132:/tmp/12.txt /tmp/ root@192.168.0.132's password: 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@qingyun-01 sbin]# ls /tmp/ 12.txt
#腳本內容 #!/usr/bin/expect set passwd "rootroot" 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
執行bash
[root@qingyun-01 sbin]# vim 5.expect [root@qingyun-01 sbin]# chmod a+x 5.expect [root@qingyun-01 sbin]# ./5.expect 192.168.0.132 "/tmp/12.txt" spawn rsync -av /tmp/12.txt root@192.168.0.132:/tmp/12.txt root@192.168.0.132's password: sending incremental file list sent 31 bytes received 12 bytes 86.00 bytes/sec total size is 5 speedup is 0.12 #本腳本適合同步一個文件 #只把本地的12.txt同步到遠程
rsync -av --files-from=list.txt / root@host:/
#rsync.expect內容 #!/usr/bin/expect set passwd "rootroot" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -avR $file root@$host:$file #若是不肯定遠程路徑能夠 加選項 -R #來建立路徑 expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r"} } expect eof #建立一個文件存放文件列表 [root@qingyun-01 sbin]# cat /tmp/file.list /tmp/12.txt /tmp/1/2/1.txt /tmp/2/2.txt
[root@qingyun-01 sbin]# vim /tmp/ip.list 192.168.0.132 127.0.0.1 ......
#!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip ./rsync.expect $ip /tmp/file.list done
執行結果ssh
[root@qingyun-01 sbin]# sh -x rsync.sh ++ cat /tmp/ip.list + for ip in '`cat /tmp/ip.list`' + echo 192.168.0.132 192.168.0.132 + ./rsync.expect 192.168.0.132 /tmp/file.list spawn rsync -avR --files-from=/tmp/file.list / root@192.168.0.132:/ root@192.168.0.132's password: building file list ... done sent 143 bytes received 12 bytes 310.00 bytes/sec total size is 5 speedup is 0.03 + for ip in '`cat /tmp/ip.list`' + echo 127.0.0.1 127.0.0.1 + ./rsync.expect 127.0.0.1 /tmp/file.list spawn rsync -avR --files-from=/tmp/file.list / root@127.0.0.1:/ root@127.0.0.1's password: building file list ... done sent 143 bytes received 12 bytes 310.00 bytes/sec total size is 5 speedup is 0.03 #注意:增長執行權限 chmod a+x 文件名 #結果 [root@qingyun-01 sbin]# ./1.expect spawn ssh root@192.168.0.132 root@192.168.0.132's password: Last login: Wed Feb 28 22:53:45 2018 from 192.168.0.130 [root@qingyun-02 ~]# ls /tmp/ 1 12.txt 2 2.txt [root@qingyun-02 ~]# ls /tmp/2/ 2.txt
#!/usr/bin/expect set host [lindex $argv 0] set passwd "rootroot" 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 /tmp/ip.list` do echo $ip ./exe.expect $ip "w;free -m;ls /tmp" done
執行過程&結果ide
[root@qingyun-01 sbin]# vi exe.expect [root@qingyun-01 sbin]# vi exe.sh [root@qingyun-01 sbin]# chmod a+x exe.expect ;chmod a+x exe.sh; [root@qingyun-01 sbin]# sh -x exe.sh ++ cat /tmp/ip.list + for ip in '`cat /tmp/ip.list`' + echo 192.168.0.132 192.168.0.132 + ./exe.expect 192.168.0.132 'w;free -m;ls /tmp' spawn ssh root@192.168.0.132 root@192.168.0.132's password: Last login: Wed Feb 28 23:09:45 2018 from 192.168.0.130 [root@qingyun-02 ~]# w;free -m;ls /tmp 23:19:26 up 2:27, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.0.4 20:53 1:45m 0.06s 0.06s -bash root pts/1 192.168.0.130 23:19 1.00s 0.01s 0.00s w total used free shared buff/cache available Mem: 1823 96 1465 8 261 1555 Swap: 2047 0 2047 1 12.txt 2 2.txt systemd-private-f76127fe2f6b440a98fd57cb13173406-chronyd.service-lcHZr0 systemd-private-f76127fe2f6b440a98fd57cb13173406-vgauthd.service-HtFYwp systemd-private-f76127fe2f6b440a98fd57cb13173406-vmtoolsd.service-rAX6FO yum_save_tx.2018-02-28.20-58.xdulyV.yumtx yum_save_tx.2018-02-28.21-03.8mtfU5.yumtx [root@qingyun-02 ~]# + for ip in '`cat /tmp/ip.list`' + echo 127.0.0.1 127.0.0.1 + ./exe.expect 127.0.0.1 'w;free -m;ls /tmp' spawn ssh root@127.0.0.1 root@127.0.0.1's password: Last failed login: Wed Feb 28 23:04:17 CST 2018 from 127.0.0.1 on ssh:notty There were 2 failed login attempts since the last successful login. Last login: Wed Feb 28 20:53:17 2018 from 192.168.0.4 [root@qingyun-01 ~]# w;free -m;ls /tmp 23:19:28 up 2:27, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.0.4 20:53 8.00s 0.42s 0.00s /usr/bin/expect ./exe.expect root pts/2 127.0.0.1 23:19 0.00s 0.02s 0.00s w total used free shared buff/cache available Mem: 1823 99 1426 8 296 1550 Swap: 2047 0 2047 1 file.list 12.txt ip.list 1.txt systemd-private-2c7c3aa47d8f4e789cecfdb3a9fab54c-chronyd.service-2qG20X 2 systemd-private-2c7c3aa47d8f4e789cecfdb3a9fab54c-vgauthd.service-BmrrqI 2.txt systemd-private-2c7c3aa47d8f4e789cecfdb3a9fab54c-vmtoolsd.service-Pkezae