Linux下用Java获取本机IP

可能有多个网卡包括虚拟网卡,需要进行排除

String ip = "";
try {
    Enumeration<?> e1 = NetworkInterface.getNetworkInterfaces();//获取多个网卡
    while (e1.hasMoreElements()) {
    NetworkInterface ni = (NetworkInterface) e1.nextElement();

    if(("eth0").equals(ni.getName()) || ("ens33").equals(ni.getName())){//取“eth0”和“ens33”两个网卡
        Enumeration<?> e2 = ni.getInetAddresses();
        while (e2.hasMoreElements()) {
          InetAddress ia = (InetAddress) e2.nextElement();
          if (ia instanceof Inet6Address) {//排除IPv6地址
              continue;
          }
          ip = ia.getHostAddress();
          }
          break;
      }
    }
} catch (SocketException e) {
    e.printStackTrace();
}

原文地址:https://www.cnblogs.com/jugglee/p/8877011.html

时间: 2024-08-30 04:16:23

Linux下用Java获取本机IP的相关文章

用java获取本机IP地址

在网上找了几个用java获取本机IP地址的代码,发现都少都有些不完美,自己整理了一下.突然之间很想把自己的IP地址给获取了,虽然用系统自带命令可 以得到,但自己想写一个程序获取一下,到网上搜索了一下java获取本机IP地址的方法,结果居然发现没有一个是可以用的,气的我老人家吐血, 这些人闭着眼睛写程序,写完了就往网上发,也不测试一下,害的我以为自己RP问题,老是获取不到正确的IP地址,强烈谴责!!!为了表示鄙视,现把网上找到的主要的两种方法的不足给指出一下方法一(只能在Windows上使用,Li

详谈再论JAVA获取本机IP地址

首先,你如果搜索“JAVA获取本机IP地址”,基本上搜到的资料全是无用的.比如这篇:http://www.cnblogs.com/zrui-xyu/p/5039551.html实际上的代码在复杂环境下是不准的 网上一个比较普遍的说法是InetAddress.getLocalHost().getHostAddress()似乎很简单,但忽略了一个问题,即IP地址在现在的网络环境更加复杂了,比如有Lan,WIFI,蓝牙热点,虚拟机网卡...即存在很多的网络接口(network interfaces),

java 获取本机ip及mac地址

package com.achun.test; import java.net.Inet4Address;import java.net.Inet6Address;import java.net.InetAddress;import java.net.NetworkInterface;import java.util.Enumeration; public class HelloWorld { public static void main(String[] args) { // TODO Au

java获取本机IP和主机名

贴代码 GetIP.java package com.datongsoft.wg.common.util; import java.net.InetAddress; public class GetIP { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { InetAddress addr = InetAddress.getLocalHost();

java获取本机IP地址

/** * WIFI没打开:ip为127.0.0.1 * 获取本机IP地址字符串 * @return */ public String getWifiIp() { if (!getWifiEnabled()) { return "127.0.0.1"; } WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ip = intToIp(i

方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息

CPU使用率: InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTokenizer tokenStat = null; // 用来分隔String的应用类 try { System.out.println("Get usage rate of CUP : "); Process process = Runtime.getRuntime().exec(&quo

java 获取本机ip地址

/** * 取当前系统站点本地地址 linux下 和 window下可用 * * @return */ public static String getLocalIP() { String sIP = ""; InetAddress ip = null; try { // 如果是Windows操作系统 if (isWindowsOS()) { ip = InetAddress.getLocalHost(); } // 如果是Linux操作系统 else { boolean bFindI

Java 获取本机IP

import java.io.*; import java.util.*; import java.net.*; public class GetIP { public static void main (String[] args) throws Exception { /* String s = InetAddress.getLocalHost().toString(); System.out.println(s); String[] arr = s.split("/"); Sys

java获取本机IP地址和MAC地址的方法

// 获取ip地址 public static String getIpAddress() { try { Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; while (allNetInterfaces.hasMoreElements()) { NetworkInterface netInterface = (Netw