solaris
solaris

Reputation: 321

Using awk to pull out IP address from RHEL 6 and RHEL 7 ifconfig

I am trying to use awk to pull out IP addresses from ifconfig on RHEL6 and RHEL7. There is a subtle difference in the output as follows:

# --- RHEL6 ---

em1   Link encap:Ethernet  HWaddr XX:YY:ZZ:DB:7C:BF
      inet addr:10.11.99.1  Bcast:10.11.99.255  Mask:255.255.254.0
      inet6 addr: fe80::eef4:zzz:yyy:xxx/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:250604031 errors:0 dropped:0 overruns:0 frame:0
      TX packets:574102184 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:17030959416 (15.8 GiB)  TX bytes:867712134376 (808.1 GiB)
      Memory:91b00000-91bfffff

# --- RHEL7 ---

em1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 10.11.99.2  netmask 255.255.254.0  broadcast 10.11.99.255
    ether ec:f4:bb:zz:yy:xx  txqueuelen 1000  (Ethernet)
    RX packets 559121109  bytes 38360873120 (35.7 GiB)
    RX errors 0  dropped 24402  overruns 0  frame 0
    TX packets 1747482075  bytes 2639172927753 (2.4 TiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    device memory 0x91d00000-91dfffff

Notice the IP lines that start with "inet". In RHEL6 this is "inet addr:" and in RHEL7 this is simply "inet".

I have the following awk one-liners that handle both scenarios:

RHEL6:

ifconfig  | awk '/inet addr/ {gsub("addr:", "", $2); print $2}' | grep -v '127.0.0.1'

RHEL7:

ifconfig  | awk '/inet/ {print $2}' | grep -v '127.0.0.1'

Both work, but I want to combine them so it will handle both forms of output. I've tried the following:

ifconfig  | awk '/inet addr/ {gsub("addr:", "", $2)} /inet/ {print $2}' | grep -v '127.0.0.1'

This works for for RHEL7 but not quite for RHEL6.

Any ideas please?

Thanks.

Upvotes: 1

Views: 285

Answers (4)

James K. Lowden
James K. Lowden

Reputation: 7837

You want every inet line. You want to print the next field unless it's addr:, in which case you want the one after that. So say so:

$ awk -F'[ :]+' '$2 == "inet" {print /addr:/? $4 : $3}' dat
10.11.99.1
10.11.99.2

Upvotes: 1

grail
grail

Reputation: 930

I agree I would use ip normally, but assuming you have to stick with ifconfig:

ifconfig | awk '$1 == "inet"{print gensub(/[a-z:]/,"","g",$2)}'

Upvotes: 1

iamauser
iamauser

Reputation: 11489

I would recommend not to use ifconfig. Use ip addr instead.

$ ip addr show dev em1 | awk '/inet/ {print $2;exit}'

This prints the ipv4 address with /24, e.g., 1.2.3.4/24

You can slice off the /24 by using substr from awk

$ ip addr show dev em1 | awk '/inet/ {print substr($2, 1, length($2)-3);exit}'

ifconfig is not part of the Base/Minimal installation in EL7. Its output may change in future similar to what you see between EL6 and EL7, whereas ip command is low-level implementation and it's being used by many system configuration scripts and thus more reliable.

Here is one with ifconfig and awk

$ ifconfig em1 | \
   awk 'match($2, /[0-9]+.[0-9]+.[0-9]+.[0-9]+$/){print substr($2, RSTART, RLENGTH);exit}'

Upvotes: 0

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185570

Using :

$ ifconfig | grep -oP '^\s*inet\s+(addr:)?\K\d+\.\d+\.\d+\.\d+'

(tested for both cases)

Upvotes: 0

Related Questions