Code Snippet

Just another Code Snippet site

[Unix] Get IP address using shell script

ifconfig | perl -nle 's/dr:(\S+)/print $1/e'

or

ifconfig | awk '/inet addr/{print substr($2,6)}'

to retrieve the first one :

ifconfig | perl -nle 's/dr:(\S+)/print $1/e' | head -n1
ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'


Leave a Reply

Your email address will not be published. Required fields are marked *