Obtain the MAC address through the IP address, as if only applicable to LAN

Source: Internet
Author: User

Public class classnetgetmac
{
Public classnetgetmac ()
{
}

[Dllimport ("iphlpapi. dll")]
Private Static extern int sendarp (int32 DEST, int32 host, byte [] Mac, ref int32 length );
[Dllimport ("ws2_32.dll")]
Private Static extern int32 inet_addr (string IP );

/// <Summary>
/// Obtain the nic mac address based on the IP address
/// </Summary>
/// <Param name = "ip"> ip address </param>
/// <Returns> the MAC address of the NIC corresponding to the IP address </returns>
Public static string getmacbyip (string IP)
{
Try
{
Byte [] AA = new byte [6];

Int32 ldest = inet_addr (IP); // ip address of the destination

Int64 macinfo = new int64 ();
Int32 Len = 6;
Int res = sendarp (ldest, 0, AA, ref Len );

Return bitconverter. tostring (AA, 0, 6 );;
}
Catch (exception ERR)
{
Throw err;
}
}

}

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.