Copy Code code as follows:
Package com.yswc.dao.sign;
Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
/**
*
* Get MAC Address
*
* @author
*
* 2011-12
*
*/
public class Getmacaddress {
public static String Callcmd (string[] cmd) {
String result = "";
String line = "";
try {
Process proc = Runtime.getruntime (). exec (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
* First Order
*
* @param another
* A second command
*
* @return 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.exec (cmd);
Proc.waitfor (); The first command has been executed, ready to execute the second command
proc = Rt.exec (another);
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 IP
* Target IP, generally in the LAN
*
* @param sourcestring
* Command-processed result string
*
* @param macseparator
* Mac Separator Symbol
*
* @return MAC address, indicated by the separator symbol above
*
*/
public static string filtermacaddress (final string IP, final string sourcestring,final string macseparator) {
String result = "";
String RegExp = "(([0-9,a-f,a-f]{1,2}" + Macseparator + ") {1,5}) [0-9,a-f,a-f]{1,2}];
Pattern pattern = pattern.compile (REGEXP);
Matcher Matcher = Pattern.matcher (sourcestring);
while (Matcher.find ()) {
result = Matcher.group (1);
if (Sourcestring.indexof (IP) <= sourcestring.lastindexof (Matcher.group (1)) {
Break If you have more than one IP, match only the Mac for this IP.
}
}
return result;
}
/**
*
*
*
* @param IP
* Target IP
*
* @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
* @return Mac Address
*
*/
public static string Getmacinlinux (final String IP) {
String result = "";
string[] cmd = {"/bin/sh", "-C", "ping" + IP + "-C 2 && arp-a"};
String Cmdresult = callcmd (cmd);
result = Filtermacaddress (Ip,cmdresult, ":");
return result;
}
/**
* Get MAC Address
*
* @return return 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;
}
public static void Main (string[] args) {
String mac=getmacaddress ("192.168.1.102");
System.out.println ("Mac:" +mac);
}
}