Linux 在bash中检测公共IP地址的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14594151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:50:54  来源:igfitidea点击:

Methods to detect public IP address in bash

linuxbashshellubuntucentos

提问by Jeroen

As part of an installation script, I want to make an "educated guess" about the machines publicIP address. It should be as robust as possible, working on a variety of hosts and platforms. Here is an example:

作为安装脚本的一部分,我想对机器的公共IP 地址进行“有根据的猜测” 。它应该尽可能健壮,可以在各种主机和平台上工作。下面是一个例子:

https://gist.github.com/4669037

https://gist.github.com/4669037

This script uses 3 methods and then defaults to the value of /etc/hostname. I have only tested this on Ubuntu so far. Will this be likely to work on other distributions? And are there additional methods that I could add?

此脚本使用 3 种方法,然后默认为/etc/hostname. 到目前为止,我只在 Ubuntu 上测试过这个。这可能适用于其他发行版吗?还有我可以添加的其他方法吗?

采纳答案by Steven Penny

curl ipinfo.io/ip

Or

或者

wget -qO- ipinfo.io/ip

Or

或者

lynx -source ipinfo.io/ip

get public ip address

获取公共IP地址

回答by peterph

How much public public IP are you looking for? What if the machine is behind NAT?

您要寻找多少公共公共 IP?如果机器在 NAT 后面怎么办?

  • curl/ wget/ netcat(nc) <URL>which contains requester's address: should work most of the time, but may the site may be unreachable from the machine (be it firewall or temporary/permanent unavailability). You'll get the most public IP you can.

  • ifconfig: must run as root, otherwise you'd have to try /sbin/ifconfigor /usr/sbin/ifconfigas well. What if the machine has more NICs? How do you tell what IP is the right one? What if only IPv6 is used on the machine's LAN? In any case, you'll get the least public IP you can (and possibly a wrong one if more interfaces are configured on the machine - which often is the case these days with omnipresent virtualization using network tap devices and/or).

  • /etc/hostname: does not need to exist, on many systems it is /etc/HOSTNAME, and it does not contain IP address rather it should contain the hostname (usually the FQDN).

  • curl/ wget/ netcatnc),<URL>其中包含请求者地址:应该工作的大部分时间,但可能该网站可能会从机器无法访问(无论是防火墙或暂时/永久不可用)。您将获得尽可能多的公共 IP。

  • ifconfig: 必须以 root 身份运行,否则你必须尝试/sbin/ifconfig或者/usr/sbin/ifconfig。如果机器有更多的网卡怎么办?你如何判断哪个IP是正确的?如果机器的 LAN 上只使用 IPv6 会怎样?在任何情况下,您都将获得尽可能少的公共 IP(如果在机器上配置了更多接口,则可能是错误的 IP——如今使用网络分流设备和/或无所不在的虚拟化通常就是这种情况)。

  • /etc/hostname: 不需要存在,在许多系统上它是/etc/HOSTNAME,并且它不包含 IP 地址,而应该包含主机名(通常是 FQDN)。

The point is, that the ways in which it can fail are numerous and you probably should consider either a) specifying more precisely what systems you are targeting or b) whether you really need to know the IP at all - is a solution that seems to work in simple cases worth using when it fails miserably in slightly more complicated setup? If you think you need the IP, prepare a way to handle failures gracefully in cases where you either don't get the IP at all or you get a wrong one.

关键是,它可能失败的方式有很多,您可能应该考虑 a) 更准确地指定您的目标系统或 b) 您是否真的需要知道 IP - 似乎是一个解决方案在简单的情况下工作,当它在稍微复杂的设置中惨遭失败时值得使用吗?如果您认为您需要 IP,请准备一种方法来优雅地处理故障,以防您根本没有获得 IP 或获得错误的 IP。

回答by Kent

curl ifconfig.mewould be the best choice, in case you don't have curl:

curl ifconfig.me将是最好的选择,如果你没有卷曲:

wget -qO- ifconfig.me/ip

wget -qO- ifconfig.me/ip

回答by Chris Montanaro

Use curl to hit shtuff.it IP service

使用 curl 打 shtuff.it IP 服务

curl http://shtuff.it/myip/short

回答by Furkan Mustafa

This solution doesn't work if you're behind nat or something. It helps without a 3rd party service. It works if the machine has the connection on it, like a server, or a ppp connection.

如果您落后于 nat 或其他东西,则此解决方案不起作用。它在没有 3rd 方服务的情况下有所帮助。如果机器上有连接,如服务器或 ppp 连接,它就可以工作。

  • Not an exactly correct answer to question, but probably helps other people (like me)
  • Requires to be root in most (or all?)cases
  • 不是一个完全正确的问题答案,但可能会帮助其他人(比如我)
  • 在大多数(或所有?)情况下需要 root

You can get the (first) default route from route -n, find the interface it uses, and find the ip it has.

您可以从 获取(第一个)默认路由route -n,找到它使用的接口,并找到它拥有的 ip。

MAINIF=$( route -n | grep '^0\.0\.0\.0' | head -n 1 | awk '{print $NF}' )
IP=$( ifconfig $MAINIF | { IFS=' :';read r;read r r a r;echo $a; } )

回答by MCurbelo

Try this command:

试试这个命令:

wget -O - -q icanhazip.com

回答by RiccardoCh

Use ipifyAPI:

使用ipifyAPI:

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

Bash script:

bash脚本:

#!/bin/bash

ip=$(curl -s https://api.ipify.org)
echo "My public IP address is: $ip"

回答by Jasen

Using only bash (with help from icanhazip.com):

仅使用 bash(在icanhazip.com 的帮助):

exec 3<>/dev/tcp/icanhazip.com/80 
echo -e 'GET / HTTP/1.0\r\nhost: icanhazip.com\r\n\r' >&3 
while read i
do
 [ "$i" ] && myip="$i" 
done <&3 
echo "$myip"

bash opens open a socket to icanhazip and sends an http request, the IP address is returned on the last non-empty line of the data returned. (previous lines are http headers)

bash 打开一个 socket 到 icanhazip 并发送一个 http 请求,IP 地址在返回数据的最后一个非空行上返回。(前几行是 http 标头)

This avoids the need for http client like wget or curl.

这避免了像 wget 或 curl 这样的 http 客户端的需要。

回答by Eric Semwenda

If on an AWS EC2 you can use:

如果在 AWS EC2 上,您可以使用:

curl checkip.amazonaws.com

NOTE: this service can be used from any web client.

注意:此服务可从任何 Web 客户端使用。