Introduction:
1. If you want to know what the/home partition usage is for the tens of thousands of servers you are managing. 2. What do you do if you want to add the same scheduled task to the tens of thousands of servers you manage? 3. What if you want the tens of thousands of servers you are managing to perform the same backup script that you just wrote?
A one-off deal? Of course not. So the most likely thing you can think of is to write a loop that loops through the tens of thousands of machines and performs the same operation. However, if you often need to do such operations, write a few times a day will be too tired of the cycle? What's more, what do you do when the list of hosts you want to loop or the pool of IP addresses is not at the same time?
So, the script in this article is to solve this problem.
---------------------------------------------
Wow, a long time not to write a blog. Two years ago, I was a novice, there are many tools available to achieve a variety of functions, but at the time I did not know that there are these things. So no matter what function you need, write it yourself. There was a need for a lot of servers to do the same operation, and then I did not know that there are puppet and other things, so there is no way, can only write their own scripts.
Now, two years later, the script is still in use, because some of the features puppet cannot be implemented, or cannot be deployed immediately. So the script that originally wrote, now seems to still use the place of martial, so, today is put out for everybody to share.
Because the script was written a long time ago, in disrepair, there are some things that are not applicable to the company's current structure, but in order to demonstrate the function, those have been deprecated features, still posted. Compared with the script I used online, this article has almost no deletions (of course: The company's domain name was replaced by me example.com).
The premise of reading this article is that you have already learned about the new features of BASH4 and that you have a trust relationship between all of your servers.
The schema of this script is:
Execute the commands you want to execute on the console (my: d008.example.com), and on which machines to execute them.
The script has a function to parse the list of hosts specified by the user and get a list of hosts that will eventually execute a command.
For example, m001-m123 indicates that the user is going to execute a command on a total of 123 machines from m001 to m123. The function's function is:
Parse the m001-m123 into a list of hosts such as m001,m002,m003....m123.
Finally, loop through the parsed host list, SSH past and execute the user-specified command. If the user is going to execute a script rather than a normal command, then the script SCP should be passed before execution.
This is basically the idea that when implemented, you can set different options to control the behavior of the script.
I have implemented the following several options (the use of the man page format to do the instructions, the alignment does not very good)
.
Rchange is the script name, deployed in the/bin directory, because it is a batch change n multi-server configuration, so the script is named Rchange
The implemented options are:-a command specifies the commands to execute, and when-A is used, all the remaining positional parameters of the Rchange are part of the command to be executed. The-C command specifies the commands to execute, and when using-C, only the first argument followed by-C is the command to execute, the second and subsequent parameters are the other option for Rchange, and if the command has parameters, enclose the command and argument in the same pair of quotes (single quotation mark)
Describe the difference between-a and-C, as shown in the following example:
Rchange-a ls-l This command represents the command to execute ' ls-l ' because all parameters after-A are identified as part of the command rchange-c ls-l the command represents the command to execute ' ls ', and '-l ' is another option for Rchange, '-l ' Does not belong to the command to be executed, but belongs to Rchange
To avoid ambiguity, it is strongly recommended that the command to be executed is enclosed in a pair of single quotes, not double quotes, because when using double quotation marks, the wildcard character in the command is parsed, which causes the command execution to fail and the script behavior is not expected.
-D debug Meaning, display some debug information, convenient debugging.
-h Specifies a list of hosts to execute commands, you can use '-' to specify a range, such as: m001-m100 represents 100 machines. Multiple hosts are separated by commas, such as: Rchange-h d015,f018-f023,n005-n522
-e except exclusion, in the specified host or IP list to exclude a certain or a certain IP, such as to make m001-m100 (m052) The 99 machines to execute a command, execute the following command: Rchange-h m001-m100-e m052, This will exclude m052 the machine. The exclusion of multiple hosts can be separated by commas, such as: Rchange-h m001-m100-e m052,m067,m091-m-94 This will altogether exclude 6 hosts, and eventually 94 will execute the command you specified.
-H--help Print help-v--version Show program version-X--example Show Use example these 3 nothing to say.
-m (mode) changes the mode of operation. When the same command is executed on many hosts, the script is executed in parallel by default, i.e., at the same time on all machines, and when the-M option is used, the script is changed to serial execution. is to come one after another.
-N (name) prints the host name of the host before executing a command on each host. This option is useful when you want to see what the system's properties are, and he will make the display more user-friendly. When using the-n option, the script's run mode forces the use of serial, because in parallel execution, the host name and program output can not be judged the corresponding relationship, the instant screen is full, there is nothing to see.
-S (script) when using the-S option, the first argument followed by-S will be considered a script, not a normal command, at which time Rchange will put the script SCP to all the hosts to execute the script, and then execute, Rchange the script's extension to determine the interpreter to invoke, such as:. PL will be considered a Perl script,. PY will be identified as a Python script,. PHP will be considered a PHP script, all other will be considered Shell script (bash), of course, you can also be judged by other means, this is how I set up and demonstrate.
There are also the following 3 long options, these 3 options are added after, so when used, do not use other options, if not to use, you must put the long format options in front, the other short options are placed in the back, because I use these several options, the basic does not specify the host list, so do not consider so much.
--MD5 displays the MD5 value of filename in all hosts. This option forces the use of serial mode and forces the print host name. When you use this option, the default host list includes the central control Machine (d008)--sync sync filename from this file so that all the hosts in the file are consistent with the file in the console. --list performs LS-LH on all hosts, viewing the properties of this file in all machines God horse.
The results of--MD5 and--list are as follows: (--sync only synchronous, no output)
Examples of use of other options:
OK, now start to paste code, recently lazy, Chinese comments do not write, the original text has comments, but is written in English, to deal with it, but I also do not use any unfamiliar words, so it should not be difficult to understand.
First of all, the post is/bin/rchange.doc, because the content of the help is also many, so put it in this file alone.
Rchang.doc.part1:
Rchange.doc.part2:
RCHANGE.DOC.PART3:
The following is the function code of the script:
Rchang.part1:rchang.part2:
RCHANG.PART3:
RCHANG.PART4:
RCHANG.PART5:
RCHANG.PART6:
RCHANG.PART7:
Rchang.part8
:
Rchang.part9:
End.
Because the domain name of all the servers in my company is named after the rules of d001-d*** and z001-z***, so I will write the list of parsing hosts according to this rule, if your company server naming rules are different (most people will be different from me), Then re-write a suitable for your company's analytic function can be.
I do not write a lot of comments, but also wrote a few words, should not be able to read it. This article only provides ideas, for informational purposes only.
Shell enables simultaneous operation of multiple servers: Server Batch Management