Method One: Separate the native address from the other machines in the local area network
/** * Obtain MAC address based on IP address * @param ipaddress 127.0.0.1 * @return * @throws socketexception * @throws unknownhost
Exception */public static string Getlocalmac (String ipaddress) throws SocketException, Unknownhostexception {
TODO auto-generated method stub String str = "";
String macAddress = "";
Final String loopback_address = "127.0.0.1";
If 127.0.0.1, the local MAC address is obtained.
if (Loopback_address.equals (IPAddress)) {inetaddress inetaddress = Inetaddress.getlocalhost ();
Looks like this method needs JDK1.6.
Byte[] mac = Networkinterface.getbyinetaddress (inetaddress). Gethardwareaddress ();
The following code is to assemble the MAC address into a string StringBuilder sb = new StringBuilder ();
for (int i = 0; i < mac.length i++) {if (i!= 0) {sb.append ("-");
}//Mac[i] & 0xFF is to convert the byte to a positive integer String s = integer.tohexstring (Mac[i] & 0xFF);
Sb.append (s.length () = = 1 0 + s:s); }//Put string all lowercaseChange the letter to uppercase to become a regular MAC address and return macAddress = sb.tostring (). Trim (). toUpperCase ();
return macAddress;
else {//Get MAC address of non-local IP try {System.out.println (ipaddress);
Process p = runtime.getruntime (). EXEC ("nbtstat-a" + ipaddress);
System.out.println ("===process==" +p);
InputStreamReader ir = new InputStreamReader (P.getinputstream ());
BufferedReader br = new BufferedReader (IR); while ((str = br.readline ())!= null) {if (Str.indexof ("MAC") >1) {macAddress = str.substring (str.
IndexOf ("MAC") +9, Str.length ());
MacAddress = Macaddress.trim ();
System.out.println ("macAddress:" + macAddress);
Break
} P.destroy ();
Br.close ();
Ir.close ();
The catch (IOException ex) {} return macAddress;
}
}
Let's take another look at method two:
Package com.alpha.test;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.LineNumberReader; public class Getmac {/** * Java gets the MAC address of the client network card * * @param args/public static void main (string[] args) {Ge
TMAC get = new Getmac ();
System.out.println ("1=" +get.getmac ());
System.out.println ("2=" +get.getmac ("127.0.0.1"));
//1. Get the client IP address (this must be uploaded from the client background)://JSP page, very simple, request.getremoteaddr (); Because the view layer of the system is implemented in JSF, there is no direct access to a similar request on the page, a cast//public String Getmyip () {//try {//Facescontext FC = F
Acescontext.getcurrentinstance ();
HttpServletRequest request = (HttpServletRequest) FC//. Getexternalcontext (). Getrequest ();
return request.getremoteaddr ();
catch (Exception e) {//E.printstacktrace ();
}//Return ""; //2. Get the client MAC address//Call Window command, in the background bean implementation through IP to obtain the MAC address.
The method is as follows://Run Speed "fast" public String Getmac () {string mac = null;
try { Process Pro = Runtime.getruntime (). EXEC ("cmd.exe/c ipconfig/all");
InputStream is = Pro.getinputstream ();
BufferedReader br = new BufferedReader (new InputStreamReader (IS));
String message = Br.readline ();
int index =-1; while (message!= null) {if (index = Message.indexof ("Physical Address") > 0) {mac = message.substring (Ind
Ex + +). Trim ();
Break
} message = Br.readline ();
} System.out.println (MAC);
Br.close ();
Pro.destroy ();
catch (IOException e) {System.out.println ("Can ' t get mac address!");
return null;
} return Mac;
}//Run speed "slow" public String getmac (string IP) {string str = NULL;
String macAddress = null;
try {Process p = runtime.getruntime (). EXEC ("nbtstat-a" + IP);
InputStreamReader ir = new InputStreamReader (P.getinputstream ());
LineNumberReader input = new LineNumberReader (IR); for (; true;)
{str = input.readline (); if (str!= null) {if Str.indexof ("MAC addresS ") > 1) {macAddress = str. SUBSTRING (Str.indexof (" MAC address ") + 14);
Break
catch (IOException e) {e.printstacktrace (System.out);
return null;
return macAddress; }
}
Method Three, more streamlined
import java.io.IOException; import java.io.InputStreamReader; import
Java.io.LineNumberReader;
public class MACAddress {String IP;
String mac;
Public MACAddress (String IP) {this.ip = IP;
Public MACAddress () {This.ip = "0.0.0.0";
Public String Getmac () {try {Process P = runtime.getruntime (). EXEC ("arp-n");
InputStreamReader ir = new InputStreamReader (P.getinputstream ());
LineNumberReader input = new LineNumberReader (IR);
P.waitfor ();
Boolean flag = true;
String ipstr = "(" + This.ip + ")";
while (flag) {String str = input.readline ();
if (str!= null) {if (Str.indexof (IPSTR) > 1) {int temp = Str.indexof ("at");
This.mac = str.substring (temp + 3, temp + 20);
Break
} else flag = false; catch (IOException |
Interruptedexception e) {e.printstacktrace (System.out);
return THIS.MAC;
public void setIp (String IP) {this.ip = IP; }
}
Finally to enlarge the recruit, small friends look carefully OH
The first thing to say is that this method can support the MAC address acquisition of the extranet machine.
I used to get one. Only LAN access. There is a firewall can not access, but this do not worry about.
Tested the IP of Baidu, already can obtain MAC address
Java gets MAC address via IP-IP-sealed MAC address
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) {St
Ring 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 command * @param another second command * @return The execution result of the second command * * public static St
Ring 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 Destination IP, generally in LAN * @param result string sourcestring command processing * @param Macseparator mac Separator symbol * @return MAC address, with the above separator symbol */public static string filtermacaddress (final string IP, final string SourceS
Tring,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 there are multiple IPs, match only the Mac for this IP.
} return result; /** * * @param IP Destination IP * @return MAC address */public static String GETMACINW
indows (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 Destination IP * @return MAC address */public static String Getmacinlinux (final Strin
G 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 Macad
Dress = "";
macAddress = Getmacinwindows (IP). Trim (); if (macaddress==null| | "".
Equals (macAddress)) {macAddress = Getmacinlinux (IP). Trim ();
return macAddress;
//do a test public static void main (string[] args) {System.out.println (getmacaddress ("220.181.111.148"));
}
}