<script type="text/javascript"> google_ad_client = "pub-8800625213955058"; google_ad_slot = "0989131976"; google_ad_width = 336; google_ad_height = 280; // </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 網卡實體位址在全球是唯一不重複的,是以有時我們需要得到一個計算機的網卡實體位址來确認使用者身份,下面我編寫了一個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.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);
}
}
|