频道栏目
首页 > 资讯 > Dos命令行 > 正文

通过dos命令获得服务器网卡地址-适合Windows和Linux

12-09-04        来源:[db:作者]  
收藏   我要投稿

通过dos命令获得服务器网卡地址-适合Windows和Linux
 
Java代码  
/** 
     * 获得服务器网卡地址 
     *    www.2cto.com  
     * @return 服务器网卡地址 
     */  
    private String getServerMACAddress() {  
        String address = "";  
        String os = AppContext.SERVER_OS_NAME;  
        // 如果是Windows系统  
        if (os.startsWith("Windows")) {  
            try {  
                String command = "cmd.exe /c ipconfig /all";  
                Process p = Runtime.getRuntime().exec(command);  
                BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));  
                String line = null;  
                while ((line = buf.readLine()) != null) {  
                    if (line.indexOf("Physical Address") >= 0) {  
                        int index = line.indexOf(":");  
                        index += 2;  
                        address = line.substring(index);  
                        break;  
                    }    www.2cto.com  
                }  
                buf.close();  
                return address.trim();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        // 如果是Linux操作系统  
        else if (os.startsWith("Linux")) {  
            try {  
                String command = "/bin/sh -c ifconfig -a";  
                Process p = Runtime.getRuntime().exec(command);  
                BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));  
                String line = null;  
                while ((line = buf.readLine()) != null) {  
                    if (line.indexOf("HWaddr") > 0) {  
                        int index = line.indexOf("HWaddr") + "HWaddr".length();  
                        address = line.substring(index);  
                        break;  
                    }  
                }    www.2cto.com  
                buf.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        // 未知操作系统  
        else {  
  
        }  
        address = address.trim();  
        return address;  
    } 
 
相关TAG标签
上一篇:DataTable.Compute用法
下一篇:如何制作高质量外链
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站