[TOC]
V. Automatic synchronization of files 5.1 configuration scripts
The core command is to use the Rsync service.
[[email protected] sbin]# vim 4.expect#!/usr/bin/expectset passwd "123456"spawn rsync -av [email protected]:/tmp/12.txt /tmp/expect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect eof
5.2 Licensing and testing
# chmod a+x 4.expect [[email protected] sbin]# ./4.expectspawn rsync -avR //加上-R是为了便于在连接的主机上没有相同文件路径时,自动创建[email protected]:/tmp/12.txt /tmp/[email protected]‘s password: receiving incremental file listtmp/tmp/12.txtsent 34 bytes received 115 bytes 298.00 bytes/sectotal size is 5 speedup is 0.03
As you can see, the files have been successfully synchronized.
The role of expect EOF
Script, if we do not add this statement, once the login success will exit.
Add set timeout here instead of expect EOF is invalid Vi. specify host and file 6.1 script configuration to synchronize
vim 5.expect#!/usr/bin/expectset passwd "123456"set host [lindex $argv 0] //第一个参数是host,ipset file [lindex $argv 1] //第二个参数是文件,file是绝对路径spawn rsync -av $file [email protected]$host:$fileexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect eof
The purpose of the script is to push a local file to the remote server.
6.2 Licensing
# chmod a+x 5.expect
I am now trying to push my local file to the server: The file contents are as follows:
# cat /tmp/users.txt username: user_9 password: 1zg^NIx7onusername: user_5 password: e21oHjeYg=username: user_6 password: e21oHjeYg=username: user_7 password: e21oHjeYg=username: user_8 password: e21oHjeYg=username: user_5 password: gNR0o{1qjwusername: user_6 password: Pam1lxsN6[username: user_7 password: 6hd5p^XgwMusername: user_8 password: kvcoyJ5_G0username: user_5 password: is0wb*SNj7username: user_6 password: BEgg89qgz<username: user_7 password: Nxvt8-xGw8username: user_8 password: 6mpbk5sDS-
6.3 Execute, Script +host address + file (absolute path)
# ./5.expect 192.168.XXX.XXX "/tmp/users.txt" spawn rsync -av /tmp/users.txt [email protected]:/tmp/users.txt[email protected]8.72.132‘s password: sending incremental file listusers.txtsent 571 bytes received 31 bytes 1204.00 bytes/sectotal size is 494 speedup is 0.82
6.4 Go to the remote server to view
[[email protected] ~]# cat /tmp/users.txt username: user_9 password: 1zg^NIx7onusername: user_5 password: e21oHjeYg=username: user_6 password: e21oHjeYg=username: user_7 password: e21oHjeYg=username: user_8 password: e21oHjeYg=username: user_5 password: gNR0o{1qjwusername: user_6 password: Pam1lxsN6[username: user_7 password: 6hd5p^XgwMusername: user_8 password: kvcoyJ5_G0username: user_5 password: is0wb*SNj7username: user_6 password: BEgg89qgz<username: user_7 password: Nxvt8-xGw8username: user_8 password: 6mpbk5sDS-
has been successfully pushed!!!
But this is just the beginning, generally we push a software or on-line a service is certainly not a file can be. So what we need is a lot of file subdirectories + script push.
VII. document distribution system to achieve 7.1 requirements background
[] For large companies, there will often be a website or configuration file updates, and the use of the machine is certainly a lot of units, less than a few, more than dozens of or even hundreds of Taiwan. Therefore, automatic synchronization of files is critical.
Implementation ideas:
- [] Start with a template machine, prepare the files to be distributed, and then simply distribute the files that need to be synchronized to the target machine in batches using the expect script.
7.2 Core command: Rsync-av--files-from=list.txt/[Email protected]:/
Note that this is the root directory.
7.3 Script Configuration
vim rsync.expect#!/usr/bin/expectset passwd "123456"set host [lindex $argv 0]set file [lindex $argv 1]spawn rsync -avR --files-from=$file / [email protected]$host:/expect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect eof核心命令:rsync -av –files-from=$file / [email protected]$host:/:上传文件的列表是$file,我们需要在list当中去定义 哪些目录或文件是我们需要同步更新上线的。
7.4 List of files
[[email protected] sbin]# !vimvim /tmp/list.txt/tmp/12.txt/root/for01.sh/root/temp/test.txt
- [] If we need to synchronize the 3 files, the premise is to ensure that the other server must also have this directory, for example:/tmp//user/local/sbin//tmp/, otherwise synchronization will be wrong. (as we added in the previous script, Rsync-avr R is the automatic creation of cascading directories.) )
7.5 IP List
[[email protected] sbin]# vim /tmp/ip.list192.168.72.133127.0.0.1
There is an important premise: all machines must be guaranteed to have the same password. But this is a bit risky, and once the document leaks it's a little tricky. So we choose to authenticate the key for the server that needs synchronization. Once the key has been authenticated, we can omit the following statements in the script:
"password:" { send "$passwd\r" }
The second way is: Change the password one by one
[[email protected] sbin]# passwd更改用户 root 的密码 。新的 密码:重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。
7.6 The purpose of creating an rsync shell Script script: Traverse all the files in the server and list to synchronize to each server.
[[email protected] sbin]# vim rsync.sh#!/bin/bashfor ip in `cat /tmp/ip.list`do echo $ip ./rsync.expect $ip /tmp/list.txtdone
7.7 Licensing and testing:
[[email protected] sbin]# sh -x rsync.sh++ cat /tmp/ip.list+ for ip in ‘`cat /tmp/ip.list`‘+ echo 116.62.XXX.XXX116.62.XXX.XXX+ ./rsync.expect 116.62.XXX.XXX /tmp/list.txtspawn rsync -avR --files-from=/tmp/list.txt / [email protected]:/[email protected]‘s password: building file list ... doneroot/root/for01.shroot/temp/root/temp/test.txttmp/tmp/12.txtsent 622 bytes received 84 bytes 1412.00 bytes/sectotal size is 339 speedup is 0.48+ for ip in ‘`cat /tmp/ip.list`‘+ echo 127.0.0.1127.0.0.1+ ./rsync.expect 127.0.0.1 /tmp/list.txtspawn rsync -avR --files-from=/tmp/list.txt / [email protected]:/[email protected]‘s password: building file list ... donesent 145 bytes received 12 bytes 314.00 bytes/sectotal size is 339
Eight, batch remote execution command
Sometimes when the transfer is complete, you may need to restart the Nginx service, or the MySQL service, to use the remote Execute command.
8.1 Script Configuration
vim exe.expect#!/usr/bin/expectset host [lindex $argv 0]set passwd "123456"set cm [lindex $argv 1]spawn ssh [email protected]$hostexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect "]*"send "$cm\r"expect "]*"
8.2 Expect script authorization
chmod a+x exe.expect
8.3 shell scripts that define EXE
[[email protected] sbin]# vim exe.sh#!/bin/bashfor ip in `cat ip.list`doecho $ip./exe.expect $ip "w;free -m;ls /tmp"done
8.4 landed on the defined remote server and executed the relevant commands!
[[email protected] sbin]# sh-x exe.sh++ cat ip.list+ for IP in ' cat ip.list ' + echo 116.62.xxx.xxx116.62.xxx.xxx+. /exe.expect 116.62.xxx.xxx ' w;free-m;ls/tmp ' spawn ssh [email protected][email protected] ' s password:last Login:tue 1 09:06:02 2018 from 117.80.xxx.xxxwelcome to Alibaba Cloud Elastic Compute Service! [[email protected] ~]# w;free-m;ls/tmp 10:42:43 up 2:44, 3 users, load average:0.00, 0.01, 0.05USER TTY From [email protected] IDLE jcpu PCPU whatroot pts/0 117.60.xxx.xxx 08:01 2:01m 0.00s 0.00s-bashroot pts/1 117.80.xxx.xxx 09:06 13:47 0.00s 0.00s-bashroot pts/2 117.80.xxx.xxx 10: 0.00s 0.00s 0.00s w total used free shared Buff/cache availablemem:1 839 1403 0 372 1628swap:0 0 012.txtaegis-< Guid (5A2C30A2-A87D-490A-9281-6765EDAD7CBA) >sysTemd-private-791e0c77625c4133bda4340fcd191439-ntpd.service-mz3iqt[[email protected] ~]# + for IP in ' cat Ip.list ' + echo 127.0.0.1127.0.0.1+./exe.expect 127.0.0.1 ' w;free-m;ls/tmp ' spawn ssh [email protected][email& Nbsp;protected] ' s password:last failed login:tue May 1 10:01:00 CST 2018 from localhost on Ssh:nottythere were 2 failed Login attempts since the last successful login. Last Login:tue 1 09:06:00 2018 from 192.168.72.1[[email protected] ~]# w;free-m;ls/tmp 10:42:43 up 7:26, 3 Users, load average:0.00, 0.01, 0.05USER TTY from [email protected] IDLE jcpu PCPU Whatr Oot pts/0 192.168.72.1 07:52 1:59m 0.44s 0.44s-bashroot pts/1 192.168.72.1 09:06 3.00s 2.1 9s 0.01s/usr/bin/expect./exe.expect 127.0.0.1 w;root pts/3 localhost 10:42 0.00s 0.04s 0.00s W Total used free shared Buff/cache availablemem:1823 243 11465 1364swap:3813 0 381312.TXTIP.LISTLIST.TXTPHP-FCGI.SOCKSYSTEMD -private-59132022d890457896ed0047dcd941fe-cups.service-rfy8w2systemd-private-59132022d890457896ed0047dcd941fe-httpd.servi Ce-h5dzdmsystemd-private-59132022d890457896ed0047dcd941fe-vmtoolsd.service-iz3m2rtmpxavier.sock
Shell Project Distribution system-expect (bottom)