none
Get gateway and subnet RRS feed

  • Question

  • I wrote a VB.Net program that you can enter a hostname such as bld019. Then it loops through the various servers and pings them. I get the output I want, but I am trying to figure out how I can get the Subnet and Gateway as well. 

    Below is my code I am using

    Dim ServersXYZ() As String = {"x01", "x02", "x03", "x04", "x05", "x06", "x07"} For Each tserver As String In ServersXYZ If tserver = "01" Then Dim hostname As IPHostEntry = Dns.GetHostByName(txtStore.Text & tserver) Dim ip As IPAddress() = hostname.AddressList txt_IPList.AppendText(ip(0).ToString() & " x01 IP" & vbCrLf) ElseIf tserver = "x02" Then Dim hostname As IPHostEntry = Dns.GetHostByName(txtStore.Text & tserver) Dim ip As IPAddress() = hostname.AddressList txt_IPList.AppendText(ip(0).ToString() & " x02 IP" & vbCrLf) ElseIf tserver = "x03" Then Dim hostname As IPHostEntry = Dns.GetHostByName(txtStore.Text & tserver) Dim ip As IPAddress() = hostname.AddressList txt_IPList.AppendText(ip(0).ToString() & " x03 IP" & vbCrLf) ElseIf tserver = "x04" Then Dim hostname As IPHostEntry = Dns.GetHostByName(txtStore.Text & tserver) Dim ip As IPAddress() = hostname.AddressList txt_IPList.AppendText(ip(0).ToString() & " x04 IP" & vbCrLf) ElseIf tserver = "x05" Then Dim hostname As IPHostEntry = Dns.GetHostByName(txtStore.Text & tserver) Dim ip As IPAddress() = hostname.AddressList txt_IPList.AppendText(ip(0).ToString() & " x05 IP" & vbCrLf) ElseIf tserver = "x06" Then Dim hostname As IPHostEntry = Dns.GetHostByName(txtStore.Text & tserver) Dim ip As IPAddress() = hostname.AddressList txt_IPList.AppendText(ip(0).ToString() & " x06 IP" & vbCrLf) ElseIf tserver = "x07" Then Dim hostname As IPHostEntry = Dns.GetHostByName(txtStore.Text & tserver) Dim ip As IPAddress() = hostname.AddressList txt_IPList.AppendText(ip(0).ToString() & " x07 IP" & vbCrLf) End If txtStatus.AppendText("Started " & txtStore.Text & tserver & vbCrLf) Dim ps As New Process With ps.StartInfo .FileName = "cmd.exe" .Arguments = "/C ping " & txtStore.Text & tserver .CreateNoWindow = True .UseShellExecute = False .RedirectStandardOutput = True End With

    ps.Start() ps.WaitForExit() Dim output As String = ps.StandardOutput.ReadToEnd()

    txtStatus.AppendText(output & vbCrLf) txtStatus.AppendText("Finished " & txtStore.Text & tserver & vbCrLf & vbCrLf & vbCrLf) ps.Close() Next


    Saturday, January 11, 2020 5:12 PM

All replies

  • Hi Broggy69, 

    Thank you for posting here.

    For your question, you want to get gateway and subnet in VB.NET.

    I make a test on my side and you can refer to the following code.

            Dim hostName As String = System.Net.Dns.GetHostName()
            For Each ip In System.Net.Dns.GetHostEntry(hostName).AddressList
                If ip.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
                    'IPv4 Adress
                    Dim ipv4 As String = ip.ToString()
                    For Each adapter As System.Net.NetworkInformation.NetworkInterface In System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
                        For Each unicastIPAddressInformation As System.Net.NetworkInformation.UnicastIPAddressInformation In adapter.GetIPProperties().UnicastAddresses
                            If unicastIPAddressInformation.Address.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
                                If ip.Equals(unicastIPAddressInformation.Address) Then
                                    'Subnet Mask
                                    Dim Subnet As String = unicastIPAddressInformation.IPv4Mask.ToString()
                                    Dim adapterProperties As System.Net.NetworkInformation.IPInterfaceProperties = adapter.GetIPProperties()
    
                                    For Each gateway As System.Net.NetworkInformation.GatewayIPAddressInformation In adapterProperties.GatewayAddresses
                                        'Default Gateway
                                        Dim gatewaystr As String = gateway.Address.ToString()
                                    Next
                                End If
                            End If
                        Next
                    Next
                End If
            Next
    

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 13, 2020 9:10 AM
    Moderator