2018-06-07 Linux學習
自動同步文件
#!/usr/bin/expect
set passwd "123456"
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
操作過程
[root@linux-01 sbin]# vim 4.expect 寫入上面 自動同步文件 內容 [root@linux-01 sbin]# chmod a+x 4.expect [root@linux-01 sbin]# ./4.expect spawn rsync -av [email protected]:/tmp/12.txt /tmp/ [email protected]‘s password: receiving incremental file list 12.txt sent 30 bytes received 84 bytes 20.73 bytes/sec total size is 5 speedup is 0.04
20.32 expect腳本指定host和要同步的文件
指定host和要同步的文件
#!/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
操作過程
[root@linux-01 sbin]# vim 5.expect 寫入上面的內容 指定host和要同步的文件 [root@linux-01 sbin]# chmod a+x 5.expect [root@linux-01 sbin]# ./5.expect 192.168.106.165 /tmp/12.txt spawn rsync -av /tmp/12.txt [email protected]:/tmp/12.txt [email protected]‘s password: sending incremental file list sent 31 bytes received 12 bytes 7.82 bytes/sec total size is 5 speedup is 0.12 [root@linux-01 sbin]# ./5.expect 192.168.106.165 "/tmp/12.txt" spawn rsync -av /tmp/12.txt [email protected]:/tmp/12.txt [email protected]‘s password: sending incremental file list sent 31 bytes received 12 bytes 7.82 bytes/sec total size is 5 speedup is 0.12
20.33 構建文件分發系統
需求背景
對於大公司而言,肯定時不時會有網站或者配置文件更新,而且使用的機器肯定也是好多臺,少則幾臺,多則幾十甚至上百臺。所以,自動同步文件是至關重要的。
實現思路
首先要有一臺模板機器,把要分發的文件準備好,然後只要使用expect腳本批量把需要同步的文件分發到目標機器即可。
核心命令rsync -av --files-from=list.txt??/??root@host:/
文件分發系統的實現
rsync.expect 內容
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
expect eof
ip.list內容
192.168.133.132
192.168.133.133
......
rsync.sh 內容
#!/bin/bash
for ip in cat /tmp/ip.list
do
./rsync.expect $ip /tmp/file.list
done
操作過程
[root@linux-01 sbin]# vim rsync.expect
#!/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
[root@linux-01 sbin]# chmod a+x rsync.expect
[root@linux-01 sbin]# vim /tmp/file.list
/tmp/12.txt
/root/shell/1.sh
/root/111/222/123.txt
[root@linux-01 sbin]# vim /tmp/ip.list
192.168.106.165
[root@linux-01 sbin]# vim rsync.sh
#!/bin/bash
for ip in cat /tmp/ip.list
do
./rsync.expect $ip /tmp/file.list
done
[root@linux-01 sbin]# sh -x rsync.sh
++ cat /tmp/ip.list
+ for ip in ‘`cat /tmp/ip.list`‘
+ ./rsync.expect 192.168.106.165 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / [email protected]:/
[email protected]‘s password:
building file list ... rsync: link_stat "/root/shell/1.sh" failed: No such file or directory (2)
done
root/
root/111/
root/111/222/
root/111/222/123.txt
tmp/
tmp/12.txt
sent 221 bytes received 62 bytes 43.54 bytes/sec
total size is 5 speedup is 0.02
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
20.34 批量遠程執行命令
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"
exe.sh 內容
#!/bin/bash
for ip in cat ip.list
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done
操作過程
[root@linux-01 sbin]# vim exe.expect
寫入上面 exe.expect 內容
[root@linux-01 sbin]# chmod a+x exe.expect
[root@linux-01 sbin]# vim exe.sh
#!/bin/bash
for ip in cat /tmp/ip.list
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done
[root@linux-01 sbin]# sh exe.sh
192.168.106.165
spawn ssh [email protected]
[email protected]‘s password:
Last login: Tue Apr 24 17:47:27 2018 from 192.168.106.160
[root@linux-02 ~]# w;free -m;ls /tmp
17:48:41 up 1:51, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.106.1 15:57 15:13 0.04s 0.04s -bash
root pts/1 192.168.106.160 17:48 1.00s 0.02s 0.01s w
total used free shared buff/cache available
Mem: 1822 159 1460 8 202 1473
Swap: 4095 0 4095
12.txt systemd-private-419467cd2d3a46f89cde1d660ef24ce8-vgauthd.service-QH8NC0
d6z systemd-private-419467cd2d3a46f89cde1d660ef24ce8-vmtoolsd.service-fDRGcr
etc
2018-06-07 Linux學習