简体   繁体   中英

Extracting IP address from a line from ifconfig output with grep

Given this specific line pulled from ifconfig , in my case:

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?

Here's one way using 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:

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

使用这个正则表达式((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)
in egrep there are named groups for character classes, eg: "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:

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:

ifconfig | egrep -o 'addr:([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | egrep -o '[[:digit:]].*'

I assumed you were talking about IPv4 addresses only

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.

[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; 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 . 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:

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 .

(extract wlan0: inet 192.168.xy address from ifconfig output)

Search for pattern 'inet 192' in ifconfig output and get the 10th position using space delimiter.

 $> ifconfig |grep 'inet 192'|cut -d' ' -f10

Output: 192.168.1.6

如果使用支持Perl正则表达式的grep:

(your command that pulls mentioned line) | grep -Po 'inet \K[\d\.]+'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM