开发者

How to get IP address of your application server

开发者 https://www.devze.com 2022-12-27 03:26 出处:网络
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

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开发者_如何学Python or some way in ColdFusion to determine what the IP address is of the host server?


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#">


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).


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).


I would use hostip.info API
Your IP in XML: http://api.hostip.info/get_xml.php
Your IP in HTML: http://api.hostip.info/get_html.php


The safest way will be to use a service like WhatIsMyIP. If the server is behind a NAT, then the OS has no knowledge of the external IP address.

There are many questions in SO regarding this, see here for example.


<cfhttp url="http://www.whatismyip.com/" result="myresult" resolveurl="yes">
<cfoutput>#myresult.filecontent#</cfoutput>
0

精彩评论

暂无评论...
验证码 换一张
取 消