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

Posted on 2004-10-04
Last Modified: 2008-03-17

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:

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...

Question by:PLSM
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
LVL 10

Expert Comment

ID: 12215568
LVL 10

Expert Comment

ID: 12215574
this link too..

hope this will work for you..


Author Comment

ID: 12215584
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 "winipcfg /batch " & TmpFile, 0, True
  Else "%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 Then' Clear ip address if subnet mask is all zero's
              IP = "0"
              MASK = "0"
            end if
         end if
      end if
  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...
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


Author Comment

ID: 12215650
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...

Accepted Solution

Lycaon earned 150 total points
ID: 12216438
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


Author Comment

ID: 12216497
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)

Expert Comment

ID: 12216533
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
    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

End Function

Expert Comment

ID: 12216544
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.

Expert Comment

ID: 12216575
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.


Expert Comment

ID: 12216588
Whoops.  I see you'd already accepted my comment.  Thanks :)

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
message box in access 4 55
Copy a row 12 75
Set email body to html using vbscript 6 96
Macro problems with Excel file 6 52
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question