All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openqa.selenium.NetworkUtils Maven / Gradle / Ivy

There is a newer version: 2.0b1
Show newest version
package org.openqa.selenium;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;

public class NetworkUtils {
  public static String getPrivateLocalAddress() {
      try {
          ArrayList ips = new ArrayList();
          Enumeration m = NetworkInterface.getNetworkInterfaces();
          while(m.hasMoreElements()) {
              NetworkInterface ni = m.nextElement();
              Enumeration addresses = ni.getInetAddresses();
              while (addresses.hasMoreElements()) {
                  InetAddress address = addresses.nextElement();
                  String ip = address.getHostAddress();

                  // filter out Inet6 Addr Entries
                  if (ip.indexOf(":") == -1) {
                      ips.add(ip);
                  }
              }
          }

          if (!ips.isEmpty()) {
              // still didn't find an ideal one? just pick the first one
              return ips.get(0);
          }
      } catch (SocketException e) {
          // ignore
      }

      return "127.0.0.1"; // shitty default, sorry wireless Ubuntu users!
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy