CentOS uses expect to remotely execute scripts and commands in batches

CentOS uses expect to remotely execute scripts and commands in batches

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:
  • Solution to the "syntax error: unexpected end of file" message when running .sh script in CentOS7
  • Use crontab to run the script of executing jar program regularly in centOS6
  • CentOS 6.5 configuration ssh key-free login to execute pssh command explanation
  • How to execute PHP scheduled tasks in CentOS7
  • Detailed explanation of using crontab to execute tasks regularly under CentOS 7
  • Solve the problem that /etc/rc.local does not execute when starting centos7
  • How to set up a scheduled task to execute a specified script in centos

<<:  Example analysis of mysql user rights management

>>:  Teach you how to subcontract uniapp and mini-programs (pictures and text)

Recommend

MySQL max_allowed_packet setting

max_allowed_packet is a parameter in MySQL that i...

How to handle long data when displaying it in html

When displaying long data in HTML, you can cut off...

Detailed explanation of the basic knowledge of front-end componentization

Table of contents Basic concepts of components Th...

nginx proxy_cache batch cache clearing script introduction

Preface: I used the official nginx proxy_cache as...

Bootstrap3.0 study notes table related

This article mainly explains tables, which are no...

Vue uses canvas handwriting input to recognize Chinese

Effect picture: Preface: Recently, I was working ...

How to get the real path of the current script in Linux

1. Get the real path of the current script: #!/bi...

The iframe refresh method is more convenient

How to refresh iframe 1. To refresh, you can use j...

Common usage of hook in react

Table of contents 1. What is a hook? 2. Why does ...

Pure CSS to achieve the effect of picture blinds display example

First, let me show you the finished effect Main i...

How to configure the My.ini file when installing MySQL5.6.17 database

I recently used the MySql database when developin...

Docker image import and export code examples

Import and export of Docker images This article i...

Linux installation MongoDB startup and common problem solving

MongoDB installation process and problem records ...

Application of anchor points in HTML

Set Anchor Point <a name="top"><...

How to safely shut down MySQL

When shutting down the MySQL server, various prob...