示例#1
0
  public String detectPPP() {
    try {
      List<String> linkPPP = new ArrayList<String>();
      List<String> linkDSL = new ArrayList<String>();
      Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();

      // System.out.println("NETS VALUES"+" "+nets);

      // System.out.println("INITIALY VALUES"+" "+linkPPP);

      for (NetworkInterface netInf : Collections.list(nets)) {
        // System.out.println("netInf VALUES"+" "+netInf);

        if (!netInf.isLoopback()) {
          if (netInf.isPointToPoint()) {
            // System.out.println("in point to point");
            // System.out.println("netInf VALUES"+" "+netInf);
            Enumeration<InetAddress> inetAdd = netInf.getInetAddresses();
            // System.out.println("inetAdd VALUES"+" "+inetAdd);
            for (InetAddress inet : Collections.list(inetAdd)) {
              // System.out.println("inet vales in the ineer loop"+" "+inet);
              // System.out.println("linkPPP vales before add in the ineer loop"+" "+linkPPP);
              linkPPP.add(inet.getHostAddress()); // 				
              // System.out.println("linkPPP vales after add in the ineer loop"+" "+linkPPP);
              // System.out.println("IP:"+" "+inet.getHostAddress());
            }
          } else {
            Enumeration<InetAddress> inetAdd = netInf.getInetAddresses();
            for (InetAddress inet : Collections.list(inetAdd)) {
              linkDSL.add(inet.getHostAddress());
            }
          }
        }
      }
      // System.out.println("FINAL VALUE of linkPPP"+" "+linkPPP.get(0));
      // System.out.println("FINAL VALUES DSL"+" "+linkDSL);
      int i = linkPPP.size();
      if (i != 0) {
        // System.out.println("linkPPP"+" "+linkPPP.get(i-1));
        return linkPPP.get(i - 1) + ",y";
        // return linkPPP.get(0)+",y";
      } else if (linkDSL.size() != 0) return linkDSL.get(i - 1) + ",n";
      // return linkDSL.get(0)+",n";
      else return "127.0.0.1,n";
    } catch (SocketException e) {
      // e.printStackTrace();
      System.err.println("exception in ");
    }

    return "127.0.0.1,n";
  }
  private List<String> getServerInterfaces() {

    List<String> bindInterfaces = new ArrayList<String>();

    String interfaceName = JiveGlobals.getXMLProperty("network.interface");
    String bindInterface = null;
    if (interfaceName != null) {
      if (interfaceName.trim().length() > 0) {
        bindInterface = interfaceName;
      }
    }

    int adminPort = JiveGlobals.getXMLProperty("adminConsole.port", 9090);
    int adminSecurePort = JiveGlobals.getXMLProperty("adminConsole.securePort", 9091);

    if (bindInterface == null) {
      try {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netInterface : Collections.list(nets)) {
          Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
          for (InetAddress address : Collections.list(addresses)) {
            if ("127.0.0.1".equals(address.getHostAddress())) {
              continue;
            }
            if (address.getHostAddress().startsWith("0.")) {
              continue;
            }
            Socket socket = new Socket();
            InetSocketAddress remoteAddress =
                new InetSocketAddress(address, adminPort > 0 ? adminPort : adminSecurePort);
            try {
              socket.connect(remoteAddress);
              bindInterfaces.add(address.getHostAddress());
              break;
            } catch (IOException e) {
              // Ignore this address. Let's hope there is more addresses to validate
            }
          }
        }
      } catch (SocketException e) {
        // We failed to discover a valid IP address where the admin console is running
        return null;
      }
    } else {
      bindInterfaces.add(bindInterface);
    }

    return bindInterfaces;
  }
示例#3
0
 public static void main(String[] args) {
   try {
     Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
     for (NetworkInterface netint : Collections.list(nets)) displayInfo(netint);
     pinging();
   } catch (java.net.SocketException s) {
     System.out.print('/'); //
     System.exit(0);
   } catch (IOException e) {
     e
         .printStackTrace(); // To change body of catch statement use File | Settings | File
                             // Templates.
   }
 }
示例#4
0
文件: Util.java 项目: gubo/slipwire
  /** @return */
  public static String getIPv4Address() {
    String ipv4address = null;

    try {
      final List<NetworkInterface> networkinterfaces =
          Collections.list(NetworkInterface.getNetworkInterfaces());
      for (final NetworkInterface networkinterface : networkinterfaces) {
        final List<InetAddress> addresses = Collections.list(networkinterface.getInetAddresses());
        for (final InetAddress address : addresses) {
          if ((address == null) || address.isLoopbackAddress()) {
            continue;
          }
          if (address instanceof Inet4Address) {
            ipv4address = address.getHostAddress().toString();
            break;
          }
        }
      }
    } catch (Exception x) {
      DBG.m(x);
    }

    return ipv4address;
  }
 /**
  * Get IP address from first non-localhost interface
  *
  * @param ipv4 true=return ipv4, false=return ipv6
  * @return address or empty string
  */
 public static String getIPAddress(boolean useIPv4) {
   try {
     List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
     for (NetworkInterface intf : interfaces) {
       List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
       for (InetAddress addr : addrs) {
         if (!addr.isLoopbackAddress()) {
           String sAddr = addr.getHostAddress().toUpperCase();
           boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
           if (useIPv4) {
             if (isIPv4) return sAddr;
           } else {
             if (!isIPv4) {
               int delim = sAddr.indexOf('%'); // drop ip6 port suffix
               return delim < 0 ? sAddr : sAddr.substring(0, delim);
             }
           }
         }
       }
     }
   } catch (Exception ex) {
   } // for now eat exceptions
   return "";
 }