Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get IP address of your application server

I need to be able to find the IP address of the server the page is currently executing on. I have some code that calls a third party site and has to pass a specific key that changes depending on which server it is on. Is there a CGI variable or some way in ColdFusion to determine what the IP address is of the host server?

like image 940
Jason Avatar asked Apr 12 '10 18:04

Jason


3 Answers

Like the other commenters have outlined if you need the external IP as the third party site sees it then you probably should use the external approaches they recommend.

However if the third party is giving you access in some form that is based on an actual IP as the Server sees itself and not the IP as they see it you can use

 <cfset cName = CreateObject("java", "java.net.InetAddress").getLocalHost().getHostAddress()>

 <cfdump var="#cName#">
like image 195
kevink Avatar answered Nov 07 '22 10:11

kevink


There are two reasons why a program can't query the host it's running on and see what its IP is:

  1. It might have multiple ips, and short of looking through all sorts of kernel data structures you're unlikely to know which one is going to be used for a given outgoing connection.

  2. It might connect to the outside world though a NAT firewall or some sort of proxy so that the outside world will see a different IP than any of the ones configured on your box.

Actually, there might be more than those two, but those are ones that have occurred to me.

Because of that, the simplest way is to connect to another box somewhere outside of your corporate network and see what IP it thinks you have. I use a two line CGI script running on my colo box to detect what IP my home server currently has (so I can detect when the cable company changes it).

like image 28
Paul Tomblin Avatar answered Nov 07 '22 11:11

Paul Tomblin


You can use CGI.LOCAL_ADDR to determine the IP-address of your server (CFML equivalent to PHP's $_SERVER["SERVER_ADDR"]). It works on IIS and Apache using ColdFusion or Railo, given you are not behind a Proxy, not natting your server IP and having only one IP assigned to your server (not sure which IP would be shown, if there are more than one).

like image 4
Andreas Schuldhaus Avatar answered Nov 07 '22 10:11

Andreas Schuldhaus