/*
* @author charles66820
*
* javac InternetIp.java
* jar -e InternetIp -c -v -f internetIp.jar InternetIp.class
*/
import java.io.*;
import java.net.URL;
import java.util.Date;
import java.text.SimpleDateFormat;
public class InternetIp {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
public static void main(String[] args) {
BufferedReader in = null;
String ip = null;
try {
// Create ip request
URL whatismyip = new URL("http://checkip.amazonaws.com");
in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
ip = in.readLine();
} catch (Exception e) {
System.out.println("Error on Get ip");
} finally {
try {
if (in != null) in.close();
} catch (IOException e) {
System.out.println("Error on close ip request");
}
}
// Print ip in file
try (PrintWriter outip = new PrintWriter(new FileWriter("internetNodeIp.log", true))) {
outip.format("[%s] : %s\n", dateFormat.format(new Date()), ip);
} catch (Exception e) {
System.out.println("Error on print ip in file");
}
}
}
[2021-04-24 23:47:00+0200] : 12.345.67.89
import java.net.*;
import java.util.*;
public class ListNets {
public static void main(String[] args) throws SocketException {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
System.out.format(
"Display name: %s\nName: %s\n",
netint.getDisplayName(),
netint.getName()
);
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses))
System.out.format(" - InetAddress: %s\n", inetAddress);
System.out.println();
}
}
}
Display name: lo
Name: lo
- InetAddress: /0:0:0:0:0:0:0:1%lo
- InetAddress: /127.0.0.1
Name: lo
- InetAddress: /0:0:0:0:0:0:0:1%lo
- InetAddress: /127.0.0.1