Monday, October 28, 2013

How to know External IP of a intranet IP

Let say you have a situation that your application runs on Intranet and you want to communicate with  other application which is behind the firewall. so they need the External IP to allow traffic
from fixed IP(s).

Below is the code which shows how to get the External IP in colfusion / Java.

IN ColdFusion
------------------
create a File IPAddress.cfm
<cfset sURL = "http://checkip.amazonaws.com">
 <cfhttp method="get" url="#sURL#>
 <cfdump var="#cfhttp.filecontent#">

IN JAVA
------------
create a class IPAddress.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class IPAddress {

    public static String getIp() throws Exception {
        URL whatismyip = new URL("http://checkip.amazonaws.com");
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(
                    whatismyip.openStream()));
            String ip = in.readLine();
            return ip;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try{
        System.out.println(getIp());
        }
        catch(Exception e){
            System.out.println("exception:"+e.getMessage());
        }
    }
   
}


compile this file on your machine and run you will get the External ip.

4 comments:

  1. Java's inetAddress would have been better, and this code I have used in ColdFusion with CreateObject succesfully many times to do this.

    http://www.ensode.net/nslookup.html

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Nice information. I had checked my External ip address using this site IP-Details.com

    ReplyDelete