转载(不过我不知道谁写的)网卡物理地址在全球是唯一不重复的,所以有时我们需要得到一个计算机的网卡物理地址来确认用户身份,下面我编写了一个GetPhysicalAddress类来获得当前计算机的网卡物理地址,此方法只能获取win2k系统的网卡物理地址
,不能用于其他操作系统。众所周知,在win2k下的shell环境里,我们可以使用ipconfig/all命令查看当前网卡配置情况,其中有一项是网卡的物理地址。例如在DOS环境中输入 ipconfig/all命令,结果如下: Host Name . . . . . . . . . . . . : mars
Primary DNS Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : worksoft.com.cnPPP 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);
}
}
,不能用于其他操作系统。众所周知,在win2k下的shell环境里,我们可以使用ipconfig/all命令查看当前网卡配置情况,其中有一项是网卡的物理地址。例如在DOS环境中输入 ipconfig/all命令,结果如下: Host Name . . . . . . . . . . . . : mars
Primary DNS Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : worksoft.com.cnPPP 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);
}
}
解决方案 »
- 处理异常的一个小问题!
- 关于jdbc的commit
- [jsp图片写入image]侠士帮下忙,想3小时没看出来
- SimpleDateFormat的使用中的 问题
- 我现在有long val=123;但要显示成00000123如何办?就是格式化的问题,用0填葱
- Borland Optimizeit Suite 不会用啊!哪位高手可以指点一下啊?(先谢了!)
- 关于Object比较
- jbuild 8 的界面问题
- beforeFirst这个操作不执行,为什么
- 请问Collection中的Iterator应该怎么翻译啊?
- 新手提问,怎样把一个string里的所有单词按字符顺序升序排列?
- mysql库操作的中文问题
要想取这个地址 肯定要使用java的本地程序
所以啊 jug 就是一个这样的包
可是我下载来 却不知如何用
主机上运行程序,取得同网段或指定网段的主机的MAC地址.
这个不就是mac地址
得到client的ip
nbtstat -a 192.168.0.1本地连接:
Node IpAddress: [192.168.0.18] Scope Id: [] NetBIOS Remote Machine Name Table Name Type Status
---------------------------------------------
NETGATE <00> UNIQUE Registered
NETGATE <20> UNIQUE Registered
SOFTFORCE <00> GROUP Registered
SOFTFORCE <1E> GROUP Registered
SOFTFORCE <1D> UNIQUE Registered
..__MSBROWSE__.<01> GROUP Registered
NETGATE <03> UNIQUE Registered MAC Address = 00-E0-4C-FE-FF-54
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~可获取MAC地址.利用原理:同 bluesmile979(笑着)的用java执行shell但这方法只适用于内部网运行的B/S系统