Solved

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

Posted on 2004-10-04
10
614 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
[X]
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
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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
 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

623 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