Programming > Java

Local Server IP hostname 구하기

#java#get server ip

ServerName : 호스트명 구하기

InetAddress.getLocalHost().getCanonicalHostName()
or
System.getenv("userdomain")

서버 IP구하기

private static void showIP() throws Exception {
    
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface n = (NetworkInterface) e.nextElement();
        Enumeration<InetAddress> ee = n.getInetAddresses();
        while (ee.hasMoreElements()) {
            InetAddress i = (InetAddress) ee.nextElement();
            if(i.isLoopbackAddress()) continue;
            if(i.isLinkLocalAddress()) continue;
            if(!i.isSiteLocalAddress()) continue;
            
            System.out.println(i.getHostAddress());
        }
    }
}