先申明下:这段代码非本人亲做,但经过测试,是正确可行的,故拿来共飨。其核心在于Process流,其余的无须我多说了。
import java.io.*;
public class GetMac
{
//通过IP获取网卡地址
private 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;
}
//通过机器名获取网卡地址
private 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)
{
GetMac getmac;
getmac=new GetMac();
String mac="";
mac=getmac.getMacAddressIP("192.168.0.18");//YOUR IP
System.out.println(mac);
mac=getmac.getMacAddressName("tom");// YOUR HOST-NAME
System.out.println(mac);
}
}
posted on 2006-01-11 13:06
zjw_albert 阅读(243)
评论(0) 编辑 收藏