One, expect script synchronization file
[[email protected] ~]# cd /usr/local/sbin/[[email protected] sbin]# ls01.expect 02.expect 03.expect check_ng.sh lvs_dr.sh lvs_nat.sh mon nginx_log_rotate.sh[[email protected] sbin]# vim 04.expect //自动同步脚本#!/usr/bin/expectset passwd "rootroot"spawn rsync -av [email protected]:/tmp/12.txt /tmp/expect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r"}}expect eof[[email protected] sbin]# ./04.expect spawn rsync -av [email protected]:/tmp/12.txt /tmp/[email protected]‘s password: receiving incremental file list12.txtsent 30 bytes received 84 bytes 228.00 bytes/sectotal size is 5 speedup is 0.04[[email protected] sbin]# ls /tmp/12.txt /tmp/12.txt
Ii. expect script specifies host and files to synchronize
[[email protected] sbin]# vim 05.expect #!/usr/bin/expectset passwd "rootroot"set host [lindex $argv 0]set file [lindex $argv 1]spawn rsync -av $file [email protected]$host:$fileexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r"}}expect eof[[email protected] sbin]# chmod a+x 05.expect[[email protected] sbin]# touch /tmp/123.txt[[email protected] sbin]# ./05.expect 192.168.242.129 /tmp/123.txtspawn rsync -av /tmp/123.txt [email protected]:/tmp/123.txt[email protected]‘s password: sending incremental file list123.txtsent 71 bytes received 31 bytes 204.00 bytes/sectotal size is 0 speedup is 0.00
Iii. building a file distribution system
Requirements background: For large companies, there must be a site or configuration file updates from time to time, and the use of the machine is certainly a lot of units, fewer, more than dozens of or even hundreds of Taiwan. Therefore, automatic synchronization of files is critical.
Realize the idea: first of all to have a template machine, the files to be distributed ready, and then as long as the use of expect script to distribute the files need to be synchronized to the target machine
Core command:rsync -av --files-from=list.txt / [email protected]:/
1, the implementation of the file distribution system
[[email protected] sbin]# vim rsync.expect
#!/usr/bin/expect
Set passwd "Rootroot"
Set host [lindex $ARGV 0]
Set file [lindex $argv 1]
Spawn RSYNC-AVR $file [email protected] $host: $file
#如果不确定远程路径可以 Plus Option-r
#来创建路径
Expect {
"Yes/no" {send "yes\r"}
"Password:" {send "$passwd \ r"}
}
Expect EOF
Shell Scripting Basics (eight)