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 database table partitioning considerations [recommended]

Table partitioning is different from database par...

Detailed steps to install and uninstall Apache (httpd) service on centos 7

uninstall First, confirm whether it has been inst...

Deep understanding of line-height and vertical-align

Several concepts Line box: A box that wraps an in...

js to implement collision detection

This article example shares the specific code of ...

A brief analysis of the difference between FIND_IN_SET() and IN in MySQL

I used the Mysql FIND_IN_SET function in a projec...

Vue implements accordion effect

This article example shares the specific code of ...

Detailed process of SpringBoot integrating Docker

Table of contents 1. Demo Project 1.1 Interface P...

9 Practical CSS Properties Web Front-end Developers Must Know

1. Rounded Corners Today's web designs are con...

JavaScript mobile H5 image generation solution explanation

Now there are many WeChat public account operatio...

Copy fields between different tables in MySQL

Sometimes, we need to copy a whole column of data...

Thinking about grid design of web pages

<br />Original address: http://andymao.com/a...

Various ways to modify the background image color using CSS3

CSS3 can change the color of pictures. From now o...

Detailed explanation of how to use the Vue date time picker component

This article example shares the specific code of ...