[Shell combat] Execute commands on multiple servers in batches

Function Description: Execute commands on multiple servers in batches

1  # !/bin/bash 
2  
3  # ========================================= == 
4  #Function : Execute commands on multiple servers in batches 
5  #Method : ./batch_cmd_manager.sh <cmd_to_exec> 
6  #Return : The list of results returned by each server executing the command 
7  #Premise : On each server where the command is to be executed 
Password -free login is set8 #Warning  : It    is forbidden to execute commands such as deletion , formatting , etc. that may cause serious consequences 
//blog.csdn.net/zoombinde/article/details/51902208 12 #File : batch_cmd_manager.sh 13 # 
 
 
 
  ==========================================
14 
15 
16 do_command()
17 {
18         hosts=`sed -n '/^[^#]/p' hostlist.txt`
19         for host in $hosts
20                 do
21                         echo ""
22                         echo HOST $host
23                         ssh $host "$@"
24                 done
25         return 0
26 }
27 
28 
29 if[ $ # != 1 ] 
30  then
 31          echo " ======================================= ==== " 
32          echo " Function: Execute commands on multiple servers in batches " 
33          echo " Method: $0 \"<cmd_to_exec>\" " 
34          echo " Return: list of results returned by each server executing commands " 
35          echo " Premise: Password-free login is set on each server where the command is to be executed " 
36          echo " Warning: It is forbidden to execute commands such as deletion and formatting that may cause serious consequences " 
37          echo "=========================================="
38          exit 1
 39  fi
 40  
41  
42 echo " Are you sure you want to execute the command? [yes/no]: $@ " 
43  read to_run
 44  
45 local_ip=10.143.2.4
 46  
47  if [ $to_run = " yes " -o $to_run = " YES " -o $to_run = " y " -o $to_run = " Y " ]
 48  then
 49          echo "" 
50          echo -e " \033[31mExecute command: $@ \033[0m "
51         do_command "$@"
52         echo ""
53         echo HOST $local_ip
54         $@
55 else
56         echo "取消执行命令!"
57 fi
58 
59 echo ""
60 echo "=========================================="

The file hostlist.txt that holds the list of IP addresses:

# List of IP addresses, one xx.xx.xx.xx per line

xx.xx.xx.xxx
xxx.xxx.xx.xx

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325172272&siteId=291194637