Stable way of retrieving the external IP for a hos

2019-03-04 01:19发布

Basically I want to display a hosts external public facing IP address regardless of whether or not it is part of a natted lan. What I'm doing now is just connecting to myipaddress.com and retrieving it from there. I just don't know if I trust that site as a stable source. Is there some authority that facilitates this?

标签: networking
2条回答
祖国的老花朵
2楼-- · 2019-03-04 01:25

STUN RFC 3489will do it, though you need access to an open STUN server. There are other sites (like myipaddress.com) that will report your apparent address back to you, but there is no "standard" service for this.

查看更多
Melony?
3楼-- · 2019-03-04 01:31

Every web server on the public internet automatically sees your external IP address. There is just no standardized way to "talk it back" as far as I know (e.g. through a header or something).

If you want to do this manually, just use one of the numerous "what's my IP?" services around like www.infobyip.com/detectmyip.php

If you want to do it in an automated fashion, the most stable way would be to set up a script on a remote server, and have that output the requester's IP. In PHP, in most cases, it would look like so:

<? echo $_SERVER["REMOTE_ADDR"]; ?>

(Here is a detailed discussion on how to retrieve the IP in various ways, but if the above worked for you once, it is likely to work forever.)

查看更多
登录 后发表回答