quickly ping a range of ip addresses

Lider

Member
Joined
Mar 12, 2021
Messages
9
Programming Experience
Beginner
Good day! Tell me how to quickly ping a range of ip addresses with Resp output. Time, HostName, Mac? The form has a datagridview 2 text boxes and a button. Enter the range of ip addresses in the text fields, click on the button and the datagridview is filled.

there is the following code, which in a split second will include the number of active devices on the network. how is it possible to remake it for my task?



code:
Private BaseIP As String = "192.168.1."
Private StartIP As Integer = 1
Private StopIP As Integer = 255
Private ip As String

Private timeout As Integer = 100
Private nFound As Integer = 0

Private Shared lockObj As New Object()
Private stopWatch As New Stopwatch()
Private ts As TimeSpan

Public Async Sub RunPingSweep_Async()
    nFound = 0

    Dim tasks = New List(Of Task)()

    stopWatch.Start()

    For i As Integer = StartIP To StopIP
        ip = BaseIP & i.ToString()

        Dim p As New System.Net.NetworkInformation.Ping()
        Dim task = PingAndUpdateAsync(p, ip)
        tasks.Add(task)
    Next i

    Await Task.WhenAll(tasks).ContinueWith(Sub(t)
        stopWatch.Stop()
        ts = stopWatch.Elapsed
        MessageBox.Show(nFound.ToString() & " devices found! Elapsed time: " & ts.ToString(), "Asynchronous")
    End Sub)
End Sub

Private Async Function PingAndUpdateAsync(ByVal ping As System.Net.NetworkInformation.Ping, ByVal ip As String) As Task
    Dim reply = Await ping.SendPingAsync(ip, timeout)

    If reply.Status = System.Net.NetworkInformation.IPStatus.Success Then
        SyncLock lockObj
            nFound += 1
        End SyncLock
    End If
End Function
 
After a while, I got the following code:


VB.NET:
Private Async Function Button1_ClickAsync(sender As Object, e As EventArgs) As Task Handles Button1.Click
        Await Ping("192.168.0.1", "255")
    End Function
    Private Async Function Ping(startIP As String, endIP As String) As Task
        Dim start As IPAddress = IPAddress.Parse(startIP)
        Dim bytes = start.GetAddressBytes()
        Dim leastSigByte = start.GetAddressBytes().Last
        Dim range = endIP - leastSigByte


        Dim pingReplyTasks = Enumerable.Range(leastSigByte, range).Select(Function(x)
                                                                              Dim p = New Ping()
                                                                              Dim bb = start.GetAddressBytes()
                                                                              bb(3) = CByte(x)
                                                                              Dim destIp = New IPAddress(bb)
                                                                              Dim pingResultTask = p.SendPingAsync(destIp)
                                                                              Return New With {
        Key pingResultTask,
        Key .addr = destIp
    }
                                                                          End Function).ToList()

        Await Task.WhenAll(pingReplyTasks.Select(Function(x) x.pingResultTask))

        For Each pr In pingReplyTasks
            Dim tsk = pr.pingResultTask
            Dim pingResult = tsk.Result
            Dim ip = pr.addr
            DataGridView1.Rows.Add(ip, pingResult.RoundtripTime, pingResult.Status)

        Next pr
    End Function

But I want the host name and mac address to be displayed in the list.
Can someone tell me how to fix the code?
 
Last edited:
Back
Top