[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 619
  • Last Modified:

VBS: How to find IP addresses on specific subnet on machines with several IP's

Hi,

I need some help working out how I can find particular IP addresses, belonging to a particular subnet, on machines with several IP addresses.

For example, I have a machine with 3 adaptors:

192.0.0.0
10.0.0.0
172.0.0.0

If I use GetIP it will find 192. I want to find 10.

I did think that I could base it on finding the first octet. 'If FirstOctet = "10" then'.... something like that, but I don't know exactly how.

How do I do that in VBS.

Thanks in advance...

Brgds,
PLSM
0
PLSM
Asked:
PLSM
  • 5
  • 3
  • 2
1 Solution
 
anvCommented:
0
 
anvCommented:
this link too..
http://forums.devshed.com/archive/t-63989

hope this will work for you..

:))
0
 
PLSMAuthor Commented:
Sorry, when I said GetIP, I meant:
------------------------------------------------------------
Function GetIP()
  Dim ws : Set ws = CreateObject("WScript.Shell")
  Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
  Dim TmpFile : TmpFile = fso.GetSpecialFolder(2) & "/ip.txt"
  Dim ThisLine, IP, Mask
  IP = "0"
  MASK = "0"
  If ws.Environment("SYSTEM")("OS") = "" Then
    ws.run "winipcfg /batch " & TmpFile, 0, True
  Else
    ws.run "%comspec% /c ipconfig > " & TmpFile, 0, True
  End If
  With fso.GetFile(TmpFile).OpenAsTextStream
    Do While NOT .AtEndOfStream
      ThisLine = .ReadLine
      if IP = "0" then ' Logic to make it read the 1st IP address if the machine has multiple IP's
         If InStr(ThisLine, "Address") <> 0 Then IP = Mid(ThisLine, InStr(ThisLine, ":") + 2)
       end if
      if MASK = "0" then ' IP address read. Check for valid subnet mask
         If InStr(ThisLine, "Mask") <> 0 Then
           MASK = Mid(ThisLine, InStr(ThisLine, ":") + 2)
            if InStr(MASK, "0.0.0.0") <> 0 Then' Clear ip address if subnet mask is all zero's
              IP = "0"
              MASK = "0"
            end if
         end if
      end if
    Loop
    .Close
  End With
  'WinXP (NT? 2K?) leaves a carriage return at the end of line
  If IP <> "" Then
    If Asc(Right(IP, 1)) = 13 Then IP = Left(IP, Len(IP) - 1)
  End If
------------------------------------------------------------------------
I'll look through your links...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
PLSMAuthor Commented:
Unfortunately none of the links give information I do not already have. They tell me how to find the first adaptors ip address, but not how to specify which which adaptor i want to read the ip from, depending on the first octet...
0
 
LycaonCommented:
Hey PLSM...

This might help you, though since I don't fully understand what you're trying to do, it's just a shot in the dark.

Under this line:

If InStr(ThisLine, "Address") <> 0 Then IP = Mid(ThisLine, InStr(ThisLine, ":") + 2)


You can use something like:
If Split(IP, ".")(0) = "10" Then
 ' You found your 10.x.x.x IP address, do whatever here
End If

0
 
PLSMAuthor Commented:
As it happens, that was just about what I did:

      If InStr(ThisLine, "IP Address") <> 0 Then                
                     If InStr(ThisLine, "10.") <> 0 Then                                     
                           IP = Mid(ThisLine, InStr(ThisLine, ":") + 2)
0
 
LycaonCommented:
Actually, try this:

Function GetIP(FirstOctet)
  Dim WS, FSO, TmpFile, File, Line, IP, Mask

  Set WS = CreateObject("WScript.Shell")
  Set FSO = CreateObject("Scripting.FileSystemObject")
  TmpFile = FSO.GetSpecialFolder(2) & "\ip.txt"

  If WS.Environment("SYSTEM")("OS") = "" Then
    WS.Run "winipcfg /batch " & TmpFile, 0, True
  Else
    WS.Run "%comspec% /c ipconfig > " & TmpFile, 0, True
  End If

  Set File = FSO.GetFile(TmpFile).OpenAsTextStream

    Do Until InStr(1, Line, "IP Address") <> 0 Or File.AtEndOfStream

      Line = File.ReadLine

      If InStr(1, Line, "IP Address") <> 0 Then
        IP = Mid(Line, InStr(Line, ":") + 2)
        If InStr(1, IP, Chr(13)) Then IP = Left(IP, Len(IP) - 1) ' Removes Chr(13) on NT systems
        If Split(IP, ".")(0) = FirstOctet Then GetIP = IP: Exit Function
     End If
    Loop

  File.Close
End Function
0
 
LycaonCommented:
Ah, you post early too :p

I tried it on my machine here, with a 64., 192. and a 127. address.  I used GetIP("64"), GetIP("192"), and GetIP("127") and it returned the correct IP all three times.
0
 
LycaonCommented:
Heh, sorry, it's early and I left out a line in the code.

After this line:  If Split(IP, ".")(0) = FirstOctet Then GetIP = IP: Exit Function

Add this:  Line = ""

And I meant to type "172" not "127", as a 127 address does not show up in an ipconfig.

Lycaon
0
 
LycaonCommented:
Whoops.  I see you'd already accepted my comment.  Thanks :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now