用java得到w2k计算机上的网卡物理地址

本文介绍如何使用Java编程获取Windows 2000系统的网卡物理地址,通过执行`ipconfig/all`命令并解析输出,但这种方法不适用于其他操作系统。
摘要由CSDN通过智能技术生成

用java得到w2k计算机上的网卡物理地址 阅读次数13543

出处 CN-JAVA原创:大砍刀    

--------------------------------------------------------------------------------
网卡物理地址在全球是唯一不重复的,所以有时我们需要得到一个计算机的网卡物理地址来确认用户身份,下面我编写了一个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);
               
               
        }
}

 


    
 

   
  
 
 
 
  整理发布

 

 

 发言人:过客   日期:2002-07-18
--------------------------------------------------------------------------------
还不如写一个 JNI 方法自己来取了,那样的话就不用考虑有没有 ipconfig 这个外部命令了。


 发言人:过客   日期:2002-12-16
--------------------------------------------------------------------------------
这种方法过于遇笨


 发言人:过客   日期:2002-12-16
--------------------------------------------------------------------------------
程序写的还可以,不知哪位高手还有更好的方法?


 发言人:过客   日期:2003-01-17
--------------------------------------------------------------------------------
还有其他的象debug那样的通用程序吗?


 发言人:过客   日期:2003-01-24
--------------------------------------------------------------------------------
sb


 发言人:过客   日期:2003-01-27
--------------------------------------------------------------------------------
依赖于操作系统,不好


 发言人:过客   日期:2003-02-23
--------------------------------------------------------------------------------
有适合于xp操作系统的么??


 发言人:过客   日期:2003-02-28
--------------------------------------------------------------------------------
能用,有没有更好的


 发言人:过客   日期:2003-03-07
--------------------------------------------------------------------------------
获取自己的啊?别人的行吗


 发言人:过客   日期:2003-04-21
--------------------------------------------------------------------------------
这样写太麻烦了


 发言人:过客   日期:2003-08-23
--------------------------------------------------------------------------------
笨人有笨方法,年轻人果然很执著,pf!


 发言人:过客   日期:2003-09-16
--------------------------------------------------------------------------------
不对,物理地址应是在
Ethernet adapter 本地连接(一般为十六进制):
Connection-specific DNS Suffix . : lan
Description . . . . . . . . . . . : Realtek RTL8029(AS
et Adapter
Physical Address. . . . . : 52-54-AB-38-D5-C7


 发言人:过客   日期:2004-03-17
--------------------------------------------------------------------------------
真够笨的


 发言人:过客   日期:2004-03-17
--------------------------------------------------------------------------------
这种方法用在实际的软件中实在不可取,万一ipconfig的输出格式变了怎么办.


 发言人:过客   日期:2004-03-22
--------------------------------------------------------------------------------
还可以,不一定适用


 发言人:过客   日期:2004-04-04
--------------------------------------------------------------------------------
很基本的操作


 发言人:过客   日期:2004-04-19
--------------------------------------------------------------------------------
别~他~妈的只说别人笨,谁有更好的办法,说出来撒。


 发言人:过客   日期:2004-05-08
--------------------------------------------------------------------------------
以前认为获取网卡地址就是这么回事,现在才知道其实这样行不通的~


 发言人:rayleo   日期:2004-10-11
--------------------------------------------------------------------------------
我在xp下运行不了....


 发言人:panda56   日期:2004-10-23
--------------------------------------------------------------------------------
那些说这种方法笨的高手们,给个示例出来看看嘛.要不然说说也行啊,别光作没依据的评论.


 发言人:过客   日期:2005-01-14
--------------------------------------------------------------------------------
唉写个东西被骂到臭头不值~~藐视骂人者~~


 发言人:过客   日期:2005-01-14
--------------------------------------------------------------------------------
同意


 发言人:过客   日期:2005-01-14
--------------------------------------------------------------------------------
举双手同意


 发言人:过客   日期:2005-01-14
--------------------------------------------------------------------------------
不过确实够笨的


 发言人:过客   日期:2005-01-27
--------------------------------------------------------------------------------
我的微机不能入入网,经查网卡的物理地址变成FFFFFFFFFFFF,这是怎么回事?咋解决?


 发言人:过客   日期:2005-02-12
--------------------------------------------------------------------------------
系统错误了嘛

 发言人:过客   日期:2005-04-03
--------------------------------------------------------------------------------
挺不错,有创意。
支持!!!


 发言人:过客   日期:2005-05-01
--------------------------------------------------------------------------------
写得非常好啊,我学到好多东西


 发言人:过客   日期:2005-05-26
--------------------------------------------------------------------------------
方法挺好


 发言人:过客   日期:2005-05-26
--------------------------------------------------------------------------------
昨天我也想到了这个办法,今天搜一下想看看有没有更好的办法,发现了这个,省我编码了。
感谢楼主!


 发言人:过客   日期:2005-05-27
--------------------------------------------------------------------------------
笨方法未尝不可,只要能解决问题。在有更好的解决办法之前,笨方法就是最好的方法。I'm SunXP.

 
 
 引用自:http://www.cn-java.com/target/news.php?news_id=1053
 

好的,以下是一个简单的上下文无关文法化简算法的 Java 程序示例: ```java import java.util.*; public class CNF_Simplifier { // 定义产生式规则 private static final Map<String, List<String>> CFG_RULES = new HashMap<>(); static { CFG_RULES.put("S", Arrays.asList("A", "B")); CFG_RULES.put("A", Arrays.asList("a", "S", "b")); CFG_RULES.put("B", Arrays.asList("c", "S", "d")); CFG_RULES.put("B", Arrays.asList("e")); } public static void main(String[] args) { // 将 CFG 规则转换为 CNF 规则 List<String> cnfRules = convertToCNF(CFG_RULES); // 输出化简后的 CNF 规则 for (String rule : cnfRules) { System.out.println(rule); } } private static List<String> convertToCNF(Map<String, List<String>> cfgRules) { List<String> cnfRules = new ArrayList<>(); // 将产生式规则中所有单一符号转换为终结符号 for (Map.Entry<String, List<String>> entry : cfgRules.entrySet()) { String nonTerminal = entry.getKey(); List<String> productions = entry.getValue(); for (String production : productions) { if (production.length() == 1 && Character.isUpperCase(production.charAt(0))) { String newNonTerminal = production; cnfRules.add(nonTerminal + " -> " + newNonTerminal); } } } // 将产生式规则中所有长度大于等于 3 的产生式拆分为多个产生式 for (Map.Entry<String, List<String>> entry : cfgRules.entrySet()) { String nonTerminal = entry.getKey(); List<String> productions = entry.getValue(); for (String production : productions) { if (production.length() >= 3) { String newNonTerminal = generateNewNonTerminal(); // 将产生式拆分为 A -> B C 和 B -> w1 和 C -> w2 的形式 cnfRules.add(nonTerminal + " -> " + production.charAt(0) + " " + newNonTerminal); cnfRules.add(newNonTerminal + " -> " + production.substring(1)); } } } return cnfRules; } private static String generateNewNonTerminal() { return "X" + UUID.randomUUID().toString().replace("-", ""); } } ``` 该程序中,我们首先定义了一个上下文无关文法的产生式规则`CFG_RULES`,并实现了一个`convertToCNF`方法,将该 CFG 规则转换为 CNF 规则。接着,我们通过`main`方法调用该方法并输出化简后的 CNF 规则。 在`convertToCNF`方法中,我们首先将 CFG 规则中所有单一符号转换为终结符号,然后将长度大于等于 3 的产生式拆分为多个产生式,最后返回化简后的 CNF 规则。拆分产生式的过程中,我们还生成了新的非终结符号,以便满足 CNF 规则中产生式的要求。 请注意,本示例程序仅适用于特定的 CFG 规则,如果您需要处理其他类型的 CFG 规则,请做相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值