1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java 根据ip获取mac地址 java服务器端根据ip获取客户端mac地址

java 根据ip获取mac地址 java服务器端根据ip获取客户端mac地址

时间:2023-03-30 17:01:54

相关推荐

java 根据ip获取mac地址 java服务器端根据ip获取客户端mac地址

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;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。