Java Host Name Get getHostNameByNic()

Here you can find the source of getHostNameByNic()

Description

Iterates through the found NICs, extracting the host name if the NIC is not the loopback interface.

License

Apache License

Return

The host name or null if one was not found.

Declaration

public static String getHostNameByNic() 

Method Source Code

//package com.java2s;
/*//from  ww  w  . j av a  2  s .co  m
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.InetAddress;
import java.net.NetworkInterface;

import java.util.Enumeration;

import java.util.regex.Pattern;

public class Main {
    /** IP4 address pattern matcher */
    public static final Pattern IP4_ADDRESS = Pattern
            .compile("((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])");
    /** IP6 address pattern matcher */
    public static final Pattern IP6_ADDRESS = Pattern.compile(
            "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])).*?");

    /**
     * Iterates through the found NICs, extracting the host name if the NIC is not the loopback interface.
     * The host name is extracted from the first address bound to the first matching NIC that does not 
     * have a canonical name that is an IP address.
     * @return The host name or null if one was not found.
     */
    public static String getHostNameByNic() {
        try {
            for (Enumeration<NetworkInterface> nicEnum = NetworkInterface.getNetworkInterfaces(); nicEnum
                    .hasMoreElements();) {
                NetworkInterface nic = nicEnum.nextElement();
                if (nic != null && nic.isUp() && !nic.isLoopback()) {
                    for (Enumeration<InetAddress> nicAddr = nic.getInetAddresses(); nicAddr.hasMoreElements();) {
                        InetAddress addr = nicAddr.nextElement();
                        String chost = addr.getCanonicalHostName();
                        if (chost != null && !chost.trim().isEmpty()) {
                            if (!IP4_ADDRESS.matcher(chost).matches() && !IP6_ADDRESS.matcher(chost).matches()) {
                                return chost;
                            }
                        }
                    }
                }
            }
            return null;
        } catch (Exception x) {
            return null;
        }
    }
}

Related

  1. getHostName(Proxy proxy)
  2. getHostName(String destination)
  3. getHostName(String host)
  4. getHostname(String targetEndpoint)
  5. getHostNameByInet()
  6. getHtmlSource(final String host)
  7. getInetHost()
  8. getLedgerAllocatorPoolName(int serverRegionId, int shardId, boolean useHostname)
  9. getLocalCanonicalHostName()