Gets the MAC address of the terminal for the specified IP

Source: Internet
Author: User



Because the business needs, the company needs to register some terminals to ensure that authorized terminals can log into the business system, the best way is to record the MAC address of each terminal to verify whether there is authorization.

The following is the MAC address of the terminal that gets the specified IP in the form of a calling API:

[DllImport ("Iphlpapi.dll")]
public static extern int Sendarp (Int32 dest, Int32 host, ref Int64 Mac, ref Int32 length);
Dest is the IP of the target machine; Host is the IP of this machine

[DllImport ("Ws2_32.dll")]
public static extern Int32 inet_addr (string IP);

public static string Getnetcardaddress (String strIp)
{
Try
{
Iphostentry host = Dns.gethostbyname (System.Environment.MachineName);
Int32 local = inet_addr (host. Addresslist[0]. ToString ());
Int32 remote = inet_addr (strIp);

Int64 macinfo = new Int64 ();
Int32 length = 6;
Sendarp (remote, local, ref macinfo, ref length);

String temp = System.Convert.ToString (Macinfo, 16). PadLeft (12, ' 0 '). ToUpper ();

StringBuilder Strreturn = new StringBuilder ();
int x = 12;
for (int i=0;i<6;i++)
{
Strreturn.append (temp. Substring (X-2, 2));
x = 2;
}

return strreturn.tostring ();
}
catch (Exception error)
{
throw new Exception (error. message);
}
}

After using the above method for a period of time, it is found that only the MAC address of the terminal that has the same network segment or no route is obtained, while the MAC address of those terminals with different network segments or routes is not properly obtained. The following operating system command method resolves this issue:

public static string GetNetCardAddress2 (String strIp)
{
String mac = "";

System.Diagnostics.Process Process = new System.Diagnostics.Process ();
Process. Startinfo.filename = "nbtstat";
Process. Startinfo.arguments = "-a" +strip;
Process. Startinfo.useshellexecute = false;
Process. Startinfo.createnowindow = true;
Process. Startinfo.redirectstandardoutput = true;

Process. Start ();

string output = process. Standardoutput.readtoend ();
int length = output. IndexOf ("MAC address =");

if (length>0)
{
Mac = output. Substring (length+14, 17);
}

Process. WaitForExit ();

Return Mac. Replace ("-", ""). Trim ();
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.