B4J Question Scanning Local Network

tufanv

Expert
Licensed User
Longtime User
HEllo,

I searched the forum but couldn't find a related topic. IS there any lib or method that help to scan a local network for connected pc information , ip etc ?

thx
 

Daestrum

Expert
Licensed User
Longtime User
You can use Shell to run arp -a then parse the output for the connections on the network.
You can get the hostname , ip address and mac address of things on the network, and also if the ip is static or dynamic.

It's not instant as it has to talk to the connections. (needs javaobject and shell libraries)
B4X:
Sub Process_Globals
    Dim sh As Shell
    Dim inet As JavaObject
End Sub

Sub AppStart (Args() As String)
    inet.InitializeStatic("java.net.InetAddress")
    sh.Initialize("sh","arp",Array As String("-a"))
    sh.Run(8000)
    StartMessageLoop
End Sub
Sub sh_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success Then
        For Each line As String In Regex.Split(Chr(10),StdOut)
            If line.Trim = "" Then Continue
            If line.Trim.Contains("dynamic") Or line.trim.Contains("static") Then
                Dim address As String = Regex.Split(" ",line.Trim)(0)
                Dim baddress() As String = Regex.Split("\.",address.trim)
                Dim addrs(4) As Byte
                For t = 0 To 3
                    addrs(t) = baddress(t)
                Next
                Dim host As String = inet.RunMethodJO("getByAddress",Array(addrs)).RunMethod("getCanonicalHostName",Null)
                If host = address Then host="Unknown"
                Dim addType As String
                If line.Trim.Contains("dynamic") Then
                    addType = "dynamic"
                Else
                    addType = "static"   
                End If
                Log("Host Name : ["&host&"] IP Address : "&address&" Mac Address : <"&Regex.Split("[ ]{2,}",line.Trim)(1)&">"&"  Type : "&addType)
            End If
        Next
   
    End If
    StopMessageLoop   
End Sub

I get the following result from my network
Waiting for debugger to connect...
Program started.
Host Name : [Unknown] IP Address : 192.168.0.1 Mac Address : <40-0d-10-42-a6-b0> Type : dynamic
Host Name : [GOFLEX_HOME] IP Address : 192.168.0.11 Mac Address : <00-10-75-2c-a1-6b> Type : dynamic
Host Name : [Unknown] IP Address : 192.168.0.13 Mac Address : <1c-7b-21-8a-12-96> Type : dynamic
Host Name : [Unknown] IP Address : 192.168.0.15 Mac Address : <c4-2f-ae-39-99-97> Type : dynamic
Host Name : [Unknown] IP Address : 192.168.0.17 Mac Address : <ac-af-b9-d2-36-be> Type : dynamic
Host Name : [Unknown] IP Address : 192.168.0.18 Mac Address : <28-3f-69-cb-6f-e4> Type : dynamic
Host Name : [LAPTOP-45QTON8S] IP Address : 192.168.0.22 Mac Address : <28-c6-3f-53-59-f8> Type : dynamic
Host Name : [Unknown] IP Address : 192.168.0.26 Mac Address : <2c-95-69-02-b1-2e> Type : dynamic
Host Name : [MYCLOUD-RTA50R] IP Address : 192.168.0.27 Mac Address : <00-14-ee-02-d4-2b> Type : dynamic
Host Name : [Unknown] IP Address : 192.168.0.28 Mac Address : <38-8c-50-b7-45-49> Type : dynamic
Host Name : [Unknown] IP Address : 192.168.0.29 Mac Address : <20-df-b9-c4-d0-67> Type : dynamic
Host Name : [HP40A98D] IP Address : 192.168.0.30 Mac Address : <ac-e2-d3-40-a9-8d> Type : dynamic
Host Name : [Unknown] IP Address : 192.168.0.44 Mac Address : <20-c6-eb-73-37-aa> Type : dynamic
Host Name : [Unknown] IP Address : 192.168.0.255 Mac Address : <ff-ff-ff-ff-ff-ff> Type : static
Host Name : [igmp.mcast.net] IP Address : 224.0.0.22 Mac Address : <01-00-5e-00-00-16> Type : static
Host Name : [Unknown] IP Address : 224.0.0.252 Mac Address : <01-00-5e-00-00-fc> Type : static
Host Name : [hp-device-disc.mcast.net] IP Address : 224.0.1.60 Mac Address : <01-00-5e-00-01-3c> Type : static
Host Name : [Unknown] IP Address : 239.255.255.250 Mac Address : <01-00-5e-7f-ff-fa> Type : static
Host Name : [Unknown] IP Address : 255.255.255.255 Mac Address : <ff-ff-ff-ff-ff-ff> Type : static
Program terminated (StartMessageLoop was not called).
 
Last edited:
Upvote 0

ST500

Active Member
Licensed User
You can use Shell to run arp -a then parse the output for the connections on the network.
You can get the hostname , ip address and mac address of things on the network, and also if the ip is static or dynamic.

It's not instant as it has to talk to the connections. (needs javaobject and shell libraries)
B4X:
Sub Process_Globals
    Dim sh As Shell
    Dim inet As JavaObject
End Sub

Sub AppStart (Args() As String)
    inet.InitializeStatic("java.net.InetAddress")
    sh.Initialize("sh","arp",Array As String("-a"))
    sh.Run(8000)
    StartMessageLoop
End Sub
Sub sh_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success Then
        For Each line As String In Regex.Split(Chr(10),StdOut)
            If line.Trim = "" Then Continue
            If line.Trim.Contains("dynamic") Or line.trim.Contains("static") Then
                Dim address As String = Regex.Split(" ",line.Trim)(0)
                Dim baddress() As String = Regex.Split("\.",address.trim)
                Dim addrs(4) As Byte
                For t = 0 To 3
                    addrs(t) = baddress(t)
                Next
                Dim host As String = inet.RunMethodJO("getByAddress",Array(addrs)).RunMethod("getCanonicalHostName",Null)
                If host = address Then host="Unknown"
                Dim addType As String
                If line.Trim.Contains("dynamic") Then
                    addType = "dynamic"
                Else
                    addType = "static"  
                End If
                Log("Host Name : ["&host&"] IP Address : "&address&" Mac Address : <"&Regex.Split("[ ]{2,}",line.Trim)(1)&">"&"  Type : "&addType)
            End If
        Next
  
    End If
    StopMessageLoop  
End Sub

I get the following result from my network
Does it work in B4a?
 
Upvote 0
Top