/**
* <获取网卡名称>
*
* @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;
}
}
}