Skip to content

Instantly share code, notes, and snippets.

@tnsicdr
Last active September 27, 2016 18:34
Show Gist options
  • Save tnsicdr/191ab439b760084ca592b02300d7d8ff to your computer and use it in GitHub Desktop.
Save tnsicdr/191ab439b760084ca592b02300d7d8ff to your computer and use it in GitHub Desktop.
CIS457 - IPFinder2.java
package com.github.seitensei;
import java.net.*;
import java.io.*;
public class IPFinder2 {
public static void main(String[] args) throws IOException {
// Get name and IP address of local host
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println("Local Hostname and its IP address: "
+ address.getHostName() + "/" + address.getHostAddress());
System.out.println("Local Host Address:");
System.out.println("\t" + address.getHostAddress());
System.out.println("Local Host Name:");
System.out.println("\t" + address.getHostName());
} catch (UnknownHostException e) {
System.out.println("Unable to determine this host's address");
}
// Get Host and IP for CLI arguments
for (String s: args) {
try {
System.out.println(s + ":");
InetAddress[] addressList = InetAddress.getAllByName(s);
System.out.println("\t" + addressList[0].getHostName());
for (int j = 0; j < addressList.length; j++) {
System.out.println("\t" + addressList[j].getHostAddress());
}
} catch (UnknownHostException e) {
System.out.println("\tUnable to find address");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment