java执行linux命令获取MAC和ip地址
来源:互联网 发布:mysql怎么降序排列 编辑:程序博客网 时间:2024/04/25 12:21
/** *获取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 0
- java执行linux命令获取MAC和ip地址
- Linux下java调取命令通过ip获取mac地址
- java获取ip地址和Mac地址
- linux编程获取IP和MAC地址
- LINUX下获取IP地址和MAC地址
- LINUX下获取IP地址和MAC地址
- LINUX下获取IP地址和MAC地址
- LINUX下获取IP地址和MAC地址
- linux上使用java获取本机IP地址和MAC地址
- linux上使用java获取本机IP地址和MAC地址
- java获取客户端IP地址和MAC地址
- java获取本机的IP地址和mac地址
- JAVA获取客户端IP地址和MAC地址
- linux下获取MAC地址,IP地址
- java通过IP地址获取MAC地址
- linux 下C实现获取网卡IP和MAC地址
- 获取linux机器的IP和mac地址
- linux中获取本机ip和mac地址
- Web开发入门不得不看
- Ubuntu之Jenkins安装
- 233 - Number of Digit One
- ls 对应各种目录、文件列出的用法
- (十) Advanced Build Customization(高级构建定制)(完)
- java执行linux命令获取MAC和ip地址
- 【西祠日志】【18】python渐成主流
- 我的CSDN第一天
- 关于DB In-Memory Option你必须知道的17个问题
- bzoj 3211
- csdn 高效 学习技能 贪心
- swift封装的跑马灯效果
- poj 2406 Power Strings
- 蜂巢格网