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.
我的电脑正坐在路由器/防火墙后面。如何以编程方式找出我的外部IP地址。我可以使用http://www.whatsmyip.org/进行临时查询,但TOS不允许自动检查。
Any ideas?
13 个解决方案
#1
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.
http://ipecho.net/plain似乎是一个可行的替代方案,因为whatismyip.com现在需要其自动链接的成员资格。他们非常友好地似乎是免费提供这项服务,所以请不要滥用它。
#2
Unfortunately there is no easy way to do it.
不幸的是,没有简单的方法可以做到这一点。
I would use a site like www.whatsmyip.org and parse the output.
我会使用像www.whatsmyip.org这样的网站并解析输出。
checkip.dyndns.com returns a very simple HTML file which looks like this:
checkip.dyndns.com返回一个非常简单的HTML文件,如下所示:
<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.
这应该很容易解析。此外,该网站存在了大约十年。希望它会存在一段时间。
#3
If you have access to a webserver with modphp, you can roll your own:
如果您可以访问带有modphp的网络服务器,您可以自己动手:
<?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.
在您网站的public_html文件夹中创建一个名为whatismyip.php的文件。它可以被称为任何东西,并且可以在你的webroot中的任何地方。
Add the line above, and then query your server:
添加上面的行,然后查询您的服务器:
curl http://example.com/whatismyip.php
for example.
#4
Unfortunately as of 2013, whatismyip.com charge for the service.
不幸的是,截至2013年,whatismyip.com收取了该服务的费用。
http://www.icanhazip.com is still going strong, 3 years later. Just outputs the IP as text, absolutely nothing else.
3年后,http://www.icanhazip.com仍然强劲。只需输出IP作为文本,绝对没有别的。
http://checkip.dyndns.org still works as well.
http://checkip.dyndns.org仍然有效。
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.
如果你想确保它不会失效,你也可以使用谷歌,但它仍然可以阻止你违反TOS。
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.
但即使他们阻止我,他们仍然会在错误消息中告诉我我的客户端IP地址。
#5
curl ifconfig.me
or
curl ifconfig.me/ip
Incase you don't have curl installed,
如果你没有安装curl,
wget ifconfig.me/ip 2>/dev/null && cat ip
Hope this helps.
希望这可以帮助。
#6
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.
如果您所在的路由器说UPnP,您可以随时使用UPnP库来处理您正在开发的任何语言,以查询路由器的外部IP。
#7
http://myexternalip.com provides this kind of information. To retrieve your IP you have plenty of options:
http://myexternalip.com提供此类信息。要检索您的IP,您有很多选择:
- http://myexternalip.com/ - browser + lot's of examples of how to use it
- http://myexternalip.com/raw - a pure text answer, only your ip, no other crap
- http://myexternalip.com/json - a resposnse ready for json-parsers, also supports jsonp
- HEAD http://myexternalip.com - send only a HEAD-request and get the answer
http://myexternalip.com/ - 浏览器+很多关于如何使用它的例子
http://myexternalip.com/raw-纯文本答案,只有你的ip,没有其他垃圾
http://myexternalip.com/json - 为json-parsers准备的resposnse,也支持jsonp
HEAD http://myexternalip.com - 只发送一个HEAD请求并得到答案
#8
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.
由于这个问题有一段时间被问到,现在有一个免费提供的Web服务,专门用于让您以编程方式确定IP地址,称为ipify。
$ curl 'https://api.ipify.org?format=json'
Results in
{"ip": "1.2.3.4" /* your public IP */}
#9
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.
另一种方法是,如果您可以访问云电子邮件(yahoo,google,hotmail),请发送电子邮件给自己。然后查看标题,您应该在那里看到您的IP地址。
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
编辑:这回答了如何找到IP地址,而不是通过PROGRAMMATIC方法
#10
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:
我的WRT54G路由器通过其本地路由器访问功能(http(s)管理界面)告诉我,我想可以用许多其他设备做类似的事情。在这种情况下,条目页面以包含该短语的四行给出IPv4地址的八位字节:
class=num maxLength=3 size=3 value='i' name='wan_ipaddr_N' id='wan_ipaddr_N'
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:
其中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);
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:
如果您的路由器设置为使用此接口的http,请删除--sslv3 curl选项,您可以使用点分十进制表示法来寻址路由器。要使用https与上面的curl选项,我也这样做:
-
Used a browser to fetch the router's self-signed certificate (saved as Linksys.crt).
使用浏览器获取路由器的自签名证书(保存为Linksys.crt)。
-
Added it to my CA bundle:
将其添加到我的CA包中:
openssl x509 -in Linksys.crt -text >> /usr/ssl/certs/ca-bundle.crt
- 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.
将“Linksys”添加到我的主机文件(我的Win8盒子上的C:\ Windows \ System32 \ Drivers \ etc \ HOSTS),作为路由器地址的别名。如果将点分十进制表示法赋予curl而不是此别名,则它会因证书主题名称不匹配而拒绝连接。
Alternatively, you could just use the --insecure option to bypass certificate verification, which probably makes more sense in the circumstances.
或者,您可以使用--insecure选项绕过证书验证,这可能在这种情况下更有意义。
#11
whatismyip.com or ipchicken.com are very easy to parse.
whatismyip.com或ipchicken.com很容易解析。
If you have a webhost or vps you can also determine it, without fear of it randomly going down leaving you stuck.
如果你有一个webhost或vps,你也可以确定它,而不用担心它会随机下降而让你陷入困境。
#12
ifcfg.me allows Lookup via
ifcfg.me允许通过查找
nslookup
telnet
ftp
and http
nslookup telnet ftp和http
even works with IPv6
甚至适用于IPv6
#13
Simple but not elegant for this use. I installed some security cameras and my cable provider - Wide Open West or WOWWAY wanted $10 a month to set up a static IP - EXTORTION for a DHCP reservation ... so, 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.
这种用途简单但不优雅。我安装了一些安全摄像头和我的有线电视提供商--Broad Open West或WOWWAY每月需要10美元来设置一个静态IP - EXTORTION用于DHCP预留...所以,我用以下代码创建了一个VBS文件,将结果删除dropbox和google drive ...由于某种原因,必须删除要同步的新文件。
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 :-)
现在我可以在旅途中获取我的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
#1
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.
http://ipecho.net/plain似乎是一个可行的替代方案,因为whatismyip.com现在需要其自动链接的成员资格。他们非常友好地似乎是免费提供这项服务,所以请不要滥用它。
#2
Unfortunately there is no easy way to do it.
不幸的是,没有简单的方法可以做到这一点。
I would use a site like www.whatsmyip.org and parse the output.
我会使用像www.whatsmyip.org这样的网站并解析输出。
checkip.dyndns.com returns a very simple HTML file which looks like this:
checkip.dyndns.com返回一个非常简单的HTML文件,如下所示:
<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.
这应该很容易解析。此外,该网站存在了大约十年。希望它会存在一段时间。
#3
If you have access to a webserver with modphp, you can roll your own:
如果您可以访问带有modphp的网络服务器,您可以自己动手:
<?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.
在您网站的public_html文件夹中创建一个名为whatismyip.php的文件。它可以被称为任何东西,并且可以在你的webroot中的任何地方。
Add the line above, and then query your server:
添加上面的行,然后查询您的服务器:
curl http://example.com/whatismyip.php
for example.
#4
Unfortunately as of 2013, whatismyip.com charge for the service.
不幸的是,截至2013年,whatismyip.com收取了该服务的费用。
http://www.icanhazip.com is still going strong, 3 years later. Just outputs the IP as text, absolutely nothing else.
3年后,http://www.icanhazip.com仍然强劲。只需输出IP作为文本,绝对没有别的。
http://checkip.dyndns.org still works as well.
http://checkip.dyndns.org仍然有效。
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.
如果你想确保它不会失效,你也可以使用谷歌,但它仍然可以阻止你违反TOS。
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.
但即使他们阻止我,他们仍然会在错误消息中告诉我我的客户端IP地址。
#5
curl ifconfig.me
or
curl ifconfig.me/ip
Incase you don't have curl installed,
如果你没有安装curl,
wget ifconfig.me/ip 2>/dev/null && cat ip
Hope this helps.
希望这可以帮助。
#6
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.
如果您所在的路由器说UPnP,您可以随时使用UPnP库来处理您正在开发的任何语言,以查询路由器的外部IP。
#7
http://myexternalip.com provides this kind of information. To retrieve your IP you have plenty of options:
http://myexternalip.com提供此类信息。要检索您的IP,您有很多选择:
- http://myexternalip.com/ - browser + lot's of examples of how to use it
- http://myexternalip.com/raw - a pure text answer, only your ip, no other crap
- http://myexternalip.com/json - a resposnse ready for json-parsers, also supports jsonp
- HEAD http://myexternalip.com - send only a HEAD-request and get the answer
http://myexternalip.com/ - 浏览器+很多关于如何使用它的例子
http://myexternalip.com/raw-纯文本答案,只有你的ip,没有其他垃圾
http://myexternalip.com/json - 为json-parsers准备的resposnse,也支持jsonp
HEAD http://myexternalip.com - 只发送一个HEAD请求并得到答案
#8
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.
由于这个问题有一段时间被问到,现在有一个免费提供的Web服务,专门用于让您以编程方式确定IP地址,称为ipify。
$ curl 'https://api.ipify.org?format=json'
Results in
{"ip": "1.2.3.4" /* your public IP */}
#9
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.
另一种方法是,如果您可以访问云电子邮件(yahoo,google,hotmail),请发送电子邮件给自己。然后查看标题,您应该在那里看到您的IP地址。
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
编辑:这回答了如何找到IP地址,而不是通过PROGRAMMATIC方法
#10
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:
我的WRT54G路由器通过其本地路由器访问功能(http(s)管理界面)告诉我,我想可以用许多其他设备做类似的事情。在这种情况下,条目页面以包含该短语的四行给出IPv4地址的八位字节:
class=num maxLength=3 size=3 value='i' name='wan_ipaddr_N' id='wan_ipaddr_N'
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:
其中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);
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:
如果您的路由器设置为使用此接口的http,请删除--sslv3 curl选项,您可以使用点分十进制表示法来寻址路由器。要使用https与上面的curl选项,我也这样做:
-
Used a browser to fetch the router's self-signed certificate (saved as Linksys.crt).
使用浏览器获取路由器的自签名证书(保存为Linksys.crt)。
-
Added it to my CA bundle:
将其添加到我的CA包中:
openssl x509 -in Linksys.crt -text >> /usr/ssl/certs/ca-bundle.crt
- 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.
将“Linksys”添加到我的主机文件(我的Win8盒子上的C:\ Windows \ System32 \ Drivers \ etc \ HOSTS),作为路由器地址的别名。如果将点分十进制表示法赋予curl而不是此别名,则它会因证书主题名称不匹配而拒绝连接。
Alternatively, you could just use the --insecure option to bypass certificate verification, which probably makes more sense in the circumstances.
或者,您可以使用--insecure选项绕过证书验证,这可能在这种情况下更有意义。
#11
whatismyip.com or ipchicken.com are very easy to parse.
whatismyip.com或ipchicken.com很容易解析。
If you have a webhost or vps you can also determine it, without fear of it randomly going down leaving you stuck.
如果你有一个webhost或vps,你也可以确定它,而不用担心它会随机下降而让你陷入困境。
#12
ifcfg.me allows Lookup via
ifcfg.me允许通过查找
nslookup
telnet
ftp
and http
nslookup telnet ftp和http
even works with IPv6
甚至适用于IPv6
#13
Simple but not elegant for this use. I installed some security cameras and my cable provider - Wide Open West or WOWWAY wanted $10 a month to set up a static IP - EXTORTION for a DHCP reservation ... so, 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.
这种用途简单但不优雅。我安装了一些安全摄像头和我的有线电视提供商--Broad Open West或WOWWAY每月需要10美元来设置一个静态IP - EXTORTION用于DHCP预留...所以,我用以下代码创建了一个VBS文件,将结果删除dropbox和google drive ...由于某种原因,必须删除要同步的新文件。
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 :-)
现在我可以在旅途中获取我的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