Sometimes we may need to operate servers in batches, such as uploading a file on the server, installing software, executing a command and script, restarting services, restarting servers, etc. If we operate them one by one manually, it will be very cumbersome and waste manpower. At this time, we can use expect to send instructions to the target server to implement batch operations. The following example will batch copy a file on centos to other service providers and execute the corresponding command 1. Install expect on centos yum install expect 2. Write the expect script copyfilebatch.sh The following script will copy an rc.local file to the servers with intranet IP addresses 192.168.0.102 to 192.168.0.112. After the copy is successful, execute the chmod command to restart the servers respectively. #!/usr/bin/expect -f set password rootpassword for {set i 102} {$i <= 112} {incr i} { set ip "192.168.0.$i" puts "$ip" spawn ssh -o StrictHostKeyChecking=no $ip set timeout 3 expect "root@$ip's password:" set timeout 3 send "$password\r" set timeout 3 send "exit\r" spawn scp /home/install/rc.local root@$ip:/etc/rc.d/rc.local set timeout 3 expect "root@$ip's password:" set timeout 3 send "$password\r" set timeout 3 send "exit\r" spawn ssh root@$ip expect { "*yes/no" { send "yes\r"; exp_continue} "*password:" { send "$password\r" } } expect "#*" #Command to be executed send "chmod +x /etc/rc.d/rc.local\r" send "reboot\r" send "exit\r" expect eof } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Example analysis of mysql user rights management
>>: Teach you how to subcontract uniapp and mini-programs (pictures and text)
max_allowed_packet is a parameter in MySQL that i...
When displaying long data in HTML, you can cut off...
Table of contents Basic concepts of components Th...
Preface: I used the official nginx proxy_cache as...
This article mainly explains tables, which are no...
Effect picture: Preface: Recently, I was working ...
1. Get the real path of the current script: #!/bi...
How to refresh iframe 1. To refresh, you can use j...
Table of contents 1. What is a hook? 2. Why does ...
First, let me show you the finished effect Main i...
I recently used the MySql database when developin...
Import and export of Docker images This article i...
MongoDB installation process and problem records ...
Set Anchor Point <a name="top"><...
When shutting down the MySQL server, various prob...