In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use vbs to run a script for an IP address range, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Q:
I want to run scripts on all computers in a subnet. Is there a way to do this without hard-coding all IP addresses into scripts?
-- RB
A:
According to your description in the email, it looks like your setting is similar: the IP address range for your subnet is 192.168.1.1 to 192.168.1.254. If you want to create a script, you can start with the first IP address, run some code on the appropriate computer, then go to the second address, run the same code, and run the same script on each computer in address order. In addition, you want to do this with as few lines of code as possible without having to hard-code hundreds of IP addresses.
So, is there a way to do that? Of course there is, and it's easier than you think.
First, let's show you how to loop a range of IP addresses. This is a sample code, so it is only used to echo the name of each IP address. After introducing how the script works, we will give a more practical example:
The code is as follows:
On Error Resume Next
IntStartingAddress = 1
IntEndingAddress = 254
StrSubnet = "192.168.1."
For I = intStartingAddress to intEndingAddress
StrComputer = strSubnet & I
Wscript.Echo strComputer
Next
No, really, this is the whole script. We first assigned some variables: assign the value "1" to intStartingAddress;, assign the value "254" to intEndingAddress; and assign the value "192.168.1." Assigned to strSubnet. Pay attention to the English full stop after "1". As you may have guessed, these values will be used as building blocks for establishing IP addresses.
After assigning these variables, we create a For-Next loop to run variables from 1 (intStartingAddress) to 254 (intEndingAddress). Why cycle 1 to 254? The reason is simple: that's your IP scope. What if the IP range is 192.168.1.7 to 109.168.1.54? No problem: use the same loop, but change the value of intStartingAddress to "7" and the value of intEndingAddress to "54".
In this loop, we put the string value 192. 168. 1. Concatenated with the current value of the loop variable (I). On the first run of the loop-when "I" equals "1", we will be 192.168.1. Combined with 1. Guess what? We get the value 192.168.1.1, which happens to be the first IP address. The last time we run the loop, we will put 192.168.1. Combine with 254to get the value of the last IP address-192.168.1.254. If you run the script, you will get:
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
Isn't it very simple?
Of course, you may not be interested in echoing a set of IP addresses; you want to run some kind of WMI code. All right:
On Error Resume Next
IntStartingAddress = 1
IntEndingAddress = 254
StrSubnet = "192.168.1."
For I = intStartingAddress to intEndingAddress
StrComputer = strSubnet & I
Set objWMIService = GetObject _
("winmgmts:\" & strComputer & "\ root\ cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_OperatingSystem")
For Each objItem in ColItems
Wscript.Echo strComputer & ":" & objItem.Caption
Next
Next
As you can see, we once again set the value of the strComputer variable to an IP address. Then connect to the WMI service on the computer represented by that address. This is easy to do because WMI can use either a computer name or an IP address to connect to the computer.
Now, we're going to add a little trick. You mentioned in your email that you want to exclude several IP addresses (which may represent a router or something else). Yes. Here is a modified script that uses the Select Case statement to exclude some computers:
IntEndingAddress = 254
StrSubnet = "192.168.1."
For I = intStartingAddress to intEndingAddress
Select Case i
Case 10
Case 50
Case 100
Case Else
StrComputer = strSubnet & I
Set objWMIService = GetObject _
("winmgmts:\" & strComputer & "\ root\ cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_OperatingSystem")
For Each objItem in ColItems
Wscript.Echo strComputer & ":" & objItem.Caption
Next
End Select
Next
Notice what happens when the "I" value equals 10, 50, or 100. That's right: it hasn't changed at all. If the computer's IP address is 192.168.1.10, 192.168.1.50, or 192.168.1.100, nothing will change; no WMI code will run, and the script will execute the loop as usual. This WMI code will be executed only on computers that have other IP addresses (except the above three). This is a simple and effective way to exclude specific IP addresses from the WMI section of the script.
The above is all the contents of the article "how to use vbs to run a script against an IP address range". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.