Solved

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

Posted on 2004-10-04
10
601 Views
Last Modified: 2008-03-17
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
Comment
Question by:PLSM
  • 5
  • 3
  • 2
10 Comments
 
LVL 10

Expert Comment

by:anv
ID: 12215568
0
 
LVL 10

Expert Comment

by:anv
ID: 12215574
this link too..
http://forums.devshed.com/archive/t-63989

hope this will work for you..

:))
0
 

Author Comment

by:PLSM
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
    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
 

Author Comment

by:PLSM
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...
0
 
LVL 1

Accepted Solution

by:
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:PLSM
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)
0
 
LVL 1

Expert Comment

by:Lycaon
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
  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
 
LVL 1

Expert Comment

by:Lycaon
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.
0
 
LVL 1

Expert Comment

by:Lycaon
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.

Lycaon
0
 
LVL 1

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now