filter/get ip of linux host

filter all host ips with ip command

ip addr | awk '/inet / {print $NF, $2}'

filter second host ip with ip command

ip addr | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | sed -n 2p

get gateway ip with ip command

ip route | awk '/default/ {print $3}'

get interface connected on default gateway with ip command

ip route | awk '/^default.*dev/ { for (x=1; x<NF; x++) if ( $x ~ "dev" ) print $(x+1) }'

filter third host ip with ifconfig command

ifconfig | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | sed -n 3p

get gateway ip with route command

route -n | awk '/^0.0.0.0/ {print $2}'

get lan ip of default gateway

ip route | head -n 1 | grep -Eo '([0-9]{1,3}\.){3}'

filter IP of eth0 interface

ip addr show eth0 | grep -Po 'inet \K[\d.]+'

filter IP of interface connected in default gateway with ifconfig command

ifconfig $( route -n | awk '/^0.0.0.0/ {print $NF}' ) | grep -Po 't addr:\K[\d.]+'

filter IP of interface connected in default gateway with ip command

ip addr show $(route -n | awk '/^0.0.0.0/ {print $NF}') | grep -Po 'inet \K[\d.]+'
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s