Wednesday, September 30, 2009

Get all IP addresses on LAN

this is a simple vb.net code to retrieve all ip addresses on your local network.
remember to import System.Net.


Public Shared Function GetAllIPADDRESSES(Optional ByVal args As String() = Nothing) As Integer
'args in the signature is optional, without it
'the function will simply get the hostname
'of the local machine then go from there

Dim strHostName As New String("")
If args.Length = 0 Then
' Getting Ip address of local machine...
' First get the host name of local machine.
strHostName = DNS.GetHostName()
Console.WriteLine("Local Machine's Host Name: " + strHostName)
Else
strHostName = args(0)
End If

' Then using host name, get the IP address list..
Dim ipEntry As IPHostEntry = DNS.GetHostByName(strHostName)
Dim addr As IPAddress() = ipEntry.AddressList

Dim i As Integer = 0
While i < addr.Length
Console.WriteLine("IP Address {0}: {1} ", i, addr(i).ToString())
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
Return 0
End Function

No comments: