dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Intermediate
Jul 9, 2018

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Find the Hostname of a Server Using the IP Address

The Java.net package has an API that does the hard work of collecting all the details and returns the server name. It's easy with the code snippet listed below.

Pass the IP address as an argument and the code will retrieve the hostname if it is valid.

import java.net.*;

public class FindHostnameUsingIPAddress
{
   public static void main(String args[])
   {
      FindHostnameUsingIPAddress findHostnameUsingIPAddress = new FindHostnameUsingIPAddress();
      findHostnameUsingIPAddress.proceed(args[0]);
   }
   
   private void proceed(String ipAddr)
   {
      try
      {
         String hostname = InetAddress.getByName(ipAddr).getHostName();
         System.out.println("Hostname of " + ipAddr + " is " + hostname);
      }catch(Exception e)
      {
         System.out.println("Exception: "+e);
      }
   }
}

/*

Expected output:

[root@mypc]# java FindHostnameUsingIPAddress 137.254.120.50
Hostname of 137.254.120.50 is vp-ocoma-cms-adc.oracle.com

*/
M S Sridhar
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date