Sunday, April 5, 2009

How to write a shell/C code to obtain Hardware/MAC address

We present 6 alternatives to obtain the hardware
addresses programatically through a shell script.
These scripts may be run independently or may be
invoked through C code with the
system() calls.

Considering that an ifconfig
on the node generates the output that yields the following
lines -
infinity:~# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:XX:YY:ZZ:WW:QQ
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

Each of the following lines of code can be run independently
to obtain the hardware address. Again, they may be invoked through
the C system command to obtain the address in C.

ifconfig eth0 | grep HWaddr | tail -c 20

ifconfig eth0 | grep HW | awk '{print $5}'

ifconfig eth0 | grep HW | cut -d' ' -f 11

ifconfig eth0 | head -1 | awk '{print $5}'

ifconfig eth0 | head -1 | cut -d' ' -f 11

ifconfig eth0 | head -1 | tail -c 20


Anonymous said...

Thanks it worked for me

Post a Comment