Get MAC Address of any machine from IP
This is kind of a hack because it usess NBTSTAT under the hood, but it doesn't rely on WMI or anything else
This is kind of a hack because it usess NBTSTAT under the hood, but it doesn't rely
on WMI or anything else:
using System;
using System.Diagnostics
;
namespace ConsoleApplication1{
public class MacAddress
{
public MacAddress()
{
}
public string GetMac(string
IP)
{
string str1=String.Empty;
try
{
string
str2=String.Empty;
ProcessStartInfo info1 = new ProcessStartInfo();
Process
process1 = new Process();
info1.FileName = "nbtstat";
info1.RedirectStandardInput
= false;
info1.RedirectStandardOutput = true;
info1.Arguments =
"-A " + IP;
info1.UseShellExecute = false;
process1 =
Process.Start(info1);
int num1 = -1;
while (num1 <= -1)
{
num1 = str2.Trim().ToLower().IndexOf("mac address", 0);
if
(num1 > -1)
{
break;
}
str2 = process1.StandardOutput.ReadLine();
}
process1.WaitForExit();
str1 = str2.Trim();
}
catch
(Exception exception2)
{
throw exception2;
}
return
str1;
}
}
class Tester
{
[STAThread]
static
void Main(string[] args)
{
// ensure the ip address is on the command
line at first argument
string ip = args[0];
MacAddress m= new MacAddress();
string mac=m.GetMac(ip);
Console.WriteLine(mac);
Console.ReadLine();
}
}
}
Submission Date: 9/23/2005
3:13:16 PM
Submitted By: Peter Bromberg
My Home Page: http://www.eggheadcafe.com
By Peter Bromberg Popularity (2115 Views)