• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當(dāng)前位置:雨林木風(fēng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁面

        整理了sun網(wǎng)站java環(huán)境下取得網(wǎng)卡信息的資料

        整理了sun網(wǎng)站java環(huán)境下取得網(wǎng)卡信息的資料

        更新時間:2022-05-09 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

        因為最近有程序需要在java環(huán)境下獲取網(wǎng)卡ID,研究了SUN網(wǎng)站的相關(guān)資料(感謝令公子的連接)。
        測試整理了一下,分享給大家。
        思路是通過調(diào)用windows環(huán)境下的ipconfig命令和Linux環(huán)境下的ifconfig命令來獲取網(wǎng)卡信息:
        分為四個程序文件:
        test.java;NetworkInfo.java;WindowsNetworkInfo.java;LinuxNetworkInfo.java
        //--------------test.java--------
        package netcardinfo;

        public class test {
        public test() {
        }
        public NetworkInfo nti = new WindowsNetworkInfo();
        public static void main(String[] args) {
        test test1 = new test();
        try {
         System.out.println("Network infos");
         System.out.println("Operating System:" + System.getProperty("os.name"));
         System.out.println("IP/Localhost:" + test1.nti.getLocalHost());
         System.out.println("MAC Address:" + test1.nti.getMacAddress());
         System.out.println("Domain:" + test1.nti.getNetworkDomain());
         }
         catch(Throwable t) {
         t.printStackTrace();
         }
        }
        }
        //---------end file---------

        //-----------NetworkInfo.java----------
        package netcardinfo;

        import java.net.*;
        import java.io.*;
        import java.text.*;
        import java.util.*;
        import java.util.regex.*;
        public abstract class NetworkInfo{
        private static final String LOCALHOST = "localhost";
        public static final String NSLOOKUP_CMD = "nslookup";
        public abstract String parseMacAddress() throws ParseException;
        /** Not too sure of the ramifications here, but it just doesn't seem right */
        public String parseDomain() throws ParseException {return parseDomain(LOCALHOST);}
        /** Universal entry for retrieving MAC Address */
        public final static String getMacAddress() throws IOException {
         try {
        NetworkInfo info = getNetworkInfo();
        String mac = info.parseMacAddress();
        return mac;
         }
         catch(ParseException ex)
        { ex.printStackTrace();
        throw new IOException(ex.getMessage());
        }
         }
        /** Universal entry for retrieving domain info */
        public final static String getNetworkDomain() throws IOException{
        try {
         NetworkInfo info = getNetworkInfo();
         String domain = info.parseDomain();
         return domain;
         }
        catch(ParseException ex)
        {ex.printStackTrace();
         throw new IOException(ex.getMessage());
        }
        }
        protected String parseDomain(String hostname) throws ParseException{
        // get the address of the host we are looking for - verification
         java.net.InetAddress addy = null;
         try {
        addy = java.net.InetAddress.getByName(hostname);
        }
        catch ( UnknownHostException e ) {
        e.printStackTrace();
        throw new ParseException(e.getMessage(),0);
        }
        // back out to the hostname - just validating
        hostname = addy.getCanonicalHostName();
        String nslookupCommand = NSLOOKUP_CMD + " " + hostname;
        // run the lookup command
        String nslookupResponse = null;
         try {
        nslookupResponse = runConsoleCommand(nslookupCommand);
        }
        catch ( IOException e ){
        e.printStackTrace();
        throw new ParseException(e.getMessage(), 0);
        }
         StringTokenizer tokeit = new StringTokenizer(nslookupResponse,"\n",false);
        while( tokeit.hasMoreTokens() )
        { String line = tokeit.nextToken();
        if( line.startsWith("Name:")){line = line.substring(line.indexOf(":") + 1);
        line = line.trim();
        if( isDomain(line, hostname)){line = line.substring(hostname.length()+1);
        return line;
        }
        }
        }
        return "n.a.";
        }
        private static booleanisDomain(String domainCandidate, String hostname)
        { Pattern domainPattern = Pattern.compile("[\\w-]+\\.[\\w-]+\\.[\\w-]+\\.[\\w-]+");
        Matcher m = domainPattern.matcher(domainCandidate);
        return m.matches() && domainCandidate.startsWith(hostname);
        }
        protected String runConsoleCommand(String command) throws IOException {
        Process p = Runtime.getRuntime().exec(command);
        InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
        StringBuffer buffer= new StringBuffer();
        for (;;) {
         int c = stdoutStream.read();
         if (c == -1) break;
         buffer.append((char)c);
         }
         String outputText = buffer.toString();
         stdoutStream.close();
         return outputText;
        }
        /** Sort of like a factory... */
        private static NetworkInfo getNetworkInfo() throws IOException {
         String os = System.getProperty("os.name");
         if(os.startsWith("Windows")) {
         return new WindowsNetworkInfo();
        }
         else if(os.startsWith("Linux")) {
         return new LinuxNetworkInfo();
         }
         else {
         throw new IOException("unknown operating system: " + os);
         }
        }
        protected String getLocalHost() throws ParseException {
         try {
         return java.net.InetAddress.getLocalHost().getHostAddress();
         }
         catch (java.net.UnknownHostException e ) {
        e.printStackTrace();
        throw new ParseException(e.getMessage(), 0);
         }
        }
        }
        //----------end file-------------

        //---------------WindowsNetworkInfo.java-------------
        package netcardinfo;

        import java.io.*;
        import java.text.*;
        import java.util.regex.*;
        public class WindowsNetworkInfo extends NetworkInfo{
        public static final String IPCONFIG_COMMAND = "ipconfig /all";
        public String parseMacAddress() throws ParseException {
        // run command
        String ipConfigResponse = null;
        try {
        ipConfigResponse = runConsoleCommand(IPCONFIG_COMMAND);
        }
        catch ( IOException e ) {
         e.printStackTrace();
        throw new ParseException(e.getMessage(), 0);
        }
        // get localhost address
        String localHost =getLocalHost();
        java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(ipConfigResponse, "\n");
        String lastMacAddress = null;
        while(tokenizer.hasMoreTokens()){
        String line = tokenizer.nextToken().trim();
        // see if line contains IP address, this means stop if we've already seen a MAC address
        if(line.endsWith(localHost) && lastMacAddress != null){
        return lastMacAddress;
        }
        // see if line might contain a MAC address
        int macAddressPosition = line.indexOf(":");
        if(macAddressPosition <= 0) {continue;}
        // trim the line and see if it matches the pattern
         String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
         if(isMacAddress(macAddressCandidate)) {
        lastMacAddress = macAddressCandidate;
        continue;
        }
        }
         ParseException ex = new ParseException("Cannot read MAC address from [" + ipConfigResponse + "]", 0);
         ex.printStackTrace();
         throw ex;}
        private static boolean isMacAddress(String macAddressCandidate){
        Pattern macPattern = Pattern.compile("[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}");
        Matcher m = macPattern.matcher(macAddressCandidate);
        return m.matches();
        }
        }
        //--------------end file--------------

        //------------LinuxNetworkInfo.java----------
        package netcardinfo;

        import java.io.*;
        import java.text.*;
        public class LinuxNetworkInfo extends NetworkInfo{
        public static final String IPCONFIG_COMMAND = "ifconfig";
        public String parseMacAddress() throws ParseException{
         String ipConfigResponse = null;
         try {
         ipConfigResponse = runConsoleCommand(IPCONFIG_COMMAND);
         }
        catch ( IOException e ) {
        e.printStackTrace();
        throw new ParseException(e.getMessage(), 0);
        }
        String localHost = null;
        try {
        localHost = java.net.InetAddress.getLocalHost().getHostAddress();
        }
        catch(java.net.UnknownHostException ex)
        {
        ex.printStackTrace();
        throw new ParseException(ex.getMessage(), 0);
        }
        java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(ipConfigResponse, "\n");
        String lastMacAddress = null;
        while(tokenizer.hasMoreTokens()) {
        String line = tokenizer.nextToken().trim();
        boolean containsLocalHost = line.indexOf(localHost) >= 0;
        // see if line contains IP address
        if(containsLocalHost && lastMacAddress != null) {
        return lastMacAddress;
        }
        // see if line contains MAC address
         int macAddressPosition = line.indexOf("HWaddr");
        if(macAddressPosition <= 0) {continue;}
        String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
        if(isMacAddress(macAddressCandidate)) {
         lastMacAddress = macAddressCandidate;
         continue;
         }
         }
         ParseException ex = new ParseException("cannot read MAC address for " + localHost + " from [" + ipConfigResponse + "]", 0);
         ex.printStackTrace();
         throw ex;
         }
         public StringparseDomain(String hostname)
         throws ParseException {return "";}
         private final boolean isMacAddress(String macAddressCandidate) {
        if(macAddressCandidate.length() != 17) return false;
        return true;}
        }
        程序在jdk1.4,windows2000下編譯測試通過,Linux環(huán)境需對test.java略做修改,條件所限未測試。

        溫馨提示:喜歡本站的話,請收藏一下本站!

        本類教程下載

        系統(tǒng)下載排行

        主站蜘蛛池模板: 亚洲一区二区三区无码中文字幕| 成年女人喷潮毛片免费播放| 日韩电影免费在线| 亚洲AV无码一区二区三区人| 免费A级毛片无码A∨免费| 亚洲无线电影官网| 亚洲成人在线免费观看| 亚洲精品中文字幕无码AV| 4虎永免费最新永久免费地址| 亚洲中文字幕久久精品无码2021| aⅴ在线免费观看| 亚洲激情视频图片| 日韩高清在线免费看| 激情吃奶吻胸免费视频xxxx| www国产亚洲精品久久久| 国产精品免费大片一区二区| 亚洲人成中文字幕在线观看| 毛片在线播放免费观看| 亚洲综合成人网在线观看| 四虎1515hh永久久免费| 亚洲色大情网站www| 国产一区在线观看免费| 9久热这里只有精品免费| 亚洲AV午夜成人片| 中字幕视频在线永久在线观看免费| 中文无码亚洲精品字幕| 免费jjzz在在线播放国产| a在线观看免费视频| 91亚洲性爱在线视频| 国产色爽女小说免费看| 不卡视频免费在线观看| 亚洲成人黄色网址| 四虎影库久免费视频| 精品免费tv久久久久久久| 亚洲国产午夜精品理论片| 国产美女a做受大片免费| 中文字幕无码毛片免费看| 亚洲人成高清在线播放| 免费人成在线观看播放国产 | 亚洲人成无码网WWW| 久久精品成人免费观看|