Solved

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

Posted on 2004-10-04
10
594 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

708 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

12 Experts available now in Live!

Get 1:1 Help Now