一、vi 1.expect
內容以下:shell
#!/usr/bin/expect set passwd "123456" spawn rsync -av root@192.168.1.31:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof #expect eof的做用是等待腳本中的命令執行完後再退出。
二、chmod +x 1.expect
三、執行:./1.expectvim
一、vi 2.expect
腳本內容:bash
#!/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 a+x 2.expect
3、本腳本只能同步一個文件.ssh
root@a shell]# touch /tmp/3.txt [root@a shell]# ./2.expect 192.168.1.31 "/tmp/3.txt" spawn rsync -av /tmp/3.txt root@192.168.1.31:/tmp/3.txt root@192.168.1.31's password: sending incremental file list 3.txt sent 69 bytes received 31 bytes 66.67 bytes/sec
準備一臺模板機器,把要分發的文件準備好,而後使用expect腳本批量把須要同步的文件分發到目標機器(把多個文件分發到多臺機器時須要建立文件、IP列表,即本文中的list.txt、iplist.txt)。
建立 分發系統
一、建立一個文件列表文件備用:
在tmp目錄下建立多個文件,要保證客戶端有一樣的目錄。
[root@a ~]# vim /tmp/list.txt
/tmp/test1.txt
/tmp/test2.txtide
二、建立一個IP列表文件備用:ui
[root@a ~]# vim /tmp/iplist.txt
192.168.1.31 #該文件下能夠指定多個IP
三、建立rsync.expect腳本:
[root@a ~]# vim rsync.expectspa
#!/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
四、給權限code
[root@a ~]# chmod a+x rsync.expect [root@a ~]# vim rsync.sh
#!/bin/bash for ip in `cat /tmp/iplist.txt` do ./rsync.expect $ip /tmp/list.txt done
五、執行
sh -x rsync.sh
++ cat /tmp/iplist.txtip
cat /tmp/iplist.txt
'sent 163 bytes received 53 bytes 432.00 bytes/sec
total size is 13 speedup is 0.06rem
1、建立exe.expect
[root@a ~]# vim exe.expect
#!/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"
[root@a ~]# chmod a+x exe.expect
[root@a ~]# vim exe.sh
#!/bin/bash for ip in `cat /tmp/iplist.txt` do ./exe.expect $ip "hostname" done
執行:sh exe.sh