java服务器端根据ip获取客户端mac地址
/8/20 22:01:29 475900947 程序员俱乐部 我要评论(1)
摘要://判断系统publicStringcheckSys(){stmt=conn.createStatement();Stringos=System.getProperty("os.name");if(os.startsWith("win")||os.startsWith("Win")){mac=getMacAdr(ip);}else{mac=getMacAdrLinux(ip);}returnmac;}//根据ip获取mac地址
标签:
//判断系统
public String checkSys(){
stmt = conn.createStatement();
String os =System.getProperty("os.name");
if(os.startsWith("win") || os.startsWith("Win")){
mac = getMacAdr(ip);
}else{
mac = getMacAdrLinux(ip);
}
return mac;
}
//根据ip获取mac地址
public static String getMacAdr(String ip){
String str = "";
String macAddress = "";
try{
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++){
str = input.readLine();
if (str != null){
// 英文环境下
if (str.indexOf("MAC Address") > 1){
macAddress=str.substring(str.indexOf("MAC Address")+14,str.length());
break;
}
// 中文环境下
if (str.indexOf("MAC 地址") > 1){
macAddress = str.substring(str.indexOf("MAC 地址") + 14, str.length());
break;
}
}
}
} catch (IOException e){
e.printStackTrace(System.out);
e.toString();
}
return macAddress;
}
//根据ip获取mac地址
public String getMacAdrLinux(String ip){
String macAddress ="";
Process p;
String str = "";
try {
p = Runtime.getRuntime().exec("arp -a " + ip);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++){
str = input.readLine();
if (str != null){
macAddress = str.substring(str.indexOf("at ")+3,str.indexOf("at ")+20);
System.out.println(macAddress);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return macAddress;
}