java执行linux命令获取MAC和ip地址

 /**
     *获取linux Ubuntu网卡的mac地址
     */
    public static  String  getmacAddressByLinux(){
        String mac = "";
        String[] commands = new String[]{"/bin/bash", "-c", "ifconfig eth0|grep 'HWaddr'|awk -F ' ' '{print $5}'"};
        try {
              mac =RmtShellExecutor.execArrayCmdRetrun("commands", commands);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mac;
    }
 

/**
     *  获取linux Ubuntu eth0 内网ip地址
     */
    public static String getinternalAddressByLinux(){
        String ip ="";
        String[] commands = new String[]{"/bin/bash", "-c", "ifconfig eth0|grep 'inet addr'|awk -F  ':'  '{print $2}'|awk '{print $1}'"};
        try {
           ip =RmtShellExecutor.execArrayCmdRetrun("commands", commands);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ip;
    }
 

/**
     * 
     * @param message
     * @param args
     * @return
     * @throws Exception
     */
    public static String execArrayCmdRetrun(String message, String[] args) throws Exception {
        log.debug("---执行linux shell ---");
        log.debug(message + ":");
        String result = "";
        Process process = Runtime.getRuntime().exec(args);
        for (String arg : args) {
            System.out.println(arg);
            System.out.print(" ");
        }
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        String line = null;
        while ((line = errorReader.readLine()) != null) {
            System.out.println("--errorReader--:"+line);
        }
        errorReader.close();
        BufferedReader infoReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((line = infoReader.readLine()) != null) {
            System.out.println("--infoReader--:"+line);
            result = line;
        }
        infoReader.close();
        log.debug("");
        return result;
    }
 

/**
     * 获取widnows网卡的mac地址.
     *
     * @return mac地址
     */
    public static String getWindowsMACAddress() {
        String mac = null;
        BufferedReader bufferedReader = null;
        Process process = null;
        try {
            // windows下的命令,显示信息中包含有mac地址信息
            process = Runtime.getRuntime().exec("ipconfig /all");
            bufferedReader = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            String line = null;
            int index = -1;
            while ((line = bufferedReader.readLine()) != null) {
                // 寻找标示字符串[physical
                index = line.toLowerCase().indexOf("physical address");

                if (index >= 0) {// 找到了
                    index = line.indexOf(":");// 寻找":"的位置
                    if (index >= 0) {
                        // 取出mac地址并去除2边空格
                        mac = line.substring(index + 1).trim();
                    }
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            bufferedReader = null;
            process = null;
        }

        return mac;
    }

    /**
     * windows 7 专用 获取MAC地址
     *
     * @return
     *
     * @throws Exception
     */
    public static String getMACAddress() throws Exception {

        // 获取本地IP对象
        InetAddress ia = InetAddress.getLocalHost();
        // 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

        // 下面代码是把mac地址拼装成String
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < mac.length; i++) {
            if (i != 0) {
                sb.append("-");
            }
            // mac[i] & 0xFF 是为了把byte转化为正整数
            String s = Integer.toHexString(mac[i] & 0xFF);
            sb.append(s.length() == 1 ? 0 + s : s);
        }

        // 把字符串所有小写字母改为大写成为正规的mac地址并返回
        return sb.toString().toUpperCase();
    }
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值