Get All IP Addresses over a network

There's probably a load of different ways to do this. This one in particular uses the console, had to convert it from C#, so I hope it works, see how you get on....

VB.NET:
Private Function GetIPAddresses() As ArrayList
 Dim Addresses As ArrayList =  New ArrayList()  ' Temp array to fill
 'Get my address
 String MyAdd = Dns.GetHostByName(Dns.GetHostName()).AddressList(0).ToString()
  
 ' Used to start the command
 Dim psi As New System.Diagnostics.ProcessStartInfo()
       
 'prompt
 psi.FileName = "C:\WINDOWS\system32\cmd.exe" ' Path for the cmd prompt
 psi.Arguments = "/c net view > boo.txt" ' Arguments for the command prompt
  psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
  
 Application.DoEvents()
 ' So the computer doesn't bog down
 Dim Run As System.Diagnostics.Process.Start(psi)
 Dim sr As System.IO.StreamReader =  Nothing  
 Dim run As Boolean =  False  ' used to tell if dos is done yet
 While run = False
  Application.DoEvents()
  Try
   System.Threading.Thread.Sleep(1000)
   sr = New System.IO.StreamReader(Application.StartupPath 
                   + "\\boo.txt") ' Try to open the file
   ' If dos is not done yet, this will throw an error
   run = True ' If I get here, dos is done and we can use the file
  Catch
   run = False ' Dos is not done yet
  End Try
 End While
 ' Cycle through the file until the info starts
 While sr.ReadLine().StartsWith("--") <> True
   Application.DoEvents()
 End While
 Dim str As String =  ""  ' filled with each line of the file
 Dim comp() As String =  New String(32)  ' temp array of computer names
 Dim i As Integer =  0  ' indexer for comp[]
 ' cycle until we reach, "The command completed successfully."
 While str.StartsWith("The") <> True
   Application.DoEvents()
   str = sr.ReadLine() ' Read the next line
  ' Split the line after the first space
   comp(i) = str.Split(Char.Parse(" "))(0)
   ' Normally, in the file it lists it like this
   ' \\MyComputer                 My Computer's Description
 
  ' Take off the slashes, "\\MyComputer" to "MyComputer"
   comp(i) = comp(i).Substring(2, comp(i).Length - 2)
   If comp(i) = "e" Then
     Application.DoEvents()
     comp(i) = Nothing
   End If
   i += 1
 End While
 sr.Close()
 sr = Nothing ' free memory
 Dim s As String
 For Each s In comp
  If Not s Is Nothing Then
   If s.ToUpper() <> Dns.GetHostName().ToUpper() Then
    Addresses.Add(Dns.GetHostByName(s).AddressList(0).ToString()) 
  ' ... add the address to the array
   End If
  End If
 Next
 Return Addresses ' return the array
End Function
 
They may not be totally reliable neither of them, I get results with both methods that the other don't show. Also neither return Vista hosts I know exists (when checking from a XP host).
 
Back
Top