public static NetworkInterface findInterfaceWithScopeId(String scopeName) { final Enumeration<NetworkInterface> enumeration; try { enumeration = NetworkInterface.getNetworkInterfaces(); } catch (SocketException ignored) { return null; } while (enumeration.hasMoreElements()) { final NetworkInterface net = enumeration.nextElement(); if (net.getName().equals(scopeName)) { return net; } } return null; }
@Override public NetworkStats networkStats() { String primaryInterface = null; final Map<String, NetworkStats.Interface> interfaces = new HashMap<>(); try { final String localAddress = nullToEmpty(InetAddress.getLocalHost().getHostAddress()); final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netIf : Collections.list(networkInterfaces)) { final String name = netIf.getName(); final NetworkStats.Interface networkInterface = buildInterface(netIf); interfaces.put(name, networkInterface); if (networkInterface.addresses().contains(localAddress)) { primaryInterface = name; } } } catch (Exception e) { // ignore } return NetworkStats.create(primaryInterface, interfaces, null); }
protected void bindToInterfaces(List<NetworkInterface> interfaces, MulticastSocket s, InetAddress mcast_addr) throws IOException { SocketAddress tmp_mcast_addr=new InetSocketAddress(mcast_addr, mcast_port); for(Iterator it=interfaces.iterator(); it.hasNext();) { NetworkInterface i=(NetworkInterface)it.next(); for(Enumeration en2=i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr=(InetAddress)en2.nextElement(); if ((Util.getIpStackType() == StackType.IPv4 && addr instanceof Inet4Address) || (Util.getIpStackType() == StackType.IPv6 && addr instanceof Inet6Address)) { s.joinGroup(tmp_mcast_addr, i); log.trace("joined " + tmp_mcast_addr + " on " + i.getName() + " (" + addr + ")"); break; } } } }
public static void main(String[] args) throws SocketException { Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces(); while(en.hasMoreElements()) { NetworkInterface i=en.nextElement(); System.out.println(i.getName() + ':'); System.out.println(" \t" + i.getDisplayName()); for(Enumeration<InetAddress> en2=i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr=en2.nextElement(); System.out.println(" \t" + addr + " (" + addr.getHostName() + ')'); } System.out.println("---------------------"); } }
/** * Return a list of internet addresses sorted by importance (most important first). * This is the order we want to test for matches when selecting an internet address. */ public static ArrayList<java.net.InetAddress> calcPrioritizedInetAddressList() { ArrayList<java.net.InetAddress> ips = new ArrayList<java.net.InetAddress>(); { ArrayList<NetworkInterface> networkInterfaceList = calcPrioritizedInterfaceList(); for (int i = 0; i < networkInterfaceList.size(); i++) { NetworkInterface ni = networkInterfaceList.get(i); Enumeration<InetAddress> ias = ni.getInetAddresses(); while( ias.hasMoreElements() ) { InetAddress ia; ia = ias.nextElement(); ips.add(ia); Log.info("Possible IP Address: " + ni.getName() + " (" + ni.getDisplayName() + "), " + ia.getHostAddress()); } } } return ips; }
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { if (!intf.getName().equalsIgnoreCase(interfaceName)) continue; List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
/** * Returns server IP address (v4 or v6) bound to local NIC. If multiple NICs are present, choose 'eth0' or 'en0' or * any one with name ending with 0. If non found, take first on the list that is not localhost. If no NIC * present (relevant only for desktop deployments), return loopback address. */ public static String getServerIPv4() { String candidateAddress = null; try { Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); while (nics.hasMoreElements()) { NetworkInterface nic = nics.nextElement(); Enumeration<InetAddress> inetAddresses = nic.getInetAddresses(); while (inetAddresses.hasMoreElements()) { String address = inetAddresses.nextElement().getHostAddress(); String nicName = nic.getName(); if (nicName.startsWith("eth0") || nicName.startsWith("en0")) { return address; } if (nicName.endsWith("0") || candidateAddress == null) { candidateAddress = address; } } } } catch (SocketException e) { throw new RuntimeException("Cannot resolve local network address", e); } return candidateAddress == null ? "127.0.0.1" : candidateAddress; }
InetAddress ia = addressList.nextElement(); if (ia.getAddress().length == 16) { Inet6Address v6ia = (Inet6Address) ia; address.scope_id = v6ia.scope_id; address.scope_ifname_set = true; address.ifname = nif.getName(); break;
public static String getMacAddr() { try { List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif : all) { if (!nif.getName().equalsIgnoreCase("wlan0")) continue; byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null) { return ""; } StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) { res1.append(String.format("%02X:",b)); } if (res1.length() > 0) { res1.deleteCharAt(res1.length() - 1); } return res1.toString(); } } catch (Exception ex) { } return "02:00:00:00:00:00"; }
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); while (nis.hasMoreElements()) { NetworkInterface ni = nis.nextElement(); System.out.println(ni.getName() + " " + ni.getDisplayName()); }
private void bindToInterfaces(List<NetworkInterface> interfaces, MulticastSocket s, InetAddress mcast_addr) throws IOException { SocketAddress tmp_mcast_addr=new InetSocketAddress(mcast_addr, mcast_port); for(Iterator it=interfaces.iterator(); it.hasNext();) { NetworkInterface i=(NetworkInterface)it.next(); for(Enumeration en2=i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr=(InetAddress)en2.nextElement(); s.joinGroup(tmp_mcast_addr, i); if(log.isTraceEnabled()) log.trace("joined " + tmp_mcast_addr + " on " + i.getName() + " (" + addr + ")"); break; } } }
public static String getWifiMacAddress() { try { String interfaceName = "wlan0"; List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (!intf.getName().equalsIgnoreCase(interfaceName)){ continue; } byte[] mac = intf.getHardwareAddress(); if (mac==null){ return ""; } StringBuilder buf = new StringBuilder(); for (byte aMac : mac) { buf.append(String.format("%02X:", aMac)); } if (buf.length()>0) { buf.deleteCharAt(buf.length() - 1); } return buf.toString(); } } catch (Exception ex) { } // for now eat exceptions return ""; }
public static String getLocalhostByNetworkInterface() throws SocketException { List<String> candidatesHost = new ArrayList<String>(); Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface networkInterface = enumeration.nextElement(); // Workaround for docker0 bridge if ("docker0".equals(networkInterface.getName()) || !networkInterface.isUp()) { continue; } Enumeration<InetAddress> addrs = networkInterface.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress address = addrs.nextElement(); if (address.isLoopbackAddress()) { continue; } //ip4 highter priority if (address instanceof Inet6Address) { candidatesHost.add(address.getHostAddress()); continue; } return address.getHostAddress(); } } if (!candidatesHost.isEmpty()) { return candidatesHost.get(0); } return null; }
//set Network Interface NetworkInterface nif = NetworkInterface.getByName("tun0"); if(nif==null){ System.err.println("Error getting the Network Interface"); return; } System.out.println("Preparing to using the interface: "+nif.getName()); Enumeration<InetAddress> nifAddresses = nif.getInetAddresses(); InetSocketAddress inetAddr= new InetSocketAddress(nifAddresses.nextElement(),0); //socket.bind(new InetSocketAddress(nifAddresses.nextElement(), 0)); DatagramSocket socket = new DatagramSocket(inetAddr); System.out.println("Interface setted");
private static String getMacAddress(Context context) { try { final List<NetworkInterface> networksInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface interfaces : networksInterfaces) { if (interfaces.getName().equalsIgnoreCase("wlan0")) { final byte[] address = interfaces.getHardwareAddress(); if (address != null) { final StringBuilder sb = new StringBuilder(); for (byte b : address) { sb.append(String.format("%02X:", b)); } final int length = sb.length(); if (length > 0) { sb.deleteCharAt(length - 1); } return sb.toString(); } } } } catch (SocketException ignored) {} return ""; } }
/** Finds a network interface of sub-interface with the given name */ public static NetworkInterface getByName(String name) throws SocketException { if(name == null) return null; Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces(); while(en.hasMoreElements()) { NetworkInterface intf=en.nextElement(); if(intf.getName().equals(name)) return intf; Enumeration<NetworkInterface> en2=intf.getSubInterfaces(); while(en2.hasMoreElements()) { NetworkInterface intf2=en2.nextElement(); if(intf2.getName().equals(name)) { return intf2; } } } return null; }
private static List<String> listSubIp(NetworkInterface networkInterface) { List<String> subIpList = new ArrayList<String>(); Enumeration<NetworkInterface> subInterfaces = networkInterface.getSubInterfaces(); while (subInterfaces.hasMoreElements()) { NetworkInterface net = subInterfaces.nextElement(); List<String> ipList = listAllIp(net); String name = net.getName(); System.out.println("name:" + name + " ipList:" + ipList); subIpList.addAll(ipList); } return subIpList; }
@SuppressLint({"NewApi"}) public static String getWifiMacAddress() { try { String str = "wlan0"; for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) { if (networkInterface.getName().equalsIgnoreCase(str)) { byte[] hardwareAddress = networkInterface.getHardwareAddress(); if (hardwareAddress == null) { return null; } StringBuilder stringBuilder = new StringBuilder(); int length = hardwareAddress.length; for (int i = 0; i < length; i++) { stringBuilder.append(String.format("%02X", new Object[]{Byte.valueOf(hardwareAddress[i])})); } return stringBuilder.toString(); } } } catch (Exception e) { e.printStackTrace(); } return null; }
import java.net.*; import java.util.*; public class Test { public static void main(String[] args) throws Exception // Just for simplicity { for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) { NetworkInterface iface = ifaces.nextElement(); System.out.println(iface.getName() + ":"); for (Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses.hasMoreElements(); ) { InetAddress address = addresses.nextElement(); System.out.println(" " + address); } } } }
private void bindToInterfaces(List<NetworkInterface> interfaces, MulticastSocket s, InetAddress mcast_addr) throws IOException { SocketAddress tmp_mcast_addr=new InetSocketAddress(mcast_addr, mcast_port); for(Iterator it=interfaces.iterator(); it.hasNext();) { NetworkInterface i=(NetworkInterface)it.next(); for(Enumeration en2=i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr=(InetAddress)en2.nextElement(); s.joinGroup(tmp_mcast_addr, i); if(log.isTraceEnabled()) log.trace("joined " + tmp_mcast_addr + " on " + i.getName() + " (" + addr + ")"); break; } } }