expect script to synchronize files
Change permissions
execute script
View execution results
expect eof needs to be added, the function is to wait for the script command to be executed before exiting
The expect script specifies the host and the file to be synchronized
Change permissions, execute scripts
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.
- Realize ideas
First of all, 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.
- core command
rsync -av --files-from=list.txt / root@host:/
Create the rsync.expect script
Create a list file of synced files
vi /tmp/file.list
Create a list file of sync IPs
vi /tmp/ip.list
Create a rsync.sh script (traverse the ip address in the ip list file)
Change permissions
execute script
Note: When synchronizing, you need to ensure that the other machine also has the same path, you can use -R to create a path; the premise of making a distribution system is to ensure that the synchronized machine passwords are consistent, which will cause security risks of password leakage, you can use key authentication .
Batch remote execution of commands
Create exe.expect
Create exe.sh
execute script