哪个终端命令只获取 IP 地址而不获取其他内容?
我试图在我编写的脚本中仅使用 IP 地址 (inet) 作为参数。
在unix终端中是否有一种简单的方法可以只获取IP地址,而不是通过ifconfig
查找?
I'm trying to use just the IP address (inet) as a parameter in a script I wrote.
Is there an easy way in a unix terminal to get just the IP address, rather than looking through ifconfig
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
您可以编写一个仅返回 IP 的脚本,例如:
对于 MAC:
或者对于 linux 系统
You can write a script that only return the IP like:
For MAC:
Or for linux system
这将为您提供所有 IPv4 接口,包括环回 127.0.0.1:
这将仅显示
eth0
:这样您可以获得 IPv6 地址:
仅
eth0
IPv6:This will give you all IPv4 interfaces, including the loopback 127.0.0.1:
This will only show
eth0
:And this way you can get IPv6 addresses:
Only
eth0
IPv6:一般来说,永远不能保证一个系统只有一个 IP 地址,例如,您可以同时拥有以太网和 WLAN 连接,如果您有活动的 VPN 连接,那么您将拥有更多的 IP 地址。
Linux
在 Linux 上,
hostname -I
将列出当前的 IP 地址。在某些情况下,依赖它总是只返回一个 IP 地址很可能无法按预期工作,因此一种更可靠的方法是将结果转换为数组,然后循环遍历元素:OSX
在 OSX 上,如果您知道接口,您可以使用:
它将返回仅 IP 地址。
要动态检测 MacOS 上的(第一个)活动网络接口:
获取外部 IP
要获取外部 IP,您可以查询文本模式服务,例如
curl https://ipecho.net/plain
将返回纯文本外部 IP。据称更快的方法可能是查询已知的 DNS 服务器,例如:
Generally, it is never guaranteed that a system will only have one IP address, for example, you can have both an ethernet and wlan connections, and if you have active VPN connections then you'll have yet more IP addresses.
Linux
On Linux,
hostname -I
will list the current IP address(es). Relying on it always returning just one IP address will most likely not work as expected under some scenarios, so a more reliable way would be converting the result to an array and then loop over the elements:OSX
On OSX, if you know the interface, you could use:
which will return just the IP address.
To detect dynamically the (first) active network interface on MacOS:
Getting the external IP
To get the external IP you could query a text-mode service, for example
curl https://ipecho.net/plain
would return a plain text external IP.A supposedly faster approach could be to query a known DNS server, e.g.:
该命令将为您提供 Ubuntu 中所需的确切 IP 地址。请注意,该标志是大写。
This command will give you the exact IP address as you want in Ubuntu. Note the flag is uppercase.
在最新的 Ubuntu 版本(14.04 - 16.04)上,这个命令对我来说很有效。
On latest Ubuntu versions (14.04 - 16.04), this command did the trick for me.
要仅获取 Mac OS X 上的 IP 地址,您可以键入以下命令:
To get only the IP address on Mac OS X you can type the following command:
我不喜欢在脚本中使用
awk
等。ip
可以选择以 JSON 格式输出。如果省略
$interface
那么您将获得所有 IP 地址:I prefer not to use
awk
and such in scripts..ip
has the option to output in JSON.If you leave out
$interface
then you get all of the ip addresses:如果你的环境有限,可以使用这个命令:
If you have limited environment, you may use this command:
ip -4 addr show eth0
在某些机器上不起作用。例如,我收到此错误:ip:符号查找错误:ip:未定义符号:bpf_program__section_name,版本LIBBPF_0.2.0
这对我有用:
这比接受的答案少了一个管道。此外,我的 ifconfig 输出没有
inet addr
。要获取 IPv6 地址,请使用以下命令:
ip -4 addr show eth0
doesn't work on some machines. For example, I get this error:ip: symbol lookup error: ip: undefined symbol: bpf_program__section_name, version LIBBPF_0.2.0
This works for me:
This has one less pipe than the accepted answer. In addition, my ifconfig output does not have
inet addr
.To get the IPv6 address, use this:
命令
ifconfig
已弃用,您应该在 Linux 上使用ip
命令。另外,
ip a
将为您提供与 IP 位于同一行的范围,因此更易于使用。此命令将显示您的全局(外部)IP:
所有 IPv4(也为 127.0.0.1):
所有 IPv6(也为 ::1):
Command
ifconfig
is deprected and you should useip
command on Linux.Also
ip a
will give you scope on the same line as IP so it's easier to use.This command will show you your global (external) IP:
All IPv4 (also 127.0.0.1):
All IPv6 (also ::1):
我想要一些简单的东西作为 Bash 别名。我发现
hostname -I
最适合我(hostname v3.15)。由于某种原因,hostname -i
返回环回 IP,但是hostname -I
为我提供了 wlan0 的正确 IP,并且无需通过 grep 或 awk 进行管道输出。一个缺点是,如果您有多个 IP,hostname -I
将输出所有 IP。I wanted something simple that worked as a Bash alias. I found that
hostname -I
works best for me (hostname v3.15).hostname -i
returns the loopback IP, for some reason, buthostname -I
gives me the correct IP for wlan0, and without having to pipe output through grep or awk. A drawback is thathostname -I
will output all IPs, if you have more than one.似乎很少有答案使用较新的
ip
命令(替换ifconfig
),因此这里是使用ip addr
、grep< /code> 和
awk
来简单地打印与wlan0
接口关联的 IPv4 地址:虽然不是最紧凑或最奇特的解决方案,但它(可以说)很容易理解(请参阅下面的解释)并为其他目的进行修改,例如获取 MAC 地址的最后 3 个八位字节,如下所示:
说明:
ip addr show wlan0
输出与名为wlan0
的网络接口关联的信息,应该与此类似:接下来
grep inet
过滤掉不包含“inet”的行(IPv4 和 IPv6 配置),并且grep -v inet6
过滤掉剩余的行do 包含“inet6”,这应该会产生如下所示的单行:最后,第一个
awk
提取“172.18.18.1/24”字段,第二个删除网络掩码简写,仅保留 IPv4 地址。另外,我认为值得一提的是,如果您正在编写脚本,那么通常有许多更丰富和/或更强大的工具来获取此信息,您可能想使用这些工具。例如,如果使用 Node.js,则有
ipaddr-linux
,如果使用 Ruby,则有linux-ip-parser
等。另请参阅https://unix.stackexchange.com/questions/119269/how-使用 shell 脚本获取 ip 地址
Few answers appear to be using the newer
ip
command (replacement forifconfig
) so here is one that usesip addr
,grep
, andawk
to simply print the IPv4 address associated with thewlan0
interface:While not the most compact or fancy solution, it is (arguably) easy to understand (see explanation below) and modify for other purposes, such as getting the last 3 octets of the MAC address like this:
Explanation:
ip addr show wlan0
outputs information associated with the network interface namedwlan0
, which should be similar to this:Next
grep inet
filters out the lines that don't contain "inet" (IPv4 and IPv6 configuration) andgrep -v inet6
filters out the remaining lines that do contain "inet6", which should result in a single line like this one:Finally, the first
awk
extract the "172.18.18.1/24" field and the second removes the network mask shorthand, leaving just the IPv4 address.Also, I think it's worth mentioning that if you are scripting then there are often many richer and/or more robust tools for obtaining this information, which you might want to use instead. For example, if using Node.js there is
ipaddr-linux
, if using Ruby there islinux-ip-parser
, etc.See also https://unix.stackexchange.com/questions/119269/how-to-get-ip-address-using-shell-script
要仅打印
eth0
的 IP 地址,而不显示其他文本:要确定您的主接口(因为它可能不是“eth0”),请使用:
上述两行可以组合成一个命令,例如这:
To print only the IP address of
eth0
, without other text:To determine your primary interface (because it might not be "eth0"), use:
The above two lines can be combined into a single command like this:
ip adddr,简短方式
ip -4 -br addr show enp1s0 | awk -F" " '{print $3}'|cut -d'/' -f1
或将
ip -4 -br 缩短为 enp1s0 | awk -F" " '{print $3}'|cut -d'/' -f1
必须在大多数现代 Linux 发行版中工作
ip adddr, the short way
ip -4 -br addr show enp1s0 | awk -F" " '{print $3}'|cut -d'/' -f1
or shorten
ip -4 -br a s enp1s0 | awk -F" " '{print $3}'|cut -d'/' -f1
must work in most modern Linux distribution
在 Ubuntu(和 Debian 等发行版)上,
要仅获取本地地址数字,请执行以下命令:
有关网络接口配置和全局 IP 地址使用的全面信息:
要深入了解任何 IP 地址(包括您自己的 IP 地址)的奥秘:
确保您安装了
whois
工具。如果没有,只需执行:On Ubuntu( and Debian like distro)
To obtain just the local address digits, execute the following command:
For a comprehensive picture of your network interface configuration and global IP address use:
To dive deeper into the mysteries of any IP address (including your own):
Ensure you have the
whois
tool installed. If not, simply execute:这在 Mac 上就可以解决问题:
在我的机器上解析到
ping 192.168.1.2
。专业提示:
$(...)
表示在子 shell 中运行括号内的任何内容并将其作为值返回。That would do the trick in a Mac :
That resolved to
ping 192.168.1.2
in my machine.Pro tip:
$(...)
means run whatever is inside the parentheses in a subshell and return that as the value.我们只需使用 2 个命令 ( ifconfig + awk ) 即可获取我们想要的 IP (v4),如下所示:
在 Linux 上,假设从
eth0
接口获取 IP 地址,请运行以下命令: OSX,假设从
en0
接口获取 IP 地址,运行以下命令:要知道我们的公共/外部 IP,请在
~/.bashrc
中添加此函数,然后运行,
whatismyip
We can simply use only 2 commands ( ifconfig + awk ) to get just the IP (v4) we want like so:
On Linux, assuming to get IP address from
eth0
interface, run the following command:On OSX, assumming to get IP adddress from
en0
interface, run the following command:To know our public/external IP, add this function in
~/.bashrc
Then run,
whatismyip
我总是在最意想不到的时候需要这个,而且毫无疑问,最终会在 SO 上搜索这样的线程。所以我写了一个简单的脚本来通过netstat获取IPv4地址,名为
echoip
- 你可以找到它在这里。网络地址的 bash 看起来像这样,它还从 ipecho.net 获取您的公共地址:echoip
脚本产生如下输出:I always wind up needing this at the most unexpected times and, without fail, wind up searching for threads like this on SO. So I wrote a simple script to get IPv4 addresses via netstat, called
echoip
- you can find it here. The bash for network addresses looks like this, it also gets your public address from ipecho.net:The
echoip
script yields an output like this:在
man hostname
中,有更简单的方法,可以自动排除环回 IP,并仅显示所有分配给主机 IP 地址的空格分隔列表:In
man hostname
there is even more easier way which automatically excluding loopback IP and showing only space separated list of all assigned to host ip addresses:默认路由的 IPv4 地址:
默认路由的 IPv6 地址:
这些仅需要命令
ip
和grep
,并支持-P
和<代码>-o。head -1
是必需的,因为当系统具有足够复杂的网络设置时,ip Route
可能会显示多个默认路由。如果你不介意哪个IP是哪个,你可以这样做
或
The IPv4 address for the default route:
The IPv6 address for the default route:
These only require commands
ip
andgrep
with support for-P
and-o
. Thehead -1
is required becauseip route
may show multiple default routes when system has complex enough network setup.If you don't mind which IP is which, you can just do
or
最简单的方法是 Mikko 所说的
输出
您也可以这样做以获得更多细节:
输出
the easiest way is as Mikko said
the output
you can also do that for little more details :
the output
使用以下命令:
Use the following command:
这是我的版本,您可以在其中传递按优先级排序的接口列表:
因此,如果我连接了 VPN、Wifi 和以太网,我的 VPN 地址(在接口 tap0 上)将被返回。该脚本适用于 linux 和 osx,如果您想覆盖 IFLIST,则可以接受参数。
请注意,如果您想使用 IPV6,则必须将 'inet' 替换为 'inet6'。
Here is my version, in which you can pass a list of interfaces, ordered by priority:
So if I'm connected with VPN, Wifi and ethernet, my VPN address (on interface tap0) will be returned. The script works on both linux and osx, and can take arguments if you want to override IFLIST
Note that if you want to use IPV6, you'll have to replace 'inet ' by 'inet6'.
使用这一行脚本:
<代码>
ifconfig | grep “inet”| grep -v 127.0.0.1|awk '匹配($0, /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {打印substr($0,RSTART,RLENGTH)}'
麦克& linux(在ubuntu中测试)都可以。
use this one line script:
ifconfig | grep "inet " | grep -v 127.0.0.1|awk 'match($0, /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {print substr($0,RSTART,RLENGTH)}'
mac & linux (tested in ubuntu) both works.
您还可以使用以下命令:
注意:只有当您连接到互联网时,此命令才有效。
You can also use the following command:
NOTE: This will only work if you have connectivity to the internet.
还有另一种方法可以做到这一点。
我经常需要检查各种接口的 IP(例如 eth0、隧道等),
所以我是这样做的:
输出:
如果您不关心网络适配器,只需从 awk 命令中删除 $1 即可。
为了简单起见,我只需将其添加为别名(记住转义 $!)
yet another way to do it.
I often need to check the IP of various interfaces (e.g eth0, tunnel, etc.)
So this is how i do it:
output:
If you do not care about the network adapter, just remove the $1 from awk command.
to make it easy, i just add it as an alias (remember to escape the $'s!)
我没有看到 nmcli 的任何答案,它是一个用于控制 NetworkManager 的命令行工具。
所以就在这里 :)
如果您想从特定网络接口获取信息(在本例中为
lo
),但由于您只想获取 IP 地址,只需将输出发送到
grep
、cut
或awk
。让我们一步一步来做。 (不确定出了什么问题,代码示例格式不适用于这 3 个示例。)
获取 IPv4 线路
wolf@linux:~$ nmcli 设备显示 lo | grep 4.A
IP4.地址[1]: 127.0.0.1/8
狼@linux:~$
使用
awk
获取IPwolf@linux:~$ nmcli 设备显示 lo | awk '/4.A/ {打印 $2}'
127.0.0.1/8
狼@linux:~$
使用
cut
删除 CIDR 符号 (/8)wolf@linux:~$ nmcli 设备显示 lo | awk '/4.A/ {print $2}' |剪切-d / -f1
127.0.0.1
狼@linux:~$
这就是你的答案。
请注意,使用我刚才演示的工具有很多方法可以做到这一点。
让我们回顾一下我使用的命令。
这 3 个命令的示例输出
命令 1 输出
命令 2 输出
命令 3 输出
I don't see any answer with
nmcli
yet which is a command-line tool for controlling NetworkManager.So here you go :)
If you want to get the information from specific network interface (let say
lo
for this example)But since you just want to get the IP address, just send the output to
grep
,cut
orawk
.Let's do it step by step. (Not sure what's wrong, the code sample format just didn't work for these 3 example.)
Get the IPv4 line
wolf@linux:~$ nmcli device show lo | grep 4.A
IP4.ADDRESS[1]: 127.0.0.1/8
wolf@linux:~$
Use
awk
to get the IPwolf@linux:~$ nmcli device show lo | awk '/4.A/ {print $2}'
127.0.0.1/8
wolf@linux:~$
Use
cut
to remove the CIDR notation (/8)wolf@linux:~$ nmcli device show lo | awk '/4.A/ {print $2}' | cut -d / -f1
127.0.0.1
wolf@linux:~$
There your answer.
Please take note that there are tons of ways to do it using the tools that I demonstrated just now.
Let's recap the commands that I used.
Sample output for these 3 commands
Command 1 output
Command 2 output
Command 3 output
显示你所有的ip
shows all your ips
在 Redhat 64 位上,这解决了我的问题。
On Redhat 64bit, this solved problem for me.