Execute the same command for multiple Linux hosts

Source: Internet
Author: User

Recently in the knowledge of the Linux cluster, the experiment often installs the same package on a large number of hosts, or add the appropriate configuration information, took a little time to write a shell script, although very poor, but still can meet the needs of the present, if there are other needs later, In considering modifying this script ...


Control Host: VS 192.168.2.131

Controlled host 1:rs1 192.168.2.132

Controlled host 2:rs2 192.168.2.133

#!/bin/bash# #FileName: cluster_cmd## #Performs  the same command in multiple linux  hostread -p  "Please input your command:"  cmdfor i in {rs1, rs2}do  if [ ! ping -c 1 -w 1  $i  &> /dev/ null ];then    echo  "server  $i  is down!"     continue  fi        echo -ne   "\ n"         echo -e  "server  $i  is  running \033[41;1m  $cmd  \033[0m "        ssh  $i   "$cmd" doneif [ ! $1 ==  ""  ];then  case $1 in   -y|y|-y| Y)         echo -ne  "\ n"          echo -e  "server  vs is running \033[41;1m  $cmd  \033[0m "         eval  $cmd   ;;   *)         usage basename $0 [-y|y|-y| y]  ;;   esacfi

There are three things to do before running the script:

1, to solve the trust between the host, the experiment is to use the putty generated public key information distributed to each host, using putty to complete the inter-host call command verification;

2, this script uses the SSH alias mechanism, needs to define the SSH alias in the/etc/ssh/ssh_config file;

3, the corresponding IP address of each host box is defined in the/etc/hosts file.

#############

The last two steps can be skipped if the loop is modified to the IP address of each host.



If the commands that need to be executed are too complex, it is recommended that you write each command to a file, assign the commands to the variable cmd as a string through a while loop, or write a script on the control host, then synchronize to each controlled host, and then execute the script.

This article is from the "Red Mansions Dream" blog, please be sure to keep this source http://leidongya.blog.51cto.com/7375845/1534251

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.