I want to edit the bashrc file to have a simple function called \"myip\" to run. As you might guess, the function myip prints only my internal IP address of my machine.
This is the more "agnostic" way to get the IP address, regardless of you *nix system (Mac OS, Linux), interface name, and even your locale configuration:
ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d:
If you have more than one active IP, will listed each one in a separated line. If you want just the first IP, add | head -n1
to the expression:
ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" \
| grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1
And if you want the IP address of a specific interface, replace the first expression ifconfig
by ifconfig INTERFACENAME
, for example ifconfig eth0 | grep -E ...
.
Finally, you noticed that one of the things the script does is to omit the IP 127.0.0.1
called by sysadmins and developers "localhost", but take into account that some applications may also add virtual network devices with an IP that may be the one returned if is not added to the omit list, eg. Docker adds a virtual interface docker0
that usually has the IP 172.17.0.1
, if you want to omit this IP along with the "localhost" one, change the expression grep -v 127.0.0.1
to take into account the another IP. In this case (omit localhost and docker) the final expression will be:
ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v '[127.0|172.17].0.1' | awk '{ print $2 }' | cut -f2 -d: | head -n1
These are some examples mentioned in this page that fails in some circumstances and why:
ip route ...
: the ip
command isn't installed in OSX machines.hostname -I
: the -I
option is invalid in OSX.ifconfig en0 ...
: the interfaces names (eth0
, en0
) are different in Linux and OSX, and in Linux the name depends also of the interface type (ethX for ethernet connection, wlanX for wireless, etc.).python -c 'import socket; print(socket.gethostbyname(socket.gethostname()))'
: this got me 127.0.1.1
(a loopback IP) in Ubuntu Linux 14.04, so doesn't work.ifconfig | grep 'inet addr:' | grep -v 127.0.0.1 | head -n1 | cut -f2 -d: | cut -f1 -d ' '
: the Geograph's post is the more close, but doesn't work in some Linux distributions without LANG=en
configured, because the text inet addr:
that grep
looks for is output with a different text in other locales, and in Mac OS that label is also different.A simple AWK + Bash script example which may give general idea how to parse command output and mix syntaxes together.
Full code is at: https://gist.github.com/darkphase/67d7ec22d47dbebd329e
BEGIN { RS = "" ; FS = "\n" } # Change separator characters
{
while ( "'"$cmd"'" | getline ){
# print $0
if ( $1 !~ /LOOPBACK/ ){
split($1,arr," ")
print "'"$blue"'"arr[1]"'"$reset"'"
for(i = 1; i <= NF; i++) { # Loop through fields (this case lines)
split($i,arr," ")
switch ( arr[1] ) {
case "inet":
print "'"$red"'" "IPV4:" "'"$reset"'" "\n IP: " "'"$yellow"'" arr[2] "'"$reset"'" "\n NM: "arr[4]"\n BC: "arr[6]
break
case "inet6":
print "'"$red"'" "IPV6:" "'"$reset"'" "\n IP: "arr[2]"\n PL: "arr[4]
break
case "ether":
print "'"$red"'" "MAC: " "'"$reset"'" arr[2]
break
default:
}
}
print ""
}
}
}'
There is another easy way to get the IP address apart from parsing ifconfig.
hostname -I
-I, --all-ip-addresses all addresses for the host
-i, --ip-address addresses for the hostname
Ref: http://linux.die.net/man/1/hostname
Example:
[ec2-user@terraform ~]$ hostname -I
10.10.10.10
In case of eth0, the following works for me. Try to tweak it with the same logic.
ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
If you're looking for just "inet" and not "inet6", this works for me:
/usr/bin/ifconfig eth0 | grep --word-regexp inet | awk '{print $2}'
"--word-regexp" will make grep look for the whole word "inet" and not match partials of words, i.e. "inet" won't match "inet6" - "inet" will only match lines with "inet" in them.
ifconfig eth0 | awk '/inet addr/{sub("addr:",""); print $2}'