Java gets the MAC address of the client

Source: Internet
Author: User

Today, there is a need, is the background of the log to record the visitor's detailed access record information, including the environment information, as for the browser information, IP of these can be obtained from the request , but this client's MAC address need to write a method to get, I wrote a method, now take out and share with you, younger brother coding level is limited, what design unreasonable place, hope you big God do not spray.

The code is as follows:

Package Com.app.archive.util;import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.util.regex.matcher;import java.util.regex.pattern;/** * @e-mail:[email protected] * @author Luckyboy * @QQ : 263235040 */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; }/** * @e-mail:[email protected] * @author luckyboy * @QQ: 263235040 */Publ          IC Static String callcmd (string[] cmd,string[] another) { String result = "";           String line = "";               try {Runtime RT = Runtime.getruntime ();               Process proc = rt.exec (cmd);               Proc.waitfor ();               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; }/** * @e-mail:[email protected] * @author luckyboy * @QQ: 263235040 */Public static string filtermacaddress (final string IP, final string sourcestring,final string macseparator) {string re           Sult = "";           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;       }} return result; }/** * @e-mail:[email protected] * @author luckyboy * @QQ: 263235040 */P           Ublic 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; }/** * @e-mail:[email protected] * @author Luckyboy *@QQ: 263235040 */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; }/** * @e-mail:[email protected] * @author luckyboy * @QQ: 263235040 */Public STA         Tic 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) {System.out.println (getmacaddress ("127.0.0.1")); }    }

Test results:


Java gets the MAC address of the client

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.