Ping Multiple of IP Address using C# and VB.Net in ASP.Net

Last Reply 7 months ago By dharmendr

Posted 7 months ago

Ping Multiple of ips at sametime by using c#

Posted 7 months ago

Hi MithunKmr,

Check this example. Now please take its reference and correct your code.

HTML

<asp:Button Text="Save" runat="server" OnClick="Save" />
<br /><br />
<asp:GridView runat="server" ID="GridView1" />

Namespaces

C#

using System.Collections.Generic;
using System.Data;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;

VB.Net

Imports System.Collections.Generic
Imports System.Data
Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Threading

Code

C#

protected void Save(object sender, EventArgs e)
{
    DataTable pingResults = new DataTable();
    pingResults.Columns.AddRange(new DataColumn[] { 
                        new DataColumn("Date", typeof(DateTime)),
                        new DataColumn("IP", typeof(string)),
                        new DataColumn("MacAddress",typeof(string)),
                        new DataColumn("MachineName",typeof(string)),
                        new DataColumn("Message",typeof(string))});
    try
    {
        pingResults.Clear();
        List<string> ipAddress = new List<string>();
        ipAddress.Add("192.168.0.1");
        ipAddress.Add("192.168.0.2");
        ipAddress.Add("192.168.0.3");
        for (int i = 0; i < ipAddress.Count; i++)
        {
            Ping ping = new Ping();
            PingReply pingReply = ping.Send(ipAddress[i].ToString());
            string message = (pingReply.Status == IPStatus.Success) ? "On" : "Off";
            lock (pingResults.Rows.SyncRoot)
            {
                pingResults.Rows.Add(DateTime.Now, ipAddress[i], GetMacAddress(ipAddress[i]), GetMachineName(ipAddress[i]), message);
            }
            Thread.Sleep(1000);
        }
        GridView1.DataSource = pingResults;
        GridView1.DataBind();

    }
    catch (Exception ex)
    {

    }
}

public string GetMacAddress(string ipAddress)
{
    string macAddress = string.Empty;
    System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
    pProcess.StartInfo.FileName = "arp";
    pProcess.StartInfo.Arguments = "-a " + ipAddress;
    pProcess.StartInfo.UseShellExecute = false;
    pProcess.StartInfo.RedirectStandardOutput = true;
    pProcess.StartInfo.CreateNoWindow = true;
    pProcess.Start();
    string strOutput = pProcess.StandardOutput.ReadToEnd();
    string[] substrings = strOutput.Split('-');
    if (substrings.Length >= 8)
    {
        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 "not found";
    }
}

private string GetMachineName(string ipAdress)
{
    string machineName = string.Empty;
    try
    {
        IPHostEntry hostEntry = Dns.GetHostEntry(ipAdress);
        machineName = hostEntry.HostName;
    }
    catch (Exception ex)
    {
            
    }
    return machineName;
}

VB.Net

Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
    Dim pingResults As DataTable = New DataTable()
    pingResults.Columns.AddRange(New DataColumn() {
                                 New DataColumn("Date", GetType(DateTime)),
                                 New DataColumn("IP", GetType(String)),
                                 New DataColumn("MacAddress", GetType(String)),
                                 New DataColumn("MachineName", GetType(String)),
                                 New DataColumn("Message", GetType(String))})
    Try
        pingResults.Clear()
        Dim ipAddress As List(Of String) = New List(Of String)()
        ipAddress.Add("192.168.0.1")
        ipAddress.Add("192.168.0.2")
        ipAddress.Add("192.168.0.3")
        For i As Integer = 0 To ipAddress.Count - 1
            Dim ping As Ping = New Ping()
            Dim pingReply As PingReply = ping.Send(ipAddress(i).ToString())
            Dim message As String = If((pingReply.Status = IPStatus.Success), "On", "Off")
            SyncLock pingResults.Rows.SyncRoot
                pingResults.Rows.Add(DateTime.Now, ipAddress(i), GetMacAddress(ipAddress(i)), GetMachineName(ipAddress(i)), message)
            End SyncLock
            Thread.Sleep(1000)
        Next
        GridView1.DataSource = pingResults
        GridView1.DataBind()
    Catch ex As Exception
    End Try
End Sub

Public Function GetMacAddress(ByVal ipAddress As String) As String
    Dim macAddress As String = String.Empty
    Dim pProcess As System.Diagnostics.Process = New System.Diagnostics.Process()
    pProcess.StartInfo.FileName = "arp"
    pProcess.StartInfo.Arguments = "-a " & ipAddress
    pProcess.StartInfo.UseShellExecute = False
    pProcess.StartInfo.RedirectStandardOutput = True
    pProcess.StartInfo.CreateNoWindow = True
    pProcess.Start()
    Dim strOutput As String = pProcess.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 "not found"
    End If
End Function

Private Function GetMachineName(ByVal ipAdress As String) As String
    Dim machineName As String = String.Empty
    Try
        Dim hostEntry As IPHostEntry = Dns.GetHostEntry(ipAdress)
        machineName = hostEntry.HostName
    Catch ex As Exception
    End Try
    Return machineName
End Function

Screenshot