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