[英]Extracting IP address from a line from ifconfig output with grep
Given this specific line pulled from ifconfig
, in my case: 鉴于从
ifconfig
中提取的这条特定行,在我的情况下:
inet 192.168.2.13 netmask 0xffffff00 broadcast 192.168.2.255
How could one extract the 192.168.2.13
part (the local IP address), presumably with regex? 怎么可以提取
192.168.2.13
部分(本地IP地址),大概是用正则表达式?
Here's one way using grep
: 这是使用
grep
的一种方法:
line='inet 192.168.2.13 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
Results: 结果:
192.168.2.13
192.168.2.256
If you wish to select only valid addresses, you can use: 如果您只想选择有效地址,可以使用:
line='inet 192.168.0.255 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | grep -oE "\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
Results: 结果:
192.168.0.255
Otherwise, just select the fields you want using awk
, for example: 否则,只需使用
awk
选择所需的字段,例如:
line='inet 192.168.0.255 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | awk -v OFS="\n" '{ print $2, $NF }'
Results: 结果:
192.168.0.255
192.168.2.256
Addendum: 附录:
Word boundaries : \\b
单词边界 :
\\b
使用这个正则表达式((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?=\\s*netmask)
you can use egrep (which is basically the same as grep -E) 你可以使用egrep(与grep -E基本相同)
in egrep there are named groups for character classes, eg: "digit" 在egrep中,有一些用于字符类的命名组,例如:“digit”
(which makes the command longer in this case - but you get the point...) (在这种情况下,这会使命令更长 - 但你明白了......)
another thing that is good to know is that you can use brackets to repeat a pattern 另一件值得注意的事情是你可以使用括号 重复一个模式
ifconfig | egrep '([0-9]{1,3}\.){3}[0-9]{1,3}'
or 要么
ifconfig | egrep '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
if you only care about the actual IP address use the parameter -o to limit output to the matched pattern instead of the whole line: 如果您只关心实际的IP地址,请使用参数-o将输出限制为匹配的模式而不是整行:
ifconfig | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
...and if you don't want BCast addresses and such you may use this grep: ...如果你不想要BCast地址,你可以使用这个grep:
ifconfig | egrep -o 'addr:([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | egrep -o '[[:digit:]].*'
I assumed you were talking about IPv4 addresses only 我以为你只是在讨论IPv4地址
Just to add some alternative way: 只是添加一些替代方式:
ip addr | grep -Po '(?!(inet 127.\d.\d.1))(inet \K(\d{1,3}\.){3}\d{1,3})'
it will print out all the IPs but the localhost one. 它将打印出所有IP,但是打印出localhost。
[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}
I don't have enough reputation points to comment, but I found a bug in Steve's "select only valid addresses" regex. 我没有足够的声誉点来评论,但我在史蒂夫的“仅选择有效地址”正则表达式中发现了一个错误。 I don't quite understand the problem, but I believe I have found the fix.
我不太明白这个问题,但我相信我找到了解决办法。 The first command demonstrates the bug;
第一个命令演示了这个bug; the second one demonstrates the fix:
第二个演示了修复:
$ echo "test this IP: 200.1.1.1" |grep -oE "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
$ echo "test this IP: 200.1.1.1" |grep -oE "\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
200.1.1.1
$
One way using sed
. 使用
sed
一种方法。 First instruction deletes all characters until first digit in the line, and second instruction saves first IP in group 1 ( \\1
) and replaces all the line with it: 第一条指令删除所有字符,直到行中的第一个数字,第二条指令保存第1组中的第一个IP(
\\1
)并用它替换所有行:
sed -e 's/^[^0-9]*//; s/\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*/\1/'
This code works nicely and easy too. 这段代码也非常简单易用。
ifconfig | grep Bcast > /tmp/ip1
cat /tmp/ip1 | awk '{ print $2 }' > /tmp/ip2
sed -i 's/addr://' /tmp/ip2
IPADDRESS=$(cat /tmp/ip2)
echo "$IPADDRESS"
也许这个,一个sed命令,仅用于运动:
ip -o -4 addr show dev eth0 | sed 's/.* inet \([^/]*\).*/\1/'
grep -oE "\b([0-9]{1,3}\.?){4}\b"
This code works for me on raspberry pi zero w . 这段代码适用于我的覆盆子pi零w 。
(extract wlan0: inet 192.168.xy address from ifconfig
output) (从
ifconfig
输出中提取wlan0:inet 192.168.xy地址)
Search for pattern 'inet 192' in ifconfig output and get the 10th position using space delimiter. 在ifconfig输出中搜索模式'inet 192'并使用空格分隔符获取第10个位置。
$> ifconfig |grep 'inet 192'|cut -d' ' -f10
Output: 192.168.1.6 输出: 192.168.1.6
如果使用支持Perl正则表达式的grep:
(your command that pulls mentioned line) | grep -Po 'inet \K[\d\.]+'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.