Example usage for java.net NetworkInterface getName

List of usage examples for java.net NetworkInterface getName

Introduction

In this page you can find the example usage for java.net NetworkInterface getName.

Prototype

public String getName() 

Source Link

Document

Get the name of this network interface.

Usage

From source file:license.regist.ReadProjectInfo.java

private static boolean checkMac(Map<Integer, String> infoMap) throws SocketException {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    Base64 base64 = new Base64();
    int i = 12;/*from   w  w  w  .  java2s .  co  m*/
    for (NetworkInterface ni : Collections.list(interfaces)) {
        if (ni.getName().substring(0, 1).equalsIgnoreCase("e")) {
            if (!((String) infoMap.get(Integer.valueOf(i))).equalsIgnoreCase(ni.getName()))
                return false;
            i++;
            byte[] mac = ni.getHardwareAddress();

            if (!((String) infoMap.get(Integer.valueOf(i))).equalsIgnoreCase(base64.encodeAsString(mac))) {
                return false;
            }
            i++;
        }
    }
    return true;
}

From source file:com.frostwire.android.gui.NetworkManager.java

private static List<String> getInterfaceNames() {
    List<String> names = new LinkedList<>();
    try {/* w w  w . j a  va2s.  c  om*/
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        if (networkInterfaces != null) {
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                String name = networkInterface.getName();
                if (name != null) {
                    names.add(name);
                }
            }
        }
    } catch (Throwable e) {
        // ignore
        // important, but no need to crash the app
    }

    return names;
}

From source file:Main.java

public static String print(List<NetworkInterface> interfaces) {
    StringBuilder sb = new StringBuilder();
    boolean first = true;

    for (NetworkInterface intf : interfaces) {
        if (first) {
            first = false;/*from w w  w  .ja va 2 s .co m*/
        } else {
            sb.append(", ");
        }
        sb.append(intf.getName());
    }
    return sb.toString();
}

From source file:ai.general.interbot.NetworkInterfaceInfo.java

/**
 * Collects information about the local network interfaces and returns the information as an
 * array of NetworkInterfaceInfo objects.
 *
 * @return Information about the local network interfaces.
 */// w w  w  .j  a  va  2s. c  o m
public static ArrayList<NetworkInterfaceInfo> queryAll() {
    ArrayList<NetworkInterfaceInfo> infos = new ArrayList<NetworkInterfaceInfo>();
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface iface = interfaces.nextElement();
            String name = iface.getName();
            if (name.startsWith("eth") || name.startsWith("wlan")) {
                NetworkInterfaceInfo info = new NetworkInterfaceInfo(name);
                Enumeration<InetAddress> addresses = iface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    if (address instanceof Inet4Address) {
                        info.getAddresses()
                                .add(new IpAddress(IpAddress.Version.IPv4, inetAddressToString(address)));
                    } else if (address instanceof Inet6Address) {
                        info.getAddresses()
                                .add(new IpAddress(IpAddress.Version.IPv6, inetAddressToString(address)));
                    }
                }
                if (info.getAddresses().size() > 0) {
                    infos.add(info);
                }
            }
        }
    } catch (SocketException e) {
    }
    return infos;
}

From source file:com.DPFaragir.DPFUtils.java

public static String getMACAddress(String interfaceName) {
    try {//from ww  w .  ja  v  a2  s .  co m
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            byte[] mac = intf.getHardwareAddress();
            if (mac == null)
                return "";
            StringBuilder buf = new StringBuilder();
            for (int idx = 0; idx < mac.length; idx++)
                buf.append(String.format("%02X:", mac[idx]));
            if (buf.length() > 0)
                buf.deleteCharAt(buf.length() - 1);
            return buf.toString();
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
    /*try {
    // this is so Linux hack
    return loadFileAsString("/sys/class/net/" +interfaceName + "/address").toUpperCase().trim();
    } catch (IOException ex) {
    return null;
    }*/
}

From source file:com.rincliu.library.util.RLNetUtil.java

/**
 * Returns MAC address of the given interface name.
 * //from   w  w  w.  j  a v a 2  s .c o  m
 * @param interfaceName eth0, wlan0 or NULL=use first interface
 * @return mac address or empty string
 */
public static String getMACAddress(String interfaceName) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            byte[] mac = intf.getHardwareAddress();
            if (mac == null)
                return "";
            StringBuilder buf = new StringBuilder();
            for (int idx = 0; idx < mac.length; idx++)
                buf.append(String.format("%02X:", mac[idx]));
            if (buf.length() > 0)
                buf.deleteCharAt(buf.length() - 1);
            return buf.toString();
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
    // try
    // { // this is so Linux hack return
    // loadFileAsString("/sys/class/net/" + interfaceName +
    // "/address").toUpperCase().trim();
    // }
    // catch (IOException e)
    // {
    // e.printStackTrace();
    // return null;
    // }
}

From source file:org.sakuli.actions.environment.CipherUtil.java

/**
 * Determines a valid network interfaces.
 *
 * @return ethernet interface name//from w ww .  j a va  2s .  c om
 * @throws Exception
 */
static String autodetectValidInterfaceName() throws Exception {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface anInterface = interfaces.nextElement();
        if (anInterface.getHardwareAddress() != null && anInterface.getHardwareAddress().length == 6) {
            return anInterface.getName();
        }
    }
    throw new Exception("No network interface with a MAC address is present, please check your os settings!");
}

From source file:com.buaa.cfs.net.DNS.java

/**
 * @return NetworkInterface for the given subinterface name (eg eth0:0) or null if no interface with the given name
 * can be found//from  ww w. j  a  v a 2  s .c o m
 */
private static NetworkInterface getSubinterface(String strInterface) throws SocketException {
    Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();

    while (nifs.hasMoreElements()) {
        Enumeration<NetworkInterface> subNifs = nifs.nextElement().getSubInterfaces();

        while (subNifs.hasMoreElements()) {
            NetworkInterface nif = subNifs.nextElement();
            if (nif.getName().equals(strInterface)) {
                return nif;
            }
        }
    }
    return null;
}

From source file:com.moss.greenshell.wizard.catastrophe.PostMortemScreen.java

public static void submitErrorReport(final Throwable cause, final ErrorReportDecorator... decorators)
        throws Exception {

    List<ErrorReportChunk> chunks = new LinkedList<ErrorReportChunk>();

    try {/*  w w  w  .j a va2 s. co  m*/
        if (cause instanceof InternalErrorException) {
            InternalErrorException ie = (InternalErrorException) cause;
            ErrorReportChunk chunk = new ErrorReportChunk("internal-error-id", "text/plain",
                    ie.id().getBytes("UTF8"));
            chunks.add(chunk);
        } else if (cause instanceof SOAPFaultException) {
            SOAPFaultException soapFault = (SOAPFaultException) cause;
            String content = soapFault.getFault().getFirstChild().getTextContent();
            String prefix = "Internal Service Error Occurred: ";
            if (content.startsWith(prefix)) {
                String id = content.substring(prefix.length());
                ErrorReportChunk chunk = new ErrorReportChunk("internal-error-id", "text/plain",
                        id.getBytes("UTF8"));
                chunks.add(chunk);
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }

    // STACK TRACE
    ByteArrayOutputStream stackBytes = new ByteArrayOutputStream();
    PrintStream stackPrintStream = new PrintStream(stackBytes);
    cause.printStackTrace(stackPrintStream);
    stackPrintStream.close();
    stackBytes.close();

    ErrorReportChunk chunk = new ErrorReportChunk("stack trace", "text/plain", stackBytes.toByteArray());
    chunks.add(chunk);

    // THREAD DUMP

    ByteArrayOutputStream dumpBytes = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(dumpBytes);
    Map<Thread, StackTraceElement[]> traceMap = Thread.getAllStackTraces();
    for (Map.Entry<Thread, StackTraceElement[]> next : traceMap.entrySet()) {
        out.println();
        out.println(next.getKey().getName());
        for (StackTraceElement line : next.getValue()) {
            String className = emptyIfNull(line.getClassName());
            String methodName = emptyIfNull(line.getMethodName());
            String fileName = emptyIfNull(line.getFileName());

            out.println("    " + className + "." + methodName + " (" + fileName + " line "
                    + line.getLineNumber() + ")");
        }
    }
    out.flush();
    out.close();
    ErrorReportChunk stackDump = new ErrorReportChunk("thread dump", "text/plain", dumpBytes.toByteArray());
    chunks.add(stackDump);

    // SYSTEM PROPERTIES
    ByteArrayOutputStream propsBytes = new ByteArrayOutputStream();
    PrintStream propsOut = new PrintStream(propsBytes);
    for (Map.Entry<Object, Object> next : System.getProperties().entrySet()) {
        propsOut.println(" " + next.getKey() + "=" + next.getValue());
    }
    propsOut.flush();
    propsOut.close();
    chunks.add(new ErrorReportChunk("system properties", "text/plain", propsBytes.toByteArray()));

    // LOCAL CLOCK
    chunks.add(new ErrorReportChunk("local clock", "text/plain", new DateTime().toString().getBytes()));

    // NETWORKING
    StringBuffer networking = new StringBuffer();
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        networking.append("INTERFACE: " + iface.getName() + " (" + iface.getDisplayName() + ")\n");
        Enumeration<InetAddress> addresses = iface.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            networking.append("  Address:" + address.getHostAddress() + "\n");
            networking.append("      Cannonical Host Name: " + address.getCanonicalHostName() + "\n");
            networking.append("                 Host Name: " + address.getHostName() + "\n");
        }
    }
    chunks.add(new ErrorReportChunk("network configuration", "text/plain", networking.toString().getBytes()));

    // DECORATORS
    if (decorators != null) {
        for (ErrorReportDecorator decorator : decorators) {
            chunks.addAll(decorator.makeChunks(cause));
        }
    }
    ErrorReport report = new ErrorReport(chunks);
    Reporter reporter = new Reporter();
    ReportId id = reporter.submitReport(report);
}

From source file:org.esupportail.nfctagdroid.NfcTacDroidActivity.java

public static String getMacAddr() {
    try {/*from  w ww . jav  a 2s  .com*/
        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(Integer.toHexString(b & 0xFF) + ":");
            }

            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception e) {
        throw new NfcTagDroidException("can't get mac address", e);
    }
    return "";
}