0

I have made VB populate Hostname and IPv4 on a Form. However, need help getting Gateway, DNS and DHCP on same form.

Here is code...

Private Sub Getip(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Shown
        Label3.Text = ("Address: " & GetHostIP(Net.Sockets.AddressFamily.InterNetwork)) ' Show IPV4
    End Sub
    Private Function GetHostIP(ByVal af As System.Net.Sockets.AddressFamily) As String
        Dim host As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
        Dim strRet As String = ""
        For Each ip As System.Net.IPAddress In host.AddressList
            If ip.AddressFamily = af Then
                strRet = ip.ToString
                Exit For
            End If
        Next
        Return strRet
    End Function
    Private Sub GetNetworkInfo() Handles Me.Shown
        Dim ipEntry As IPHostEntry = Dns.GetHostEntry(Environment.MachineName)
        Dim IpAddr As IPAddress() = ipEntry.AddressList
        Dim strHostName As String
        'Dim i As Integer
        strHostName = System.Net.Dns.GetHostName()
        Label4.Text = ("Host Name: " & strHostName)
    End Sub

 Public Function GetHostEntryIPv4()
        'Display IP address of local machine
        Dim ipEntry As IPHostEntry = Dns.GetHostEntry(Environment.MachineName)
        Dim ipAddr As IPAddress() = ipEntry.AddressList
        Me.Label3.Text = ("Address: " & ipAddr(0).ToString())
        'Display DNS of local machine
        Dim myiphost As IPHostEntry = Dns.Resolve(UserDomainName)
        Dim myipaddresses() As IPAddress = myiphost.AddressList
        Dim myipaddress As IPAddress
        For Each myipaddress In myipaddresses
            Label6.Text = myipaddress.ToString()
        Next
        'Display DHCP server address of local machine
        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Dim adapter As NetworkInterface
        For Each adapter In adapters
            Dim adapterProperties As IPInterfaceProperties = adapter.GetIPProperties()
            Dim addresses As IPAddressCollection = adapterProperties.DhcpServerAddresses
            If addresses.Count > 0 Then
                MessageBox.Show(adapter.Description)
                Dim address As IPAddress
                For Each address In addresses
                    Label7.Text = address.ToString()
                Next
            End If
        Next
        'Display Default Gateway of local machine
        Dim myNetworkAdapaters() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces
        Dim myadapprops As IPInterfaceProperties = Nothing
        Dim myGateways As GatewayIPAddressInformationCollection = Nothing
        For Each myNetworkAdapter As NetworkInterface In myNetworkAdapaters
            myadapprops = myNetworkAdapter.GetIPProperties
            myGateways = myadapprops.GatewayAddresses
            For Each gateway As GatewayIPAddressInformation In myGateways
                Label8.Text = gateway.Address.ToString()
            Next
        Next
    End Function
2
Contributors
5
Replies
17
Views
3 Years
Discussion Span
Last Post by tinstaafl
0

Did some more thinking on your problem, then realized all the info you want can be gleaned from the ipconfig utility.

Here's some code that will fill a textbox with the connection specific information for the 'Local" connections. This can easily be tailored to whichever connection you want to examine.

Each line has a description so it should be very easy to extract the specific information you want.

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim Proc As New Process
    Dim StartInfo As New ProcessStartInfo
    StartInfo.Arguments = "/all"
    StartInfo.CreateNoWindow = True
    StartInfo.FileName = "ipconfig"
    StartInfo.RedirectStandardOutput = True
    StartInfo.UseShellExecute = False
    Proc.StartInfo = StartInfo
    Proc.Start()
    Dim Output As List(Of String) = Proc.StandardOutput.ReadToEnd.Split(vbNewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries).ToList
    Dim Good As Boolean = False
    For Each s As String In Output
        If s.Contains("Local") Then
            Good = True
        ElseIf Not s.StartsWith(" ") Then
            Good = False
        End If
        If Good Then
            TextBox1.AppendText(s & vbNewLine)
        End If
    Next
End Sub

Edited by tinstaafl

0

tinstaafl:
I had already tried that, however my code was a bit different. It doesnt output what I need. I have attached the results of the textbox1.text for you.
I need the results in IPv4. But thanks for the idea. :)

Tunnel adapter Local Area Connection* 12:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
IPv6 Address. . . . . . . . . . . : 2001:0:4137:9e76:347f:1932:b380:b650(Preferred)
Link-local IPv6 Address . . . . . : fe80::347f:1932:b380:b650%5(Preferred)
Default Gateway . . . . . . . . . : ::
DHCPv6 IAID . . . . . . . . . . . : 83886080
DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-19-67-5B-E6-40-61-86-0F-04-C9
NetBIOS over Tcpip. . . . . . . . : Disabled

0

No I have not, but I will soon. Sorry for the Delay but crazy here.
Would I be able to capture txt from the output so I only display what I want and not the entire result?

0

The return of GetHostEntry is IpHostEntry which contains an array of Ip Addresses property,IPHostEntry.AddressList

Edited by tinstaafl

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.