use of java.net.InterfaceAddress in project crate by crate.
the class IfConfig method doLogging.
/**
* perform actual logging: might throw exception if things go wrong
*/
private static void doLogging() throws IOException {
StringBuilder msg = new StringBuilder();
for (NetworkInterface nic : NetworkUtils.getInterfaces()) {
msg.append(System.lineSeparator());
// ordinary name
msg.append(nic.getName());
msg.append(System.lineSeparator());
// display name (e.g. on windows)
if (!nic.getName().equals(nic.getDisplayName())) {
msg.append(INDENT);
msg.append(nic.getDisplayName());
msg.append(System.lineSeparator());
}
// addresses: v4 first, then v6
List<InterfaceAddress> addresses = nic.getInterfaceAddresses();
for (InterfaceAddress address : addresses) {
if (address.getAddress() instanceof Inet6Address == false) {
msg.append(INDENT);
msg.append(formatAddress(address));
msg.append(System.lineSeparator());
}
}
for (InterfaceAddress address : addresses) {
if (address.getAddress() instanceof Inet6Address) {
msg.append(INDENT);
msg.append(formatAddress(address));
msg.append(System.lineSeparator());
}
}
// hardware address
byte[] hardware = nic.getHardwareAddress();
if (hardware != null) {
msg.append(INDENT);
msg.append("hardware ");
for (int i = 0; i < hardware.length; i++) {
if (i > 0) {
msg.append(":");
}
msg.append(String.format(Locale.ROOT, "%02X", hardware[i]));
}
msg.append(System.lineSeparator());
}
// attributes
msg.append(INDENT);
msg.append(formatFlags(nic));
msg.append(System.lineSeparator());
}
LOGGER.debug("configuration:{}{}", System.lineSeparator(), msg);
}
use of java.net.InterfaceAddress in project zeppelin by apache.
the class RemoteInterpreterUtils method findAvailableHostAddress.
public static String findAvailableHostAddress() throws UnknownHostException, SocketException {
String zeppelinServerIP = System.getenv("ZEPPELIN_LOCAL_IP");
if (zeppelinServerIP != null) {
return zeppelinServerIP;
}
InetAddress address = InetAddress.getLocalHost();
if (address.isLoopbackAddress()) {
for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
if (!networkInterface.isLoopback()) {
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress a = interfaceAddress.getAddress();
if (a instanceof Inet4Address) {
return a.getHostAddress();
}
}
}
}
}
return address.getHostAddress();
}
use of java.net.InterfaceAddress in project cloudstack by apache.
the class NetUtils method getAllDefaultNicIps.
public static List<String> getAllDefaultNicIps() {
final List<String> addrs = new ArrayList<>();
final String pubNic = getDefaultEthDevice();
if (pubNic == null) {
return addrs;
}
NetworkInterface nic = null;
try {
nic = NetworkInterface.getByName(pubNic);
} catch (final SocketException e) {
return addrs;
}
for (InterfaceAddress address : nic.getInterfaceAddresses()) {
addrs.add(address.getAddress().getHostAddress().split("%")[0]);
}
return addrs;
}
use of java.net.InterfaceAddress in project platformlayer by platformlayer.
the class InetAddressUtils method getLocalAddresses.
public static List<InetAddress> getLocalAddresses() {
List<InetAddress> addresses = Lists.newArrayList();
Enumeration<NetworkInterface> networkInterfaces;
try {
networkInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
throw new IllegalStateException("Error reading network addresses", e);
}
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
for (InterfaceAddress interfaceAddress : interfaceAddresses) {
InetAddress address = interfaceAddress.getAddress();
addresses.add(address);
}
}
return addresses;
}
use of java.net.InterfaceAddress in project AndroidUtilCode by Blankj.
the class NetworkUtils method getBroadcastIpAddress.
/**
* Return the ip address of broadcast.
*
* @return the ip address of broadcast
*/
public static String getBroadcastIpAddress() {
try {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
LinkedList<InetAddress> adds = new LinkedList<>();
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
if (!ni.isUp() || ni.isLoopback())
continue;
List<InterfaceAddress> ias = ni.getInterfaceAddresses();
for (int i = 0, size = ias.size(); i < size; i++) {
InterfaceAddress ia = ias.get(i);
InetAddress broadcast = ia.getBroadcast();
if (broadcast != null) {
return broadcast.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return "";
}
Aggregations