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.

Ref: to article 'http://www.linuxjournal.com/content/tech-tip-getting-your-mac-and-ip-address-script'

Ex::-
[root@localhost rajesh]# ifconfig |grep '\<inet\>'
          inet addr:192.168.150.4  Bcast:192.168.150.255  Mask:255.255.255.0
          inet addr:192.168.150.100  Bcast:192.168.150.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0
[root@localhost rajesh]# ifconfig |grep '\<inet\>'| sed -n '1p'\
          inet addr:192.168.150.4  Bcast:192.168.150.255  Mask:255.255.255.0
[root@localhost rajesh]# ifconfig |grep '\<inet\>'| sed -n '1p'| tr -s ' ' 
 inet addr:192.168.150.4 Bcast:192.168.150.255 Mask:255.255.255.0
[root@localhost rajesh]# ifconfig |grep '\<inet\>'| sed -n '1p'| tr -s ' '  |cut -d ' ' -f3
addr:192.168.150.4
[root@localhost rajesh]# ifconfig |grep '\<inet\>'| sed -n '1p'| tr -s ' '  |cut -d ' ' -f3 | cut -d ':' -f2
192.168.150.4
 =============================================================
[root@localhost rajesh]# ifconfig |grep 'eth0' | tr -s ' ' | cut -d ' ' -f5
00:XX:YY:B9:YY:D7
=============================================================
[rajesh@localhost ~]$ ifconfig eth0 | perl -ne 'm/.*HWaddr (\S+).*/ && print "$1 ";m/.*inet addr:(\S+)/ && print "$1\n"'
00:XX:YY:B9:YY:D7 192.168.150.4




Comments

Popular posts from this blog

Xmanager RHEL 6 xclock :command not found -- Resolved

Racktable on ubuntu installation

RPM Tit-Bits ;-)