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 );