expect builds the distribution filesystem

20.31 expect script to synchronize files

Sync files from server2 to server1.

[root@z1 ~]# vim 4.expect
#!/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

Change permissions:
[root@z1 ~]# chmod a+x 4.expect

执行:
[root@z1 ~]# ./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 76.00 bytes/sec
total size is 5 speedup is 0.04

Check the local file:
[root@z1 ~]# ls /tmp/
12.txt
Description: The function of expect eof is to wait for the commands in the script to be executed before exiting.

20.32 The expect script specifies the host and the files to be synchronized

[root@z1 ~]# vim 5.expect
#!/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@z1 ~]# chmod a+x 5.expect

Create test file:
[root@z1 ~]# touch /tmp/3.txt

执行:
[root@z1 ~]# ./5.expect 192.168.8.138 "/tmp/3.txt"
spawn rsync -av /tmp/3.txt [email protected]:/tmp/3.txt
[email protected]'s password:
sending incremental file list
3.txt

sent 69 bytes received 31 bytes 200.00 bytes/sec
total size is 0 speedup is 0.00

Client:
[root@z2 ~]# ls /tmp/
12.txt 3.txt
Note: This script can only synchronize one file.

20.33 Building a file distribution system

Demand background:
For large companies, there must be website or configuration file updates from time to time, and there must be many machines used, ranging from a few to dozens or even hundreds. So, automatically syncing files is crucial.

Implementation idea:
First, you need a template machine, prepare the files to be distributed, and then just use the expect script to distribute the files that need to be synchronized to the target machine in batches (when distributing multiple files to multiple machines, you need to create files, IP list, namely list.txt, iplist.txt in this article).

Core command:

rsync -av --files-from=list.txt /root@host:/
create a distribution system

Create a file list file for backup:

[root@z1 ~]# vim /tmp/list.txt
/tmp/12.txt /tmp/3.txt
#Multiple
files can be added to this file
Note: Make sure that the client has the same directory here.

Create an IP list file for backup:

[root@z1 ~]# vim /tmp/iplist.txt
192.168.8.138 #Multiple IPs
can be specified in this file
Note: The IP (host) password here should be the same as that in the rsync.expect script. In order to avoid the risk of password leakage, the method of key authentication can be used.

Create the rsync.expect script:

[root@z1 ~]# 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@z1 ~]# chmod a+x rsync.expect
to create rsync.sh:

[root@z1 ~]# vim rsync.sh
#!/bin/bash
for ip in cat /tmp/iplist.txt
do
./rsync.expect $ip /tmp/list.txt
done
Description: The function of this script is to traverse the file and IP list.

implement:

[root@z1 ~]# sh -x rsync.sh
++ cat /tmp/iplist.txt

  • for ip in 'cat /tmp/iplist.txt'
  • ./rsync.expect 192.168.8.138 /tmp/list.txt
    spawn rsync -avR --files-from=/tmp/list.txt / [email protected]:/
    [email protected]'s password:
    building file list ... done

sent 65 bytes received 12 bytes 154.00 bytes/sec
total size is 0 speedup is 0.00

Client:
[root@z2 ~]# ls /tmp/
12.txt 3.txt
Multiple files are synchronized successfully!

20.34 Batch remote command execution

Create exe.expect

[root@z1 ~]# 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@z1 ~]# chmod a+x exe.expect
The function of this script is to execute commands remotely.

Create exe.sh:

[root@z1 ~]# vim exe.sh
#!/bin/bash
for ip in cat /tmp/iplist.txt
do
./exe.expect $ip "hostname"
done
The function of this script is to call the IP and exe.expect scripts in the iplist.txt file .

implement

[root@z1 ~]# sh exe.sh
spawn ssh [email protected]
[email protected]'s password:
Last login: Thu Sep 21 18:21:42 2017 from 192.168.8.1
[root@z2 ~]# hostname
z2
[root@z2 ~]# [root@z1 ~]#

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324927533&siteId=291194637