在JAVA中如何获取本机的网卡物理地址?

syuhai 2002-08-05 09:09:15
在JAVA中如何获取本机的网卡物理地址?
...全文
297 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuzzysoft 2002-08-05
  • 打赏
  • 举报
回复
关注:这个问题很有用
xingxing 2002-08-05
  • 打赏
  • 举报
回复
调用c代码吧!!!
lifeis 2002-08-05
  • 打赏
  • 举报
回复

Host Name . . . . . . . . . . . . : mars
Primary DNS Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : worksoft.com.cn

PPP adapter 263:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : WAN (PPP/SLIP) I
Physical Address. . . . . . . . . : 00-53-45-00-00-0
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 61.135.2.27
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 61.135.2.27
DNS Servers . . . . . . . . . . . : 202.106.196.152
202.106.196.115




加粗部分是网卡的物理地址

我们只要在java中执行这个外部命令,然后把需要的字符串解析出来就可以得到当前机器的物理网卡地址了。
首先编写一个Util_syscmd类,方法execute()使我们能够执行外部命令,并返回一个Vector类型的结果集。



import java.lang.*;
import java.io.*;
import java.util.*;

/**
* @author Jackliu
*
*/
public class Util_syscmd{

/**
* @param shellCommand
*
*/
public Vector execute(String shellCommand){
try{
Start(shellCommand);
Vector vResult=new Vector();
DataInputStream in=new DataInputStream(p.getInputStream());
BufferedReader reader=new BufferedReader(new

InputStreamReader(in));

String line;
do{
line=reader.readLine();
if (line==null){
break;
}
else{
vResult.addElement(line);
}
}while(true);
reader.close();
return vResult;

}catch(Exception e){
//error
return null;
}
}
/**
* @param shellCommand
*
*/
public void Start(String shellCommand){
try{
if(p!=null){
kill();
}
Runtime sys= Runtime.getRuntime();
p=sys.exec(shellCommand);
}catch(Exception e){
System.out.println(e.toString());
}
}
/**
kill this process
*/
public void kill(){
if(p!=null){
p.destroy();
p=null;
}
}

Process p;
}






然后设计一个GetPhysicalAddress类,这个类用来调用Util_syscmd类的execute()方法,执行cmd.exe/c ipconfig/all命令,并解析出网卡物理ip地址。getPhysicalAddress()方法返回网卡的物理地址,如果机器中没有安装网卡或操作系统不支持ipconfig命令,返回not find字符串




import java.io.*;
import java.util.*;

class GetPhysicalAddress{
//网卡物理地址长度
static private final int _physicalLength =16;

public static void main(String[] args){
//output you computer phycail ip address
System.out.println(getPhysicalAddress());
}

static public String getPhysicalAddress(){
Util_syscmd shell =new Util_syscmd();
String cmd = "cmd.exe /c ipconfig/all";
Vector result ;
result=shell.execute(cmd);
return parseCmd(result.toString());
}

//从字符串中解析出所需要获得的字符串
static private String parseCmd(String s){
String find="Physical Address. . . . . . . . . :";
int findIndex=s.indexOf(find);
if (findIndex==-1)
return "not find";
else
return

s.substring(findIndex+find.length()+1,findIndex+find.length()+1+_physicalLength);


}
}


lifeis 2002-08-05
  • 打赏
  • 举报
回复
我也不知道,但在C中是有办法的,用socket直接就可以,也可以用netbios
希望对你有用,我想SUN支持SOCKET,那用SOCKET的方法是可以的。
sharetop 2002-08-05
  • 打赏
  • 举报
回复
我记得这个问题我很久以前在网络通讯版问过一次,没有现成的类可以用的,只能用比较笨的方法。

比如,用ipconfig的命令,然后分析它的输出得到MAC地址。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧