将 IP 地址放入 bash 变量。有没有更好的办法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6829605/
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
Putting IP Address into bash variable. Is there a better way
提问by user000001
I'm trying to find a short and robust way to put my IP address into a bash variable and was curious if there was an easier way to do this. This is how I am currently doing it:
我试图找到一种简短而可靠的方法将我的 IP 地址放入 bash 变量中,并且很好奇是否有更简单的方法来做到这一点。这就是我目前的做法:
ip=`ifconfig|xargs|awk '{print }'|sed -e 's/[a-z]*:/''/'`
采纳答案by woliveirajr
You can take a look at this sitefor alternatives.
您可以查看此站点以获取替代方案。
One way would be:
一种方法是:
ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print }'
A bit smaller one, although it is not at all robust, and can return the wrong value depending on your system:
稍微小一点,虽然它根本不健壮,并且可能会返回错误的值,具体取决于您的系统:
$ /sbin/ifconfig | sed -n '2 p' | awk '{print }'
回答by TuxSax
I've been struggling with this too until I've found there's a simple command for that purpose
我也一直在为此苦苦挣扎,直到我发现有一个简单的命令用于此目的
hostname -i
Is that simple!
有那么简单吗!
回答by user000001
man hostname
recommends using the --all-ip-addresses
flag (shorthand -I
), instead of -i
, because -i
works only if the host name can be resolved. So here it is:
man hostname
建议使用--all-ip-addresses
标志(简写-I
),而不是-i
,因为-i
只有在可以解析主机名时才有效。所以这里是:
hostname -I
And if you are interested only in the primary one, cut
it:
如果你只对初级感兴趣,cut
它:
hostname -I | cut -f1 -d' '
回答by Synchro
ip
is the right tool to use as ifconfig
has been deprecated for some time now. Here's an awk/sed/grep-free command that's significantly faster than any of the others posted here!:
ip
是正确的工具,因为它ifconfig
已经被弃用了一段时间。这是一个 awk/sed/grep-free 命令,它比这里发布的任何其他命令都快得多!:
ip=$(ip -f inet -o addr show eth0|cut -d\ -f 7 | cut -d/ -f 1)
(yes that is an escaped space after the first -d
)
(是的,这是第一个之后的转义空间-d
)
回答by Matthew G
The ifdata command (found in the moreutilspackage) provides an interface to easily retrieve ifconfig data without needing to parse the output from ifconfig manually. It's achieved with a single command:
ifdata 命令(在moreutils包中找到)提供了一个界面来轻松检索 ifconfig 数据,而无需手动解析 ifconfig 的输出。它是通过一个命令实现的:
ifdata -pa eth1
Where eth1
is the name of your network interface.
eth1
您的网络接口的名称在哪里。
I don't know how this package behaves when ifconfig is not installed. As Syncrho stated in his answer, ifconfig has been deprecated for sometime, and is no longer found on a lot of modern distributions.
我不知道当没有安装 ifconfig 时这个包的行为。正如 Syncrho 在他的回答中所说的那样, ifconfig 已经被弃用了一段时间,并且在许多现代发行版中不再存在。
回答by Jotne
Here is the best way to get IP address of an device into an variable:
以下是将设备的 IP 地址获取到变量中的最佳方法:
ip=$(ip route get 8.8.8.8 | awk 'NR==1 {print $NF}')
NB Update to support new Linux version. (works also with older)
NB 更新以支持新的 Linux 版本。(也适用于较老的)
ip=$(ip route get 8.8.8.8 | awk -F"src " 'NR==1{split(,a," ");print a[1]}')
Why is it the best?
为什么它是最好的?
Hostname -I
some times get only the IP or as on my VPS it gets127.0.0.2 143.127.52.130 2a00:dee0:ed3:83:245:70:fc12:d196
Hostnmae -I
does not work on all system.- Using
ifconfig
may not always give the IP you like.
a. It will fail you have multiple interface (wifi/etcernet) etc.
b. Main IP may not be on the first found interface Searching of
eth0
may fail if interface have other name as inVPS
server orwifi
ip route get 8.8.8.8
Hostname -I
有时只获得 IP 或在我的 VPS 上获得127.0.0.2 143.127.52.130 2a00:dee0:ed3:83:245:70:fc12:d196
Hostnmae -I
不适用于所有系统。- 使用
ifconfig
可能并不总是提供您喜欢的 IP。
一种。如果您有多个接口(wifi/etcernet)等,它会失败。
b. 主 IP 可能不在第一个找到的接口上 搜索的
eth0
可能,如果接口有其他名称作为失败VPS
的服务器或wifi
ip 路由得到 8.8.8.8
Tries to get route and interface to Googles DNS server (does not open any session)
Then its easy to get the ip
or interface name if you like.
尝试获取到 Google 的 DNS 服务器的路由和接口(不打开任何会话)
然后ip
如果您愿意,可以轻松获取或接口名称。
This can also be used to get a ip
address of an interface to a host on a multiruted net
这也可用于获取多ip
路网络上主机的接口地址
回答by skatkatt
my short version. Useful when you have multiple interface and just want the main ip.
我的简短版本。当您有多个接口并且只想要主 IP 时很有用。
host `hostname` | awk '{print }'
回答by gpojd
Not really shorter or simpler, but it works for me:
不是更短或更简单,但它对我有用:
ip=$(ip addr show eth0 | grep -o 'inet [0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | grep -o [0-9].*)
回答by glenn Hymanman
You can get just awk to do all the parsing of ifconfig:
您可以使用 awk 来完成 ifconfig 的所有解析:
ip=$(ifconfig | gawk '
/^[a-z]/ {interface = }
interface == "eth0" && match(#!/bin/bash
#get interface used for defalt route (usually en0)
IF=$(route get default |grep 'interface' |awk -F: '{print }');
#get the IP address for inteface IF
#does ifconfig, greps for interface plus 5 lines, greps for line with 'inet '
IP=$(ifconfig |grep -A5 $IF | grep 'inet ' | cut -d: -f2 |awk '{print }');
#get the gateway for the default route
GW=$(route get default | awk '/gateway:/ {print }');
, /^.*inet addr:([.0-9]+)/, a) {
print a[1]
exit
}
')
回答by John Springer
The following works on Mac OS (where there is no ip commnand or hostname options):
以下适用于 Mac OS(没有 ip commnand 或主机名选项):
##代码##