' Scans all local network interfaces and pings devices on each subnet
Public Sub ScanAllNetworks
Log("🔍 Starting full network scan...")
' Get a list of (IP, interfaceName) pairs for all active IPv4 interfaces
Dim Interfaces As List = GetLocalIPs
If Interfaces.Size = 0 Then
Log("⚠️ No valid local interfaces found.")
Return
End If
For Each entry() As String In Interfaces
Dim ip As String = entry(0).Trim
Dim ifaceName As String = entry(1).Trim
Log($"🌐 Scanning IP: ${ip} (Interface: ${ifaceName})"$)
' Split the IP string on the literal dot using Regex
Dim parts() As String = Regex.Split("\.", ip)
If parts.Length = 4 Then
Dim subnet As String = $"${parts(0)}.${parts(1)}.${parts(2)}."$
Log("📡 Subnet: " & subnet)
ScanSubnet(subnet)
Else
Log("⚠️ Invalid IP format: " & ip)
End If
Next
Log("✅ All subnet scans completed.")
End Sub
' Scans a given /24 subnet by pinging addresses .1 to .254
Private Sub ScanSubnet(subnet As String)
For i = 1 To 254
Dim targetIp As String = subnet & i
CheckHost(targetIp, 300, False)
Next
For i = 1 To 254
Wait For Address_Checked (HostAddress As String, Reachable As Boolean, HostName As String)
If Reachable Then Log("🟢 Reachable: " & HostAddress)
Next
End Sub
' Calls Java code asynchronously to ping one IP
Private Sub CheckHost(Ip As String, Timeout As Int, GetHostName As Boolean)
Dim joInet As JavaObject
joInet.InitializeStatic("java.net.InetAddress")
Dim address As JavaObject = joInet.RunMethod("getByName", Array(Ip))
Dim jMe As JavaObject = Me
jMe.RunMethod("checkAddress", Array(address, Timeout, GetHostName))
End Sub
' Returns a list of (IP, interfaceName) for each active IPv4 interface
Private Sub GetLocalIPs As List
Dim result As List
result.Initialize
Dim niClass As JavaObject
niClass.InitializeStatic("java.net.NetworkInterface")
Dim enumIfs As JavaObject = niClass.RunMethod("getNetworkInterfaces", Null)
Do While enumIfs.RunMethod("hasMoreElements", Null)
Dim intf As JavaObject = enumIfs.RunMethod("nextElement", Null)
If intf.RunMethod("isUp", Null) = False Then Continue
If intf.RunMethod("isLoopback", Null) = True Then Continue
Dim enumAddrs As JavaObject = intf.RunMethod("getInetAddresses", Null)
Do While enumAddrs.RunMethod("hasMoreElements", Null)
Dim addr As JavaObject = enumAddrs.RunMethod("nextElement", Null)
Dim ip As String = addr.RunMethod("getHostAddress", Null)
If ip.StartsWith("127.") Then Continue
If ip.Contains(":") Then Continue ' Skip IPv6
result.Add(Array As String(ip, intf.RunMethod("getName", Null)))
Loop
Loop
Return result
End Sub
#If Java
// Asynchronously checks reachability of an IP and raises Address_Checked event
public void checkAddress(java.net.InetAddress address, int timeout, boolean getHostName) {
final String hostAddress = address.getHostAddress();
BA.runAsync(getBA(), null, "address_checked", new Object[] {false, hostAddress, ""}, new java.util.concurrent.Callable<Object[]>() {
public Object[] call() throws Exception {
if (address.isReachable(timeout))
return new Object[] {hostAddress, true, getHostName ? address.getHostName() : ""};
else
return new Object[] {hostAddress, false, ""};
}
});
}
#End If