Asp.net function for retrieving MAC addresses of different CIDR Blocks Based on IP addresses [souzang]

Source: Internet
Author: User

there are many ways to obtain a MAC address from an IP address on the Internet. However, due to the relationship between routers, you can only obtain the MAC address of an IP address in the same network segment, later, I learned a doscommand "NBTSTAT". This command can be used to obtain the MAC from different network segments. However, I have tested that some addresses cannot be obtained, as if I had enabled the firewall, later, I found a piece of Code . The principle is to obtain the returned data based on this command and then use a regular expression to intercept the Mac information segment, as shown below:

// Obtain the MAC address through an IP address (cross-network segment) string getmac (string IP) {string dirresults = ""; processstartinfo psi = new processstartinfo (); process proc = new process (); PSI. filename = "NBTSTAT"; PSI. redirectstandardinput = false; PSI. redirectstandardoutput = true; PSI. arguments = "-a" + IP; PSI. useshellexecute = false; proc = process. start (PSI); dirresults = Proc. standardoutput. readtoend (); Proc. waitforexit (); Dirresults = dirresults. replace ("\ r ",""). replace ("\ n ",""). replace ("\ t", ""); RegEx Reg = new RegEx ("Mac [] {0,} Address [] {0,} = [] {0 ,} (? <Key> ((.)*?)) _ Mac ", regexoptions. ignorecase | regexoptions. compiled); match MC = reg. match (dirresults + "_ Mac"); If (MC. success) {return MC. groups ["key"]. value;} else {Reg = new RegEx ("Host Not Found", regexoptions. ignorecase | regexoptions. compiled); MC = reg. match (dirresults); If (MC. success) {return "host not found! ";}Else {return "";}}}

Asp.net calls the following:

 
// Obtain the Client IP address string IPaddress = request. userhostaddress. tostring (). Trim (); // call the function to obtain the MAC address string macaddress = getmac (IPaddress );
Related Article

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.