如果我知道6个设备的MAC地址,如何修改我的代码以填充IP地址列表框?
我正在使用VB.net向我显示我当前的IP和MAC地址,但我想将其更改为添加到ListBox以使用其MAC地址在同一网络上显示6个设备。由于我们无法修改DHCP服务器,因此我们只想要一种简单的方法来使用已知的Mac地址显示每个设备的IP地址。我将在代码中添加MAC地址。但只是希望在启动应用程序时填充列表框。
现有代码:
Imports System.Net
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports System.IO
Imports System.Net.NetworkInformation
Public Class Form1
Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
Dim mac As String
mac = GetMacAddress()
Label1.Text = mac
End Sub
Function GetMacAddress()
Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
Return nics(0).GetPhysicalAddress.ToString
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GetIPv4Address()
End Sub
Private Function GetIPv4Address() As String
GetIPv4Address = String.Empty
Dim strHostName As String = System.Net.Dns.GetHostName()
Dim iphe As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(strHostName)
For Each ipheal As System.Net.IPAddress In iphe.AddressList
If ipheal.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
GetIPv4Address = ipheal.ToString()
Label2.Text = "IP Address: " & ipheal.ToString
End If
Next
End Function
End Class
提前致谢!
答案 0 :(得分:0)
更新后的答案 - 经过多次挖掘后,我发现here下方代码的未编码版本并进行了一些调整。
您最终得到的是IpInfo
结构列表。这些对象中的每一个都具有IpAddress
,MacAddress
和HostName
的自解释属性。您可以通过列表和IP地址迭代匹配的mac地址到列表框。
您可能需要调整Thread.Sleep
间隔以确保获得所有结果,但我希望这个新答案能够排除您的后果。
如果确实如此,我建议删除关于代码无效的评论,这样他们就不会混淆其他人看到这个答案。
Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Net.Sockets
Public Class Form1
Structure IpInfo
Dim IpAddress As String
Dim HostName As String
Dim MacAddress As String
End Structure
Dim connectedIPAddresses As New List(Of IpInfo)
Private Shared Function NetworkGateway() As String
Dim ip As String = Nothing
For Each f As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
If f.OperationalStatus = OperationalStatus.Up Then
For Each d As GatewayIPAddressInformation In f.GetIPProperties().GatewayAddresses
ip = d.Address.ToString()
Next
End If
Next
Return ip
End Function
Public Sub Ping_all()
Dim gate_ip As String = NetworkGateway()
Dim array As String() = gate_ip.Split("."c)
For i As Integer = 2 To 255
Dim ping_var As String = array(0) & "." & array(1) & "." & array(2) & "." & i.ToString
Ping(ping_var, 1, 1000)
Next
Task.WhenAll(taskList)
End Sub
Dim taskList As New List(Of Task)
Public Sub Ping(ByVal host As String, ByVal attempts As Integer, ByVal timeout As Integer)
For i As Integer = 0 To attempts - 1
taskList.Add(Task.Run(Sub()
Try
Dim ping As System.Net.NetworkInformation.Ping = New System.Net.NetworkInformation.Ping()
AddHandler ping.PingCompleted, AddressOf PingCompleted
ping.SendAsync(host, timeout, host)
Catch
End Try
End Sub))
Next
End Sub
Private Sub PingCompleted(ByVal sender As Object, ByVal e As PingCompletedEventArgs)
Dim ip As String = CStr(e.UserState)
If e.Reply IsNot Nothing AndAlso e.Reply.Status = IPStatus.Success Then
Dim hostname As String = GetHostName(ip)
Dim macaddres As String = GetMacAddress(ip)
Dim newIpAddress As IpInfo
newIpAddress.IpAddress = ip
newIpAddress.MacAddress = macaddres
newIpAddress.HostName = hostname
connectedIPAddresses.Add(newIpAddress)
Else
End If
End Sub
Public Function GetHostName(ByVal ipAddress As String) As String
Try
Dim entry As IPHostEntry = Dns.GetHostEntry(ipAddress)
If entry IsNot Nothing Then
Return entry.HostName
End If
Catch __unusedSocketException1__ As SocketException
End Try
Return Nothing
End Function
Public Function GetMacAddress(ByVal ipAddress As String) As String
Dim macAddress As String = String.Empty
Dim Process As System.Diagnostics.Process = New System.Diagnostics.Process()
Process.StartInfo.FileName = "arp"
Process.StartInfo.Arguments = "-a " & ipAddress
Process.StartInfo.UseShellExecute = False
Process.StartInfo.RedirectStandardOutput = True
Process.StartInfo.CreateNoWindow = True
Process.Start()
Dim strOutput As String = Process.StandardOutput.ReadToEnd()
Dim substrings As String() = strOutput.Split("-"c)
If substrings.Length >= 8 Then
macAddress = substrings(3).Substring(Math.Max(0, substrings(3).Length - 2)) & "-" & substrings(4) & "-" & substrings(5) & "-" & substrings(6) & "-" & substrings(7) & "-" + substrings(8).Substring(0, 2)
Return macAddress
Else
Return "OWN Machine"
End If
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Ping_all()
Threading.Thread.Sleep(10000)
For Each ip As IpInfo In connectedIPAddresses
ListBox1.Items.Add(ip.IpAddress)
Next
End Sub