为 NAT 后面的主机检索外部 IP 的稳定方法 [英] Stable way of retrieving the external IP for a host behind a NAT

查看:49
本文介绍了为 NAT 后面的主机检索外部 IP 的稳定方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想显示一个主机对外公开的 IP 地址,无论它是否是 natted lan 的一部分.我现在所做的只是连接到 myipaddress.com 并从那里检索它.我只是不知道我是否相信该网站是一个稳定的来源.是否有一些权威可以促进这一点?

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?

推荐答案

公共 Internet 上的每个 Web 服务器都会自动查看您的外部 IP 地址.据我所知,没有标准化的回话"方式(例如,通过标题或其他方式).

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

如果您想手动执行此操作,只需使用众多我的 IP 是什么?"之一.周边服务如 www.infobyip.com/detectmyip.php

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

如果您想以自动化方式执行此操作,最稳定的方法是在远程服务器上设置脚本,然后输出请求者的 IP.在 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"]; ?>

(这里 详细讨论了如何以各种方式检索 IP,但如果上述方法对您有效,则可能永远有效.)

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

这篇关于为 NAT 后面的主机检索外部 IP 的稳定方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆