Hi, and welcome to Ubuntu forums.
I am not an "awk" and "sed" expert, but I'll try to answer. First, the first command only (modified for the computer I am using):
Code:
$ ip addr show ens5
2: ens5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:23:32:9f:a3:cb brd ff:ff:ff:ff:ff:ff
inet 192.168.111.31/24 brd 192.168.111.255 scope global dynamic noprefixroute ens5
valid_lft 74762sec preferred_lft 74762sec
O.K. seems simple enough, information about the interface of interest. Now add the next command:
Code:
$ ip addr show ens5 | grep inet
inet 192.168.111.31/24 brd 192.168.111.255 scope global dynamic noprefixroute ens5
Extract the network information line. Now add the third command:
Code:
$ ip addr show ens5 | grep inet | awk '{ print $2; }'
192.168.111.31/24
Extract the IP address (field 2), however includes netmask. Now add the fourth command:
Code:
$ ip addr show ens5 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
192.168.111.31
Chop off the netmask, and only the actual ip address is left.
Hope this helps
How did I know that my interface name was "ens5"? I did this (which is all I would have in the first place, because the IP address is there):
Code:
~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: ens5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:23:32:9f:a3:cb brd ff:ff:ff:ff:ff:ff
inet 192.168.111.31/24 brd 192.168.111.255 scope global dynamic noprefixroute ens5
valid_lft 74309sec preferred_lft 74309sec
EDIT: My answer differs from Rocket2DMn's because I always run with ipv6 disabled. i.e. in /etc/default/grub:
Code:
GRUB_CMDLINE_LINUX_DEFAULT="ipv6.disable=1"
Bookmarks