java获取linux网卡名

/**
     * <获取网卡名称>
     *
     * @return 结果
     * @throws
     */
    public List<String> getInetName()
        throws ServiceException
    {
        String command = "ifconfig";
        Reader reader = null;
        BufferedReader inetReader = null;
        InputStream inputStream = null;
        List<String> cmdOutput = new ArrayList<>();
        List<String> inetList = new ArrayList<>();
        List<String> inetNameListTemp = new ArrayList();
        List<String> inetNameList = new ArrayList();
        String line = null;
        try
        {
            Process process = Runtime.getRuntime().exec(command);
            inputStream = process.getInputStream();
            reader = new InputStreamReader(inputStream);
            inetReader = new BufferedReader(reader);

            while ((line = inetReader.readLine()) != null)
            {
                cmdOutput.add(line);
            }
            LOGGER.info("cmdOutput is {}", cmdOutput);
            if (null != cmdOutput && cmdOutput.size() > 0)
            {
                for (String output : cmdOutput)
                {
                    if (output.contains("Link encap"))
                    {
                        inetList.add(output);
                    }
                }
            }
            LOGGER.info("inetList is {}", inetList);

            if (null != inetList && inetList.size() > 0)
            {
                for (String output : inetList)
                {
                    output = output.replaceAll(" ", "");
                    LOGGER.info("output is {}", output);
                    LOGGER.info("output.indexOf is {}", output.indexOf("Linkencap"));
                    inetNameListTemp.add(output.substring(0, output.indexOf("Linkencap")));
                }
            }
            LOGGER.info("inetNameListTemp is {}", inetNameListTemp);

            if (null != inetNameListTemp && inetNameListTemp.size() > 0)
            {
                for (String inetName : inetNameListTemp)
                {
                    if (!"lo".equals(inetName) && !"docker0".equals(inetName))
                    {
                        inetNameList.add(inetName);
                    }
                }
            }

            process.waitFor();

            return inetNameList;
        }
        catch (Exception ex)
        {
            LOGGER.error("get inetnames error.");
            return inetNameList;
        }
        finally
        {
            try
            {
                if (null != inputStream)
                {
                    inputStream.close();
                }
                if (null != reader)
                {
                    reader.close();
                }
                if (null != inetReader)
                {
                    inetReader.close();
                }
            }
            catch (IOException e)
            {
                LOGGER.error("close inputstream error.");
                return inetNameList;
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值