java编程实现获取服务器IP地址及MAC地址的方法
程序员文章站
2024-03-06 23:44:50
本文实例讲述了java编程实现获取服务器ip地址及mac地址的方法。分享给大家供大家参考,具体如下:
已测系统:
windows linux unix
排除127.0...
本文实例讲述了java编程实现获取服务器ip地址及mac地址的方法。分享给大家供大家参考,具体如下:
已测系统:
windows linux unix
排除127.0.0.1 和 0.0.0.0.1等非正常ip
import java.net.inetaddress; import java.net.networkinterface; import java.net.socketexception; import java.util.arraylist; import java.util.enumeration; import java.util.list; public class iputil { private iputil(){} /** * 此方法描述的是:获得服务器的ip地址 * @author: zhangyang33@sinopharm.com * @version: 2014年9月5日 下午4:57:15 */ public static string getlocalip() { string sip = ""; inetaddress ip = null; try { boolean bfindip = false; enumeration<networkinterface> netinterfaces = (enumeration<networkinterface>) networkinterface .getnetworkinterfaces(); while (netinterfaces.hasmoreelements()) { if (bfindip) { break; } networkinterface ni = (networkinterface) netinterfaces .nextelement(); enumeration<inetaddress> ips = ni.getinetaddresses(); while (ips.hasmoreelements()) { ip = (inetaddress) ips.nextelement(); if (!ip.isloopbackaddress() && ip.gethostaddress().matches( "(\\d{1,3}\\.){3}\\d{1,3}")) { bfindip = true; break; } } } } catch (exception e) { oututil.error(iputil.class, e.getmessage()); } if (null != ip) { sip = ip.gethostaddress(); } return sip; } /** * 此方法描述的是:获得服务器的ip地址(多网卡) * @author: zhangyang33@sinopharm.com * @version: 2014年9月5日 下午4:57:15 */ public static list<string> getlocalips() { inetaddress ip = null; list<string> iplist = new arraylist<string>(); try { enumeration<networkinterface> netinterfaces = (enumeration<networkinterface>) networkinterface .getnetworkinterfaces(); while (netinterfaces.hasmoreelements()) { networkinterface ni = (networkinterface) netinterfaces .nextelement(); enumeration<inetaddress> ips = ni.getinetaddresses(); while (ips.hasmoreelements()) { ip = (inetaddress) ips.nextelement(); if (!ip.isloopbackaddress() && ip.gethostaddress().matches( "(\\d{1,3}\\.){3}\\d{1,3}")) { iplist.add(ip.gethostaddress()); } } } } catch (exception e) { oututil.error(iputil.class, e.getmessage()); } return iplist; } /** * 此方法描述的是:获得服务器的mac地址 * @author: zhangyang33@sinopharm.com * @version: 2014年9月5日 下午1:27:25 */ public static string getmacid() { string macid = ""; inetaddress ip = null; networkinterface ni = null; try { boolean bfindip = false; enumeration<networkinterface> netinterfaces = (enumeration<networkinterface>) networkinterface .getnetworkinterfaces(); while (netinterfaces.hasmoreelements()) { if (bfindip) { break; } ni = (networkinterface) netinterfaces .nextelement(); // ----------特定情况,可以考虑用ni.getname判断 // 遍历所有ip enumeration<inetaddress> ips = ni.getinetaddresses(); while (ips.hasmoreelements()) { ip = (inetaddress) ips.nextelement(); if (!ip.isloopbackaddress() // 非127.0.0.1 && ip.gethostaddress().matches( "(\\d{1,3}\\.){3}\\d{1,3}")) { bfindip = true; break; } } } } catch (exception e) { oututil.error(iputil.class, e.getmessage()); } if (null != ip) { try { macid = getmacfrombytes(ni.gethardwareaddress()); } catch (socketexception e) { oututil.error(iputil.class, e.getmessage()); } } return macid; } /** * 此方法描述的是:获得服务器的mac地址(多网卡) * @author: zhangyang33@sinopharm.com * @version: 2014年9月5日 下午1:27:25 */ public static list<string> getmacids() { inetaddress ip = null; networkinterface ni = null; list<string> maclist = new arraylist<string>(); try { enumeration<networkinterface> netinterfaces = (enumeration<networkinterface>) networkinterface .getnetworkinterfaces(); while (netinterfaces.hasmoreelements()) { ni = (networkinterface) netinterfaces .nextelement(); // ----------特定情况,可以考虑用ni.getname判断 // 遍历所有ip enumeration<inetaddress> ips = ni.getinetaddresses(); while (ips.hasmoreelements()) { ip = (inetaddress) ips.nextelement(); if (!ip.isloopbackaddress() // 非127.0.0.1 && ip.gethostaddress().matches( "(\\d{1,3}\\.){3}\\d{1,3}")) { maclist.add(getmacfrombytes(ni.gethardwareaddress())); } } } } catch (exception e) { oututil.error(iputil.class, e.getmessage()); } return maclist; } private static string getmacfrombytes(byte[] bytes) { stringbuffer mac = new stringbuffer(); byte currentbyte; boolean first = false; for (byte b : bytes) { if (first) { mac.append("-"); } currentbyte = (byte) ((b & 240) >> 4); mac.append(integer.tohexstring(currentbyte)); currentbyte = (byte) (b & 15); mac.append(integer.tohexstring(currentbyte)); first = true; } return mac.tostring().touppercase(); } }
希望本文所述对大家java程序设计有所帮助。