how to make autoPingIP System using asp c#
i am trying to write mention code but unable to add Emp_Name column and unable to fetch data from table to Gridview.
namespace Network_auto_ping
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(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)),
new DataColumn("Emp_Name",typeof(string))});
try
{
pingResults.Clear();
List<string> ipAddress = new List<string>();
ipAddress.Add("10.1.246.41");
ipAddress.Add("10.1.246.59");
ipAddress.Add("10.1.246.14");
ipAddress.Add("10.1.150.11");
ipAddress.Add("10.1.246.233");
ipAddress.Add("10.1.246.62");
ipAddress.Add("10.1.246.4");
ipAddress.Add("10.1.246.8");
ipAddress.Add("10.1.246.11");
ipAddress.Add("192.168.200.181");
pingResults.Clear();
List<string> Emp_Name = new List<string>();
Emp_Name.Add("DP Chakarborty");
Emp_Name.Add("Chakarborty");
Emp_Name.Add("Vijay");
Emp_Name.Add("Kumar");
Emp_Name.Add("Pandey");
Emp_Name.Add("Hiraman");
Emp_Name.Add("Singh");
Emp_Name.Add("Indradeo");
Emp_Name.Add("Kumar");
Emp_Name.Add("Engineer(IT)");
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,Emp_Name);
}
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;
}
}
}