I\'m trying to use just the IP address (inet) as a parameter in a script I wrote.
Is there an easy way in a unix terminal to get just the IP address, rather than loo
We can simply use only 2 commands ( ifconfig + awk ) to get just the IP (v4) we want like so:
On Linux, assuming to get IP address from eth0 interface, run the following command:
/sbin/ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'
On OSX, assumming to get IP affffdress from en0 interface, run the following command:
/sbin/ifconfig en0 | awk '/inet /{print $2}'
To know our public/external IP, add this function in ~/.bashrc
whatismyip () {
curl -s "http://api.duckduckgo.com/?q=ip&format=json" | jq '.Answer' | grep --color=auto -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
}
Then run, whatismyip