Obtain the MAC address of a specified IP Address

Source: Internet
Author: User
Package COM. eleventh. chapter; import Java. io. bufferedreader; import Java. io. inputstreamreader; import Java. util. regEx. matcher; import Java. util. regEx. pattern; public class getmacaddress {public static string callcmd (string [] cmd) {string result = ""; string line = ""; try {process proc = runtime.getruntime(cmd.exe C (CMD ); inputstreamreader is = new inputstreamreader (Proc. getinputstream (); bufferedreader BR = new bufferedreader (is); While (line = Br. Readline ())! = NULL) {result + = line;} catch (exception e) {e. printstacktrace ();} return result ;} /***** @ Param cmd the First Command * @ Param another the second command * @ return the execution result of the second command */public static string callcmd (string [] cmd, string [] Another) {string result = ""; string line = ""; try {runtime RT = runtime. getruntime (); process proc = rt.exe C (CMD); Proc. waitfor (); // after the First Command has been executed, prepare to execute the second command proc = rt.exe C (another); inpu Tstreamreader is = new inputstreamreader (Proc. getinputstream (); bufferedreader BR = new bufferedreader (is); While (line = Br. Readline ())! = NULL) {result + = line;} catch (exception e) {e. printstacktrace ();} return result;}/***** @ Param IP destination IP address, generally, the result string processed by the * @ Param sourcestring command in the LAN * @ Param macseparator Mac separator * @ return MAC address, use the separator number above to indicate */public static string filtermacaddress (final string IP, final string sourcestring, final string macseparator) {string result = ""; string Regexp = "([0-9, A-F, A-F] {}" + macseparator + ") {}) [0-9, A-F, a-F] {1, 2}) "; pattern = pattern. compile (Regexp); matcher = pattern. matcher (sourcestring); While (matcher. find () {result = matcher. group (1); If (sourcestring. indexof (IP) <= sourcestring. lastindexof (matcher. group (1) {break; // if multiple IP addresses exist, only match the MAC address corresponding to this IP address .}} return result;}/***** @ Param IP destination IP address * @ return MAC address **/public static string getmacinwindows (final string IP) {string result = ""; string [] cmd = {"cmd", "/C", "ping" + IP}; string [] Another = {"cmd", "/C ", "ARP-a"}; string cmdresult = callcmd (CMD, another); Result = filtermacaddress (IP, cmdresult, "-"); return result ;} /***** @ Param IP target IP Address * @ return MAC address **/public static string getmacinlinux (final string IP) {string result = ""; string [] cmd = {"/bin/sh", "-c", "ping" + IP + "-C 2 & ARP-"}; string cmdresult = callcmd (CMD); Result = filtermacaddress (IP, cmdresult, ":"); return result ;} /*** obtain the MAC address * @ return returns the MAC address */public static string getmacaddress (string IP) {string macaddress = ""; macaddress = getmacinwindows (IP ). trim (); If (macaddress = NULL | "". equals (macaddress) {macaddress = getmacinlinux (IP ). trim ();} return macaddress;}/*** test */public static void main (string [] ARGs) {system. out. println (getmacaddress ("192.168.1.125 "));}}
Obtain the IP address of the specified DNS: inetaddress A = inetaddress. getbyname (www.test.com );
 
 

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.