Get IP Address Linux

From MS Computech
Jump to: navigation, search

This is quick tip, howto get internal IP address and external IP address on Linux Shell / Command Line. This guide also show, howto make useful Bash functions to get IP addresses quickly.

Note: All functions could be named as you wish and to make functions permanent, add functions to ~/.bashrc or /etc/bashrc. Also all awk commands should work also with gawk and nawk.

/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
10.20.10.1
function int-ip { /sbin/ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'; }
 
## Example usage ##
int-ip eth0
10.20.10.1
/sbin/ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'
 
## Example output ##
eth0: 10.20.10.1
eth1: 10.20.1.168
lo: 127.0.0.1
function int-ips { /sbin/ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'; }
 
## Example usage ##
int-ips
eth0: 10.20.10.1
eth1: 10.20.1.168
lo: 127.0.0.1
lynx --dump http://whatismyip.org
 
## Example output ##
80.10.10.80
function ext-ip () { lynx --dump http://whatismyip.org; }
 
## Example usage ##
ext-ip
80.10.10.80
curl http://whatismyip.org; echo
 
## Example output ##
80.10.10.80
function ext-ip () { curl http://whatismyip.org; echo; }
 
## Example usage ##
ext-ip
80.10.10.80