Labels

Friday, 24 October 2014

to find mac and ip address in linux through shell scripting

Ever wanted to get the MAC or IP address of your computer in a Linux shell script? The following two commands should work on most flavours of Linux/Unix.
To get your IP address:
/sbin/ifconfig \
   | grep '\<inet\>' \
   | sed -n '1p' \
   | tr -s ' ' \
   | cut -d ' ' -f3 \
   | cut -d ':' -f2
To get your MAC address (Hardware address):
/sbin/ifconfig \
   | grep 'eth0' \
   | tr -s ' ' \
   | cut -d ' ' -f5
Note that this retrieves the address of the eth0 interface by default.

No comments:

Post a Comment