开发者

Users IP Address when many NIC

开发者 https://www.devze.com 2023-01-21 12:23 出处:网络
I am wanting to get the users IP address (the logged in user will be running the application under there user context on there local PC), but many PC\'s in our environment have multiple NIC\'s that ha

I am wanting to get the users IP address (the logged in user will be running the application under there user context on there local PC), but many PC's in our environment have multiple NIC's that have been added by VMWare Workstation, I would like to exclude these type of bridged connections and only show the "primary" NIC on the PC.

The f开发者_开发百科ollowing function will get the IPv4 address, however on my test PC it is returning the bridged connection and not the IP Address of the network facing NIC.

 Shared Function GetIP(ByVal computerName As String) As String
    'Dim ipEntry As IPHostEntry = Dns.GetHostEntry(computerName)
    'Dim tmpAddr As IPAddress() = ipEntry.AddressList
    Dim ipAddress As String = ""
    'Dim i As Integer = 0
    'Do While i < tmpAddr.Length
    '    If tmpAddr(i).AddressFamily = Sockets.AddressFamily.InterNetwork Then
    '        ipAddress = tmpAddr(i).ToString
    '    End If
    '    i += 1
    'Loop
    Dim ipentry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("")

    For i As Integer = 0 To ipentry.AddressList.Count - 1
        ipAddress = System.Net.Dns.GetHostEntry("").AddressList(i).ToString
    Next

    Return ipAddress
End Function

My users have a mixture of DHCP and static addresses so cannot limit the NIC to either of these connection types. We tend to have a 172.16.x.x IP range, so is there a way to modify the above function so that it will only return a 172.16.x.x address?

Your assistance is greatly appreciated.

Thanks,

Matt


You could always use

If ipAddress.ToString().StartsWith("172.16) Then
  ' Now you've got a match
  ipAddress = tmpAddr(i).ToString()
End If

Of course, my code is bad because you're calling .ToString() twice, which could be a performance drag if there are a lot of addresses to parse, so you may want to modify it slightly...


This might help, or provide hints on other approaches

'Imports System.Net
'get all IP addresses on PC
Dim IPs As System.Net.IPHostEntry = Dns.GetHostEntry("")
'look for IPv4 that starts with 172.16
For Each IPaddr As System.Net.IPAddress In IPs.AddressList
    'check for IPv4
    If IPaddr.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
        If Not System.Net.IPAddress.IsLoopback(IPaddr) Then 'not loopback
            If IPaddr.ToString.StartsWith("172.16") Then
                'found it, see if matching gateway
                Dim adapters() As NetworkInformation.NetworkInterface
                adapters = NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
                For Each adapter As NetworkInformation.NetworkInterface In adapters
                    Dim adapterProperties As NetworkInformation.IPInterfaceProperties = adapter.GetIPProperties()
                    Dim addresses As NetworkInformation.GatewayIPAddressInformationCollection = adapterProperties.GatewayAddresses
                    If addresses.Count > 0 Then
                        Dim addr As NetworkInformation.GatewayIPAddressInformation
                        For Each addr In addresses
                            If addr.Address.ToString.StartsWith("172.16") Then
                                Stop
                            End If
                        Next addr
                    End If
                Next adapter
            End If
        End If
    End If
Next
0

精彩评论

暂无评论...
验证码 换一张
取 消