如何知道我的外部 IP 地址是什么?

发布于 2024-07-27 18:42:48 字数 185 浏览 6 评论 0原文

我的计算机位于路由器/防火墙后面。 如何以编程方式找出我的外部 IP 地址是什么。 我可以使用 http://www.whatsmyip.org/ 进行临时查询,但 TOS 不这样做不允许自动检查。

有任何想法吗?

My computers are sitting behind a router/firewall. How do I programmatically find out what my external IP address is. I can use http://www.whatsmyip.org/ for ad-hoc queries, but the TOS don't allow for automated checks.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

痴情换悲伤 2024-08-03 18:42:49

如果您后面的路由器使用 UPnP,您始终可以使用您正在开发的任何语言的 UPnP 库来查询路由器的外部 IP。

If the router you are behind speak UPnP you could always use a UPnP library for whatever language you are developing in to query the router for its external ip.

最好是你 2024-08-03 18:42:49

http://myexternalip.com 提供此类信息。 到
检索您的 IP,您有很多选择:

http://myexternalip.com provides this kind of information. To
retrieve your IP you have plenty of options:

一个人的旅程 2024-08-03 18:42:49

由于这个问题不久前就被问到了,现在有一个免费的 Web 服务,专门设计用于允许您以编程方式确定您的 IP 地址,称为 ipify

$ curl 'https://api.ipify.org?format=json'

结果是

{"ip": "1.2.3.4" /* your public IP */}

Since this question was asked a while back, there's now a freely available web service designed specifically to allow you to determine your IP address programmatically, called ipify.

$ curl 'https://api.ipify.org?format=json'

Results in

{"ip": "1.2.3.4" /* your public IP */}
遥远的她 2024-08-03 18:42:49

另一种方法是,如果您可以访问云电子邮件(yahoo、google、hotmail),请给自己发送一封电子邮件。 然后查看标头,您应该会在其中看到您的 IP 地址。

我会查找确切的区域,但标题可能因每个实现而异,查找接收者并遵循它,直到找到看起来像发送者的内容

编辑:这回答了如何查找IP地址,而不是通过程序化方法

Another way is if you have access to a cloud email (yahoo, google, hotmail), send yourself an email. Then view the headers and you should see your IP address in there.

I would look up the exact area but the headers may vary from each implmentation, Look for the received-by and follow that until you get to something that looks like sent-by

EDIT: This answers the how to find IP address, not the via PROGRAMMATIC approach

栀子花开つ 2024-08-03 18:42:49

我的 WRT54G 路由器通过其本地路由器访问功能(http(s) 管理界面)告诉我,我想许多其他设备也可以完成类似的操作。 在这种情况下,条目页面在包含以下短语的四行中给出 IPv4 地址的八位字节:

class=num maxLength=3 size=3 value='i' name='wan_ipaddr_N ' id='wan_ipaddr_N'

其中 i 是八位位组值,N 是八位位组编号。 这段打油诗为我获取并解析它,由 cygwin 提供:

#! /usr/bin/env perl
use strict;
use warnings 'all';

my( $account, $password ) = @ARGV;

open QUERY,
    "curl --sslv3 --user '$account:$password' https://Linksys/ --silent |"
    or die "Failed to connect to router";

my @ipaddr = ('x','x','x','x');

while( <QUERY> ) {
    $ipaddr[$2] = $1 if /value='(\d+)' name='wan_ipaddr_([0-3])/;
}
close QUERY;
print join('.', @ipaddr);

不能保证这适用于所有版本的路由器固件。

如果您的路由器设置为此接口使用 http,请删除 --sslv3 curl 选项,然后您可以使用点分十进制表示法来寻址路由器。 要将 https 与上面的curl选项一起使用,我还执行了以下操作:

  1. 使用浏览器获取路由器的自签名证书(另存为 Linksys.crt)。

  2. 将其添加到我的 CA 捆绑包中:

    openssl x509 -in Linksys.crt -text >>>   /usr/ssl/certs/ca-bundle.crt 
      
  3. 将“Linksys”添加到我的主机文件(我的 Win8 机器上的 C:\Windows\System32\Drivers\etc\HOSTS)中,作为路由器地址的别名。 如果为curl 指定点分十进制表示法而不是此别名,则它会因证书使用者名称不匹配而拒绝连接。

或者,您可以仅使用 --insecure 选项来绕过证书验证,这在这种情况下可能更有意义。

My WRT54G router tells me through its Local Router Access feature (the http(s) administration interface), and I imagine something similar could be done with many other devices. In this case, the entry page gives the octets of the IPv4 address in four lines containing this phrase:

class=num maxLength=3 size=3 value='i' name='wan_ipaddr_N' id='wan_ipaddr_N'

Where i is the octet value and N is the octet number. This bit of doggerel fetches and parses it for me, courtesy of cygwin:

#! /usr/bin/env perl
use strict;
use warnings 'all';

my( $account, $password ) = @ARGV;

open QUERY,
    "curl --sslv3 --user '$account:$password' https://Linksys/ --silent |"
    or die "Failed to connect to router";

my @ipaddr = ('x','x','x','x');

while( <QUERY> ) {
    $ipaddr[$2] = $1 if /value='(\d+)' name='wan_ipaddr_([0-3])/;
}
close QUERY;
print join('.', @ipaddr);

There is no guarantee that this will work with all versions of the router firmware.

If your router is set to use http for this interface, drop the --sslv3 curl option, and you can use dotted-decimal notation to address the router. To use https with the curl options above, I also did this:

  1. Used a browser to fetch the router's self-signed certificate (saved as Linksys.crt).

  2. Added it to my CA bundle:

    openssl x509 -in Linksys.crt -text >> /usr/ssl/certs/ca-bundle.crt
    
  3. Added 'Linksys' to my hosts file (C:\Windows\System32\Drivers\etc\HOSTS on my Win8 box), as an alias for the router's address. If the dotted-decimal notation is given to curl instead of this alias, it rejects the connection on account of a certificate subject name mismatch.

Alternatively, you could just use the --insecure option to bypass certificate verification, which probably makes more sense in the circumstances.

小猫一只 2024-08-03 18:42:49

Whatismyip.com 或 ipchicken.com 非常容易解析。

如果您有虚拟主机或虚拟专用服务器,您也可以确定它,而不必担心它会随机关闭而让您陷入困境。

whatismyip.com or ipchicken.com are very easy to parse.

If you have a webhost or vps you can also determine it, without fear of it randomly going down leaving you stuck.

抹茶夏天i‖ 2024-08-03 18:42:49

ifcfg.me
允许通过

nslookup 查找
远程登录
FTP
http

甚至可以与 IPv6 一起使用

ifcfg.me
allows Lookup via

nslookup
telnet
ftp
and http

even works with IPv6

乖乖兔^ω^ 2024-08-03 18:42:49

对于这种用途来说简单但不优雅。 我使用以下代码创建了一个 VBS 文件,以将结果放入保管箱和谷歌驱动器...出于某种原因,必须删除该文件才能同步新文件。

它在我家的电脑上运行。 我的电脑设置为在断电时恢复,并且计划每天运行一次任务(请注意,如果您经常运行它,该站点将阻止您的请求)。

现在我可以在路上获取我的 IP 地址并看着人们偷我的东西:-)

get_html "http://ipecho.net/plain", "C:\Users\joe\Google Drive\IP.html"

get_html "http://ipecho.net/plain", "C:\Users\joe\Dropbox\IP.html"



sub get_html (up_http, down_http)

dim xmlhttp : set xmlhttp = createobject("msxml2.xmlhttp.3.0")

xmlhttp.open "get", up_http, false

xmlhttp.send

dim fso : set fso = createobject ("scripting.filesystemobject")

dim newfile : set newfile = fso.createtextfile(down_http, true)

newfile.write (xmlhttp.responseText)

newfile.close

set newfile = nothing
set xmlhttp = nothing

end sub

Simple but not elegant for this use. I created a VBS file with the following code to drop the result to dropbox and google drive ... have to delete the file for new one to sync though for some reason.

This runs on a PC at my home. My PC is set to resume on power outage and a task is scheduled to run this every day once (note if you have it run often, the site will block your requests).

Now I can get my IP address on the road and watch people steal my stuff :-)

get_html "http://ipecho.net/plain", "C:\Users\joe\Google Drive\IP.html"

get_html "http://ipecho.net/plain", "C:\Users\joe\Dropbox\IP.html"



sub get_html (up_http, down_http)

dim xmlhttp : set xmlhttp = createobject("msxml2.xmlhttp.3.0")

xmlhttp.open "get", up_http, false

xmlhttp.send

dim fso : set fso = createobject ("scripting.filesystemobject")

dim newfile : set newfile = fso.createtextfile(down_http, true)

newfile.write (xmlhttp.responseText)

newfile.close

set newfile = nothing
set xmlhttp = nothing

end sub
惟欲睡 2024-08-03 18:42:48

http://ipecho.net/plain 似乎是
可行的替代方案,因为whatismyip.com 现在需要会员资格
他们的自动链接。 他们看起来非常友善
免费提供此服务,
所以请不要滥用它。

http://ipecho.net/plain appears to be a
workable alternative, as whatismyip.com now requires membership for
their automated link. They very kindly appear to be
offering this service for free,
so please don't abuse it.

忆依然 2024-08-03 18:42:48

不幸的是,没有简单的方法可以做到这一点。

我会使用像 www.whatsmyip.org 这样的网站并解析输出。

checkip.dyndns.com 返回一个非常简单的 HTML 文件,如下所示:

<代码> 
    <头> 
      当前 IP 检查 
     
    <正文> 
      当前IP地址:84.151.156.163 
     
   
  

这应该很容易解析。
而且该网站已经存在了大约十年。
人们希望它会存在一段时间。

Unfortunately there is no easy way to do it.

I would use a site like www.whatsmyip.org and parse the output.

checkip.dyndns.com returns a very simple HTML file which looks like this:

<html>
  <head>
    <title>Current IP Check</title>
  </head>
  <body>
    Current IP Address: 84.151.156.163
  </body>
</html>

This should be very easy to parse.
Moreover the site is exists for about ten years.
There is hope that it will be around for a while.

谎言月老 2024-08-03 18:42:48

如果您可以使用 modphp 访问网络服务器,您可以推出自己的服务器:

<?php print $_SERVER['REMOTE_ADDR']; ?>

如果您不希望其被滥用,则必须对其保密或添加请求限制。

多年来我一直在我的服务器上使用它。

明确:

在网站的 public_html 文件夹中创建一个名为 Whatismyip.php 的文件。 它可以被称为任何名称,并且可以位于您的网络根目录中的任何位置。

添加上面的行,然后查询您的服务器:

curl http://example.com/whatismyip.php

例如。

If you have access to a webserver with modphp, you can roll your own:

<?php print $_SERVER['REMOTE_ADDR']; ?>

If you don't want that to get abused, you'll have to keep it secret or add request limits.

I've been using one on my server for years.

Explicitly:

Create a file called whatismyip.php in your public_html folder in your website. It can be called anything and be anywhere in your webroot.

Add the line above, and then query your server:

curl http://example.com/whatismyip.php

for example.

不疑不惑不回忆 2024-08-03 18:42:48

不幸的是,从 2013 年起,whatismyip.com 对该服务收费。

3 年后,http://www.icanhazip.com 仍然强劲。 仅以文本形式输出 IP,绝对没有其他内容。

http://checkip.dyndns.org 仍然有效。

如果您想确保它不会下降,您也可以使用 Google,但它仍然可以阻止您违反 TOS。

https://www.google.ie/search?q=whats+is+my+ip< /a>

但即使他们阻止了我,他们仍然在错误消息中告诉我我的客户端 IP 地址。

Unfortunately as of 2013, whatismyip.com charge for the service.

http://www.icanhazip.com is still going strong, 3 years later. Just outputs the IP as text, absolutely nothing else.

http://checkip.dyndns.org still works as well.

You can also use Google if you want to be sure it won't go down, but it can still block you for TOS violations.

https://www.google.ie/search?q=whats+is+my+ip

But even when they block me, they still tell me my client IP address in the error message.

冷情妓 2024-08-03 18:42:48
curl ifconfig.me

或者

curl ifconfig.me/ip

如果您没有安装curl,

wget ifconfig.me/ip 2>/dev/null && cat ip

希望这有帮助。

curl ifconfig.me

or

curl ifconfig.me/ip

Incase you don't have curl installed,

wget ifconfig.me/ip 2>/dev/null && cat ip

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文