Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell implements batch execution of the same command on multiple windows servers and gets the returned results

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

1. demand

In the operation and maintenance of windows server, if you want to view the current host name, resource usage, software installation, etc., how do you operate it, is to log in and click next to view, or through command? It seemed like an order. It was more professional. However, if you want to view this information for 50, 100 or more servers, it seems that it is not so simple.

Have you ever thought that you can view the host name, resource usage, software installation and other information of multiple Windows servers by typing a command for more than ten seconds; or you can start or stop the system services of N Windows servers by clicking a command, restart\shutdown, modify the registry, etc. Most of the operations you can think of, after simple deployment in the early stage, you can manage multiple Windows servers at the same time by only one command. Here's how to make that happen.

2. Principle of implementation and demonstration 2.1 Principle

Through the script program I wrote under the shell, using the paramiko module in Python, verify the login information of windows through the ssh protocol, and then call PowerShell of windows to implement the command execution. Windows Server2008R2 and Windows Server 2012R2 have been tested and can run normally.

2.2 Show 2 simple examples: After executing the script, enter the PowerShell command of windows

Query Host Name

View CPU Usage

As long as it is PowerShell supported, it can be executed in batches. I'm only testing two of them here, and of course 100 of them will give the same result.

3. Prepare 3.1 A Centos system: I use the Centos 6.6 virtual machine to execute scripts and then execute windows commands. Python is required (usually already installed)

3.2 Software:FREESSHD can be downloaded from the official website, mainly installed on the managed Windows server, to achieve ssh verification

3.3 Software:SecureCRT or Xshell for connecting to Centos virtual machines

3.4 Optional software:WinSCP If you need to distribute files, you need to install this software on the windows server.

4. Implementation steps and examples 4.1 Install fresshd and winscp on each windows server (winscp optional): I used 2 windows virtual machines in the test

Winscp and FREESSHD installation directly default to the next step;

After Winscp installation, execute the command in powershell to cache the host key.

cd 'C:\Program Files (x86)\WinSCP\' ; ./ winscp. exe/console /command 'option batch continue' 'option confirm off' 'open sftp://root:root password @centos host IP:centos host ssh port

Press Y after execution, as shown in the figure:

Freshd needs to be set up slightly after installation, as shown in the figure:

The first run of freshd is shown in the figure

Then click the freshshd icon in the lower right corner to set the relevant settings. In the SSH tab, set the Command shell to C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe as shown in the figure:

Click "Add…" in the Users tab

Add the administrator account of windows server in the pop-up window, and then mark Shell as shown in the figure:

Finally, you can choose whether to turn off automatic updates in Automatic updates. I usually choose to turn it off.

After setting up, type "services.msc" in Start-Run to restart the freshd service

Sshd is set on the windows server. You can test whether the sshd service of windows is normal through telnet ssh port, such as telnet X.22.

4.2 Security settings (optional): ipsec configured on each windows server only allows our centos server to connect to port 22 of sshd, so as to avoid other unauthorized ssh connections. It is recommended to set it (skip the following steps if you do not set it or will set it).

Start-Run Type "gpedit.msc" to enter Windows Settings-Security Settings-IP Security Policy, and create a policy on the local computer:

First set the allowed hosts, here we need to add centos hosts to allow, such as partial screenshots, no screenshot steps click "Next" by default:

After completing the "OK", then start to establish a policy that rejects any address (source address: any IP address) to the windows local address sshd port, as shown in the figure:

After determining the security settings are complete, then assign the policy to take effect, as shown in the figure:

If the firewall is enabled, you also need to set the firewall to allow the fresshd program to pass through the firewall. After setting, you can test whether the connection is OK by telnet X.X. X (Windows Server IP) 22 on the centos server, as shown in the figure:

4.3 script is ready

1) Download the shell script later in this article

2) Upload the script to the centos server, command rz and then select the script upload (without rz command, install it by yum install -y lrzz command), as shown in the figure:

3) The vim run_win_same_cmd.sh command modifies the script, adding IP, username and password of each server to be managed later, as shown in the figure, and then saving it.

Note that the order of ip,user,pwd is one-to-one correspondence, and the corresponding error is not verified:

You can also modify it in windows and upload it to centos server, but it is best to run dos2unix command conversion.

4.4 run command

1) Run sh run_win_same_cmd.sh in centos server (note that the script path is correct), as shown in the figure, prompt to select after running, we choose 1. Run windows command in batches, prompt to enter windows command after entering, here we use a simple command hostname to view, as shown in the figure:

For a more complicated command (of course, this kind of long command can be copied and pasted directly), we need to check the remaining disk space of each of the N servers:

If you are not familiar with powershell commands, you can refer to some commonly used Powershell operation and maintenance commands in the links below. Of course, more powerful commands can be found according to your needs.

Link: Windows PowerShell Operation and Maintenance Personnel Common Command Collection and Arrangement

2) If you need to distribute files or software, you can select 2. Batch distribute files to windows after running the script, which can realize batch distribution of files to various windows servers, as shown in the figure:

Check on windows server that the file has been copied

5. Script download address

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report