Getting the Current IP Address
Although this under the "API" category, this is a Java tip. (I don't have a Java category... there's a JavaScript category, but this is strictly Java).I was working on an issue where a server of mine was being blocked by another server. They wanted to make sure I was whitelisted, so they asked me for my IP address. I went out to Google, asked it what my IP address was, and it told me and I told the other server's administrator. Then, a few weeks later, the same blocking was happening. Naturally, they asked me again what my IP address was and I had to go out to Google again and find out. I decided that I would start logging, every hour, what my IP address was.
Well, I found some code to get your current IP address, but that gets your INTERNAL address, not what the rest of the world sees. So I needed a way to get my public IP address. There's a service through Amazon that will tell you - it will return just your IP address. Here's the code that will give you both your private and public IP address. It's written in Java. Note that in LotusScript, there's ways to do this in Notes 10 and higher, but I was on a Notes 9 server, so that's why it had to be written in Java.
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
//import javax.net.ssl.HttpsURLConnection;
import java.net.InetAddress;
import java.net.URL;
import lotus.domino.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
// Get the private IP Address of this machine
InetAddress thisIp = InetAddress.getLocalHost();
String ipAddress = thisIp.getHostAddress();
// The code is omitted here to do something with that value - log it to a document, whatever
// Find public IP address by calling a service
String systemipaddress = "";
try {
URL url; // The URL to read
HttpURLConnection conn = null; // The actual connection to the web page
BufferedReader rd; // Used to read results from the web page
String line; // An individual line of the web page HTML
// If you want to use SSL, uncomment the following line as well as the commented-out include statement above
//url = new URL("https://bot.whatismyipaddress.com");
url = new URL("http://checkip.amazonaws.com");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
systemipaddress = systemipaddress + line;
}
rd.close();
} catch (Exception e) {
systemipaddress = e.getLocalizedMessage();
}
// The code is omitted here to do something with that value - log it to a document, whatever
} catch (Exception e) {
e.printStackTrace();
}
}
}
Note that if you use this code as-is, you'll be setting two variables but never reading them. I left out that part where the values were getting logged - it was irrelevant to the actual tip here.
The variable ipAddress is the internal IP address (the one given to you by your router/gateway). The variable systemipaddress is the one that all the other computers in the world see. Note that it's possible that you get an error when trying to read that Amazon URL - in that case, the IP address is the error message. I periodically get a 403 error message and I simply don't log those when that happens.
I also use the non-SSL version in this code but also include the code to use the SSL version. I seemed to have a few more issues on my server when using the SSL version, so that's why I ultimately went with the HTTP version instead of the HTTPS version.