Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBscript - Check IP Range for Multiple NICs

Status
Not open for further replies.

dtidti

Technical User
Jun 15, 2011
5
BE
Hello,

I'm trying to set up a script to have the ip range checked on clients.
If they fall into a certain range, the script must execute some batch file.

This is working as long as the client only has one network card installed.
For example, it works perfectly on my own workstation but as soon as I had a loopback adapter for example, it no longer works.

In other words, it only seems to look at first interface it can find.

Any idea what I need to change or add to make the script look at all available network interfaces?

Thank you in advance.

Here's the afformentioned script.
Note I do not have that much scripting experience and kind of hacked this together from parts of other scripts I could find.

Dim Wmi :Set Wmi = GetObject("winmgmts:\\.\root\CIMV2")
Dim Ip, Z1, Z2
Dim shell
set shell=createobject("wscript.shell")
For Each Obj in Wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration")
If not isNull(Obj.IPSubnet) Then
If not isNull(Obj.IPAddress) Then
Z1 = Join(Obj.IPAddress, ",")
If Len(Z1) <= 15 Then
Ip = Z1
ElseIf Len(Z1) >= 16 Then
Z2 = Split(Z1, ",")
Ip = Z2(0)
End If
End If
End If
Next
If Ip > "194.7.23.200" And Ip < "194.7.23.254" Then
shell.run "RUN BATCH1"
ElseIf Ip > "192.168.180.100" And Ip < "192.168.180.199" Then
shell.run "RUN BATCH2"
ElseIf Ip > "192.168.180.100" And Ip < "192.168.180.199" Then
shell.run "RUN BATCH3"
End If
set shell=nothing
WScript.Quit
 
retrieve only those that have an IP address.

Wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration [green]where IPEnabled=1",,48[/green])

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
I tried adding "where IPEnabled=1",,48" but this gives the same results.
As soon as the network interface I need to check is no longer first in the "ipconfig" list, the script no longer works.

There is also no error message.
 
To give you a better overview, below you can find examples of working and non-working ip configurations of a workstation.
This is all using the same script.

WORKING IPCONFIG A
==================

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\username>ipconfig

Windows IP Configuration


Ethernet adapter Local Area Connection 2 (loopback with static IP):

Connection-specific DNS Suffix . :
IPv4 Address. . . . . . . . . . . : 194.7.23.201
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :

Ethernet adapter Local Area Connection (Network Interface that needs to be checked):

Connection-specific DNS Suffix . : domainname.local
IPv4 Address. . . . . . . . . . . : 194.7.23.241
Subnet Mask . . . . . . . . . . . : 255.255.255.128
Default Gateway . . . . . . . . . : 194.7.23.129

C:\Users\username>

WORKING IPCONFIG B
==================

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\username>ipconfig

Windows IP Configuration

Ethernet adapter Local Area Connection (Network Interface that needs to be checked):

Connection-specific DNS Suffix . : domainname.local
IPv4 Address. . . . . . . . . . . : 194.7.23.241
Subnet Mask . . . . . . . . . . . : 255.255.255.128
Default Gateway . . . . . . . . . : 194.7.23.129

C:\Users\username>


NON WORKING IPCONFIG A
======================

C:\Users\username>ipconfig

Windows IP Configuration


Ethernet adapter Local Area Connection 2 (loopback with static IP):

Connection-specific DNS Suffix . :
IPv4 Address. . . . . . . . . . . : 194.7.23.100
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :

Ethernet adapter Local Area Connection (Network Interface that needs to be checked):

Connection-specific DNS Suffix . : domainname.local
IPv4 Address. . . . . . . . . . . : 194.7.23.241
Subnet Mask . . . . . . . . . . . : 255.255.255.128
Default Gateway . . . . . . . . . : 194.7.23.129

C:\Users\username>

NON WORKING IPCONFIG B
======================

C:\Users\username>ipconfig

Windows IP Configuration


Ethernet adapter Local Area Connection 2 (loopback with Auto Config IP):

Connection-specific DNS Suffix . :
Autoconfiguration IPv4 Address. . : 169.254.1.185
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :

Ethernet adapter Local Area Connection (Network Interface that needs to be checked):

Connection-specific DNS Suffix . : domainname.local
IPv4 Address. . . . . . . . . . . : 194.7.23.241
Subnet Mask . . . . . . . . . . . : 255.255.255.128
Default Gateway . . . . . . . . . : 194.7.23.129

C:\Users\username>
 
The way the script is written, it will only execute the if..then AFTER the for..loop iterates the NICs. You need to include your
if..then inside the for..next loop

Code:
For Each Obj in Wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration")
   If not isNull(Obj.IPSubnet) Then
      If not isNull(Obj.IPAddress) Then
         Z1 = Join(Obj.IPAddress, ",")
         If Len(Z1) <= 15 Then
            Ip = Z1
         ElseIf Len(Z1) >= 16 Then
            Z2 = Split(Z1, ",")
            Ip = Z2(0)
         End If
      End If
   End If
   If Ip > "194.7.23.200" And Ip < "194.7.23.254" Then
      shell.run "RUN BATCH1"
   ElseIf Ip > "192.168.180.100" And Ip < "192.168.180.199" Then
      shell.run "RUN BATCH2"
   ElseIf Ip > "192.168.180.100" And Ip < "192.168.180.199" Then
      shell.run "RUN BATCH3"
   End If
[green]Next[/green]

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thanks for the explanation.
The "if..then" is already inbetween my "for..next" isn't it?

I'm not sure I understand what you mean, could you please clarify?

Thank you
 
Look at the original code. The if..then that determines which batch file to run is outside the for..next. Therefore, it will only occur once. If it is moved inside the for..next, then it will occur with each iteration of the loop.

Although, I'm not sure it is going to result in what you want. Could you restate your objective?

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Woops, I looked over that the first time.
I changed the script and it seems to work, atleast on the workstation I'm testing it on.

Still not sure how this change makes a difference between it only running once or for all available interfaces.

Anyway, where do I send the beers? :)
 
The code inside the for..loop is run 5 times. You will get 5 msgbox

Code:
for i = 1 to 5
   x = x + i
   msgbox x
next

Move the msgbox outside of the loop and you will only get one msgbox.
Code:
for i = 1 to 5
   x = x + 1
next
msgbox x

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top