Tip of the day: Find network interfaces by name
Function FindNetworkInterface(name as string) As NetworkInterface
name = name.trim
if name.len = 0 then Return nil
// search by IP/MAC
dim u as integer = System.NetworkInterfaceCount-1
for i as integer = 0 to u
dim n as NetworkInterface = System.GetNetworkInterface(i)
if n.IPAddress = name or n.MACAddress = name then
Return n
end if
next
// use MBS Plugin to build a mapping
dim interfaces() as NetworkInterfaceMBS = NetworkInterfaceMBS.AllInterfaces
dim map as new Dictionary
for each n as NetworkInterfaceMBS in interfaces
dim IPv4s() as string = n.IPv4s
dim IPv6s() as string = n.IPv6s
for each IPv4 as string in IPv4s
map.Value(IPv4) = n.Name
next
for each IPv6 as string in IPv6s
map.Value(IPv6) = n.Name
next
if n.MAC<>"" then
map.Value(n.MAC) = n.Name
end if
next
// now search interfaces by name, IPv4 or IPv6
for i as integer = 0 to u
dim n as NetworkInterface = System.GetNetworkInterface(i)
if map.Lookup(n.IPAddress, "") = name then
Return n
end if
if map.Lookup(n.MACAddress, "") = name then
Return n
end if
next
End Function