Wednesday, January 6, 2010

[HOW TO] get MAC Address and IP in shell

Do you wanted to get the MAC or IP address of your computer in a Linux shell script?

Some people including me feel it is troublesome to get the real ip if using router.The following commands can help you to get them easily.

(These commands only get information from eth0 interface. If you have many network interfaces, you need to modify the command by changing "eth0" to your favour(e.g. lo))

To get internal IP address:

ifconfig eth0 | grep 'inet '  | sed -n '1p' | tr -s ' ' | cut -d ' ' -f3 | cut -d ':' -f2


To get MAC address

ifconfig | grep 'eth0' | tr -s ' ' | cut -d ' ' -f5 


To get external IP address:

wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1 | sed -e 's/^ *//' -e 's/ *$//'


If you think these commands are good and want to be your shell command, just copy following commands to "~/.bashrc" or "~/.bash_profile"

alias showip="ifconfig | grep 'inet ' | sed -n '1p' | tr -s ' ' | cut -d ' ' -f3 | cut -d ':' -f2"
alias showmac="ifconfig | grep 'eth0' | tr -s ' ' | cut -d ' ' -f5 "
alias showrealip="wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1 | sed -e 's/^ *//' -e 's/ *$//'"

No comments:

Post a Comment