java获取服务器端网卡地址
来源:互联网 发布:幼儿园大班弄的美工 编辑:程序博客网 时间:2024/04/28 15:32
package com.ctgusec.bean;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
/**
*
* @author zhupan
* @version 1.0
*/
public class MACAddress {
public MACAddress() {
}
public static String getMACAddress() {
String address = "";
String os = System.getProperty("os.name");
if (os != null && os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
System.out.println(address);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
}
}
return address;
}
// 通过IP获取网卡地址
public static String getMacAddressIP(String remotePcIP) {
String str = "";
String macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -A " + remotePcIP);
InputStreamReader ir = new InputStreamReader(pp.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;
}
}
}
} catch (IOException ex) {
}
return macAddress;
}
// 通过机器名获取网卡地址
public static String getMacAddressName(String remotePcIP) {
String str = "";
String macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -a " + remotePcIP);
InputStreamReader ir = new InputStreamReader(pp.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;
}
}
}
} catch (IOException ex) {
}
return macAddress;
}
public static void main(String[] args) {
System.out.println(MACAddress.getMACAddress());
System.out.println(getMacAddressIP("192.168.175.200"));
System.out.println(getMacAddressName("527fefbedd5b43b."));
}
}
package com.ctgusec.bean;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
/**
*
* @author zhupan
* @version 1.0
*/
public class GetMACAddress {
public String getMACAddress(String ipAddress) {
String str = "", strMAC = "", macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.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) {
strMAC = str.substring(str.indexOf("MAC Address") + 14,
str.length());
break;
}
}
}
} catch (IOException ex) {
return "Can't Get MAC Address!";
}
//
if (strMAC.length() < 17) {
return "Error!";
}
macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)
+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)
+ ":" + strMAC.substring(12, 14) + ":"
+ strMAC.substring(15, 17);
//
return macAddress;
}
public static void main(String[] args) {
GetMACAddress getMACAddress = new GetMACAddress();
System.out.println(getMACAddress.getMACAddress("192.168.175.66"));
try {
java.lang.Process proc = Runtime.getRuntime().exec("ipconfig /all");
InputStream istr = proc.getInputStream();
byte[] data = new byte[1024];
istr.read(data);
String netdata = new String(data);
System.out.println("Your Mac Address=" + procAll(netdata));
} catch (IOException e) {
System.out.println("error=" + e);
}
}
public static String procAll(String str) {
return procStringEnd(procFirstMac(procAddress(str)));
}
public static String procAddress(String str) {
int indexof = str.indexOf("Physical Address");
if (indexof > 0) {
return str.substring(indexof, str.length());
}
return str;
}
public static String procFirstMac(String str) {
int indexof = str.indexOf(":");
if (indexof > 0) {
return str.substring(indexof + 1, str.length()).trim();
}
return str;
}
public static String procStringEnd(String str) {
int indexof = str.indexOf("/r");
if (indexof > 0) {
return str.substring(0, indexof).trim();
}
return str;
}
}
- java获取服务器端网卡地址
- java获取服务器端网卡地址
- java获取服务器端网卡地址
- java程序获取网卡地址
- php获取服务器端机器网卡的物理(MAC)地址
- java如何获取网卡地址(硬件地址)
- java服务器端获取客户端IP地址
- java获取所有网卡和ip地址
- 纯Java代码获取网卡MAC地址
- JAVA获取本机网卡IP地址
- java获取网卡的mac地址
- java获取网卡的mac地址
- java通过ip获取网卡MAC地址
- 获取网卡MAC地址
- 获取网卡MAC地址
- C#获取网卡地址
- 获取网卡Mac地址
- 获取网卡MAC地址
- 每天一句(二十一)
- 你不得不遵守的十二大著名法则
- 一个软件测试人员的经验分享
- 不用爱情的婚姻
- 易懂易用的MD5加密(可直接运行)
- java获取服务器端网卡地址
- Eclipse快捷键指南
- 精妙SQL
- 春节后的第一个东东,DirectDisk
- Spring学习以及测试程序笔记
- 扬州美女能否走出传说产生经济价值?
- vs2005 sp1 添加的对smart device native(c++) 支持(support) 新类, 如CSpliterWnd
- eclipsework学习笔记
- JSP错误:JSPG0227E: JSPG0123E