A small example of how Java obtains a client MAC address via IP _java

Source: Internet
Author: User
Tags readline

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

}

Related Article

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.