求个java Runtime.getRuntime().exec的问题,每次只是匹配了第一个判断条件

求个java Runtime.getRuntime().exec的问题,每次只是匹配了第一个判断条件

勿忘初心 发布于 2021-11-24 字数 3573 浏览 928 回复 1

求个  Runtime.getRuntime().exec的问题,每次只是匹配了第一个判断条件(处理 centos 7 的虚拟机特殊情况
),第二个判断条件一直判断不上,也就是说,如果运行在 
 centos 7 的虚拟机 就能判断到,而运行到 centos 7 下就不行,怎么破?

下图是
centos 7 环境

下图是centos 7 虚拟机环境

public String getLinuxMACAddress() {
		String mac = null;
		BufferedReader bufferedReader = null;
		Process process = null;

		if (process == null) {
 
			try {
				// 处理 centos 7 的虚拟机特殊情况
				process = Runtime.getRuntime().exec("ip addr show enp0s3");
 
				if(process != null){
					bufferedReader = new BufferedReader(new InputStreamReader(
							process.getInputStream()));
					String line = null;
					int index = -1;
					while ((line = bufferedReader.readLine()) != null) {
						index = line.toLowerCase().indexOf("link/ether");
						if (index != -1) {
							mac = line.substring(index + 10).trim();
							break;
						}
					}
					mac = mac.substring(0, 17);
				}
				int exitVal = 0;
				try {
					exitVal = process.waitFor();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            System.out.println("Process exitValue: " + exitVal);
			} catch (IOException e) {
				logger.info(e.getMessage());
			}
 
		}
		

		
		if (process == null) {
 
			try {
				// 处理 centos 7 的特殊情况
				process = Runtime.getRuntime().exec("ip addr show ens33");
 
				if(process != null){
					bufferedReader = new BufferedReader(new InputStreamReader(
							process.getInputStream()));
					String line = null;
					int index = -1;
					while ((line = bufferedReader.readLine()) != null) {
						index = line.toLowerCase().indexOf("link/ether");
						if (index != -1) {
							mac = line.substring(index + 10).trim();
							break;
						}
					}
					mac = mac.substring(0, 17);
				}
				int exitVal = 0;
				try {
					exitVal = process.waitFor();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            System.out.println("Process exitValue: " + exitVal);
			} catch (IOException e) {
				logger.info(e.getMessage());
			}
 
		}
 
		
		if (process == null) {
 
			try {
				process = Runtime.getRuntime().exec("ifconfig eth0");
 
				if(process != null){
					bufferedReader = new BufferedReader(new InputStreamReader(
							process.getInputStream()));
					String line = null;
					int index = -1;
					while ((line = bufferedReader.readLine()) != null) {
						index = line.toLowerCase().indexOf("硬件地址");
						if (index != -1) {
							// 取出mac地址并去除2边空格
							mac = line.substring(index + 4).trim();
							break;
						}
					}
				}
				int exitVal = 0;
				try {
					exitVal = process.waitFor();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            System.out.println("Process exitValue: " + exitVal);
			} catch (IOException e) {
				logger.info(e.getMessage());
			}
		}

 
		
		if (bufferedReader != null) {
			try {
				bufferedReader.close();
			} catch (IOException e) {
				logger.info(e.getMessage());
			}
		}

		bufferedReader = null;
		process = null;

		return mac;

	}

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2021-11-25 1 楼

catch NullPointerException 到第二个处理,依次,全错的时候再抛出。