java获取linux系统服务器ip地址问题。(大神请进)

kahnnash 2014-10-13 03:28:25

private static InetAddress getLinuxLocalIp() throws SocketException {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces
.nextElement();
logger.error("ni.getName():"+ni.getName());
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = ips.nextElement();
logger.error("ip.isSiteLocalAddress():"+ip.isSiteLocalAddress());
logger.error("ip.isLoopbackAddress():"+ip.isLoopbackAddress());
logger.error("ip.getHostAddress():"+ip.getHostAddress());
if (ip != null && ip instanceof Inet4Address && ip.getHostAddress().indexOf(".") != -1) {// 1.获取符合条件的ip
//return ip;
} else {
ip = null;
}
}
}
return null;
}


代码如上,在测试环境上打印出来的语句如下:
19:24:34,920 [TimerListener] ERROR ni.getName():virbr0
19:24:34,921 [TimerListener] ERROR ip.isSiteLocalAddress():false
19:24:34,921 [TimerListener] ERROR ip.isLoopbackAddress():false
19:24:34,921 [TimerListener] ERROR ip.getHostAddress():fe80:0:0:0:200:ff:fe00:0%4
19:24:34,921 [TimerListener] ERROR ip.isSiteLocalAddress():true
19:24:34,921 [TimerListener] ERROR ip.isLoopbackAddress():false
19:24:34,921 [TimerListener] ERROR ip.getHostAddress():192.168.122.1// 不是我想要的
19:24:34,921 [TimerListener] ERROR ni.getName():eth0
19:24:34,921 [TimerListener] ERROR ip.isSiteLocalAddress():false
19:24:34,921 [TimerListener] ERROR ip.isLoopbackAddress():false
19:24:34,921 [TimerListener] ERROR ip.getHostAddress():fe80:0:0:0:250:56ff:fea4:46e8%2
19:24:34,922 [TimerListener] ERROR ip.isSiteLocalAddress():true
19:24:34,922 [TimerListener] ERROR ip.isLoopbackAddress():false

19:24:34,922 [TimerListener] ERROR ip.getHostAddress():192.168.2.1** // 这个是要获取的,真实的
19:24:34,922 [TimerListener] ERROR ni.getName():lo
19:24:34,922 [TimerListener] ERROR ip.isSiteLocalAddress():false
19:24:34,922 [TimerListener] ERROR ip.isLoopbackAddress():true
19:24:34,922 [TimerListener] ERROR ip.getHostAddress():0:0:0:0:0:0:0:1%1
19:24:34,922 [TimerListener] ERROR ip.isSiteLocalAddress():false
19:24:34,922 [TimerListener] ERROR ip.isLoopbackAddress():true
19:24:34,922 [TimerListener] ERROR ip.getHostAddress():127.0.0.1// 不是我想要的

代码放到正式环境下打印日志如下:
20:09:05,303 [TimerListener] ERROR ni.getName():virbr0
20:09:05,305 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,305 [TimerListener] ERROR ip.isLoopbackAddress():false
20:09:05,305 [TimerListener] ERROR ip.getHostAddress():fe80:0:0:0:200:**:***:***
20:09:05,306 [TimerListener] ERROR ip.isSiteLocalAddress():true
20:09:05,306 [TimerListener] ERROR ip.isLoopbackAddress():false
20:09:05,306 [TimerListener] ERROR ip.getHostAddress():192.168.122.1// 不是我想要的
20:09:05,307 [TimerListener] ERROR ni.getName():bond0
20:09:05,307 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,308 [TimerListener] ERROR ip.isLoopbackAddress():false
20:09:05,308 [TimerListener] ERROR ip.getHostAddress():fe80:0:0:0:5ef3:f***:****:******
20:09:05,309 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,309 [TimerListener] ERROR ip.isLoopbackAddress():false

20:09:05,309 [TimerListener] ERROR ip.getHostAddress():19*.1*.*.*** // 这个是我要获取的
20:09:05,310 [TimerListener] ERROR ni.getName():usb0
20:09:05,310 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,311 [TimerListener] ERROR ip.isLoopbackAddress():false
20:09:05,311 [TimerListener] ERROR ip.getHostAddress():fe80:0:0:0:5cf3:f***:****:*****
20:09:05,311 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,312 [TimerListener] ERROR ip.isLoopbackAddress():false
20:09:05,312 [TimerListener] ERROR ip.getHostAddress():1**.2**.9*.1** // 不是我想要的
20:09:05,312 [TimerListener] ERROR ni.getName():lo
20:09:05,313 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,313 [TimerListener] ERROR ip.isLoopbackAddress():true
20:09:05,314 [TimerListener] ERROR ip.getHostAddress():0:0:0:0:0:0:0:1%1
20:09:05,314 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,314 [TimerListener] ERROR ip.isLoopbackAddress():true
20:09:05,315 [TimerListener] ERROR ip.getHostAddress():127.0.0.1// 不是我想要的


遍历时要获取我想要的Inet4Address 地址,但是有那么多不是我想要的,怎么把它们过滤掉?看网上的方法都是判断红色日志中的内容,但是为什么测试环境是true false,正式环境中却是false false?

...全文
742 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgd77521 2014-10-14
  • 打赏
  • 举报
回复
WinSpread 2014-10-14
  • 打赏
  • 举报
回复
试试这个代码
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

/**
 * @author suzhw<br>
 *         日期:2014-10-14<br>
 *         功能:根据启动并运行的网卡名称,找到本机实际的IP地址(已知当前运行的无线网卡名包含“wireless”字段)<br>
 *         版权:版权所有(C) 2014,suzhw<br>
 * @version v0.1
 * @see
 */
public class ToGetLocalLinkIP2 {
	public static void main(String[] args) throws Exception {
		Enumeration<NetworkInterface> en = NetworkInterface
				.getNetworkInterfaces();
		while (en.hasMoreElements()) {
			NetworkInterface ni = en.nextElement();
			printParameter(ni);

		}
	}

	public static void printParameter(NetworkInterface ni)
			throws SocketException {
		if (null != ni.getDisplayName()
				&& ni.getDisplayName().contains("Wireless") && ni.isUp()) {
			System.out.println(" Name = " + ni.getName());
			System.out.println(" Display Name = " + ni.getDisplayName());
			System.out.println(" Is up = " + ni.isUp());
			System.out
					.println(" Support multicast = " + ni.supportsMulticast());
			System.out.println(" Is loopback = " + ni.isLoopback());
			System.out.println(" Is virtual = " + ni.isVirtual());
			System.out.println(" Is point to point = " + ni.isPointToPoint());
			System.out
					.println(" Hardware address = " + ni.getHardwareAddress());
			System.out.println(" MTU = " + ni.getMTU());

			System.out.println("\nList of Interface Addresses:");
			List<InterfaceAddress> list = ni.getInterfaceAddresses();
			Iterator<InterfaceAddress> it = list.iterator();

			while (it.hasNext()) {
				InterfaceAddress ia = it.next();
				if(null!=ia.getBroadcast()){
					System.out.println(" 本机实际联网网卡IPv4地址: " + ia.getAddress().getHostAddress());
				}
			}

			System.out
					.println("**************************************************");
		}
	}
}
WinSpread 2014-10-14
  • 打赏
  • 举报
回复
NetworkInterface调用getDisplayName方法 显示网卡名称 判定 是服务器使用的网卡名,则区其IP地址
attilax 2014-10-13
  • 打赏
  • 举报
回复
机器上有多块网卡,会显示多个ip地址。。这个只能根据网段来判断那个是你需要的ip
kahnnash 2014-10-13
  • 打赏
  • 举报
回复
CSDN好冷清。。。

67,511

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧