我正在尝试在我编写的脚本中将IP地址(inet)作为参数使用。
在Unix终端中,是否有一种简单的方法可以仅获取IP地址,而不必查看ifconfig
?
我正在尝试在我编写的脚本中将IP地址(inet)作为参数使用。
在Unix终端中,是否有一种简单的方法可以仅获取IP地址,而不必查看ifconfig
?
/sbin/ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'
对于MAC操作系统:
ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2
或者对于Linux系统
hostname -i | awk '{print $3}' # Ubuntu
hostname -i # Debian
hostname -i
命令会返回以下结果:::1 127.0.1.1 192.168.1.100
。 - Book Of Zeus这将会提供给你所有的IPv4接口,包括回环地址127.0.0.1:
ip -4 addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
这将仅显示eth0
:
ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
您可以通过以下方式获取IPv6地址:
ip -6 addr | grep -oP '(?<=inet6\s)[\da-f:]+'
仅限 eth0
的IPv6:
ip -6 addr show eth0 | grep -oP '(?<=inet6\s)[\da-f:]+'
-P
开关,否则我们无法使用后顾(?<=inet\s)
标记。您可以通过运行grep -oe 'inet [0-9\.]\+'
或grep -oe 'inet6 [0-9a-f:]\+'
来获得类似的结果,但这样我无法摆脱第一个单词。在SuSE中,man grep
报告-P
标志是实验性的。 - Jorge Bellonip -4 a show eth0 | grep -Po 'inet \K[0-9.]*'
。该命令用于显示eth0网络接口的IPv4地址。 - tukusejssirsip -4 -br addr show eth0 | awk '{print $3}'
- isp_developerip -4 a show eth0 | awk '/inet/ {gsub(/\/\S+$/,"",$2) ; print $2}'
- krad通常情况下,系统不一定只有一个IP地址,例如,你可以同时拥有以太网和无线网络连接,如果你还有一个活跃的VPN连接,那么你会有另一个IP地址。
在Linux上,hostname -I
命令将列出当前的IP地址(们)。依赖它总是返回一个IP地址很可能在某些情况下无法按预期工作(例如:VPN链接已建立,多个以太网适配器等),因此更可靠的方法是将结果转换为数组,然后遍历其中的元素:
ips=($(hostname -I))
for ip in "${ips[@]}"
do
echo $ip
done
注意:如果
hostname -I
返回 IPv4 和 IPv6 格式的 IP,则可以使用hostname -I | cut -f1 -d' '
仅显示 IPv4 IP。
在 OSX 上,如果您知道接口,则可以使用:
~$ ipconfig getifaddr en0
# OUTPUT: 192.168.1.123
它将仅返回IP地址。
在MacOS上动态检测(第一个)活动的网络接口:
network_device=$(scutil --dns |awk -F'[()]' '$1~/if_index/ {print $2;exit;}')
ip=$(ipconfig getifaddr "$network_device")
echo $ip
### OUTPUT: 192.168.1.123
此外,如果一台机器有多个以太网接口、同时建立了有线和wifi连接,或者VPN隧道已经建立,获取IP地址可能变得不确定。
如果您需要获取外部IP地址,那么可以查询文本模式服务。例如,curl https://ipecho.net/plain
将返回纯文本的外部 IP。
一个可能更快的替代方法是查询已知的DNS服务器,例如:
dig @ns1-1.akamaitech.net ANY whoami.akamai.net +short
hostname -I | xargs -rn1 echo
即可。 - Mikko Rantalainenhostname -I
这个命令可以在Ubuntu中给出你想要的精确IP地址。请注意,标志是大写。
要在 Mac OS X 上仅获取IP地址,您可以输入以下命令:
ipconfig getifaddr en0
在最新的Ubuntu版本(14.04 - 16.04)中,这个命令对我有用。
hostname -I | awk '{print $1}'
print $1
可能会或者不会给出正确的结果。 - 9769953如果你的环境受限,可以使用以下命令:
ip -4 addr show dev eth0 | grep inet | tr -s " " | cut -d" " -f3 | head -n 1
ip -o -4 addr show dev eth0 | cut -d' ' -f7 | cut -d'/' -f1
该命令的作用是显示eth0网络接口的IPv4地址,并将其截取为不包含子网掩码长度的形式。 - Jose Albanip -4 addr show eth0 | grep inet | awk '{print $2}' | cut -d'/' -f1
。
对于v4和v6都是:ip addr show eth0 | grep inet | awk '{print $2}' | cut -d'/' -f1
。 - Sumit Murariip -4 addr show eth0 | grep inet
:ip -4 addr show | grep 192
。 - cyberbirdip -4 address show eth0 | awk '/inet/{split($2,a,"/");print a[1];exit}'
- sparks我不喜欢在脚本中使用awk
等工具,ip
有输出JSON的选项。
如果省略$interface
参数,则会显示所有IP地址:
ip -json addr show $interface | \
jq -r '.[] | .addr_info[] | select(.family == "inet") | .local'
ip -j addr show eth0 | jp "[0].addr_info[?family== 'inet'].local | [0]"
- Jack Liu Shuruiip -json -pretty -family inet addr show scope global | sed -n '/local/{s/[^0-9.]//g;p;q}'
。这将打印具有全局范围(不包括回环)的第一个接口的 IPv4 地址。另一种方法没有使用 sed:ip -json -pretty -family inet addr show scope global | grep -m 1 local | tr -d -c 0-9.
。最后,如果可以的话,您可能想尝试一下 jq,它是处理 ip 输出的非 hacky 方法(终于有一个 tokenizer):ip -json -family inet addr show scope global | jq -r '.[0].addr_info[0].local'
。 - pupitetrisscope global
的建议很有用。在许多情况下,省略jq
中的0
索引是可取的。如果存在多个IP地址,则每行将获得一个IP地址:ip -json -family inet addr show scope global | jq -r '.[].addr_info[].local'
。 - pabouk - Ukraine stay strong在 Linux 上,ifconfig
命令已被弃用,应该使用 ip
命令。
此外,ip a
命令可以在同一行上提供 IP 的范围,更易于使用。
这个命令将显示您的全局(外部)IP:
ip a | grep "scope global" | grep -Po '(?<=inet )[\d.]+'
所有IPv4地址(包括127.0.0.1):
ip a | grep "scope" | grep -Po '(?<=inet )[\d.]+'
ip a | grep "scope" | grep -Po '(?<=inet6 )[\da-z:]+'
ip -4 addr show eth0
在某些机器上无法正常工作。例如,我会收到以下错误:
ip: symbol lookup error: ip: undefined symbol: bpf_program__section_name, version LIBBPF_0.2.0
这个命令对我有效:
/sbin/ifconfig eth0 | grep 'inet ' | awk '{ print $2}'
这个答案比被接受的答案少了一个管道符。此外,我的ifconfig输出中没有 inet addr
。
要获取IPv6地址,请使用以下命令:
/sbin/ifconfig eth0 | grep 'inet6 ' | awk '{ print $2}'