Connecting Tech Pros Worldwide Forums | Help | Site Map

Help me Extract a MAC Address

Newbie
 
Join Date: Mar 2006
Posts: 1
#1: Mar 20 '06
Im trying to extract the Mac address from ifconfig eth0

$HWAddr = `ifconfig eth0`;


$HWAddr= m/([0-9a-fA-F]{0,2})[:]?([0-9a-fA-F]{0,2})[:]?([0-9a-fA-F]{0,2})[:]?([0-9a-fA-F]{0,2})[:]?([0-9a-fA-F]{0,2})[:]?([0-9a-fA-F]{0,2})/g;
But being a perl newbie im really struggling, any help much appreciated :)

LH

Newbie
 
Join Date: Aug 2006
Posts: 2
#2: Aug 10 '06

re: Help me Extract a MAC Address


[quote=leehaggan]Im trying to extract the Mac address from ifconfig eth0

There are several modules that do this in CPAN. Look in Net::Packet::Utils for starters.

Peace,
Hacker
Newbie
 
Join Date: Aug 2006
Posts: 1
#3: Aug 16 '06

re: Help me Extract a MAC Address


Here is a quick script I chruned out. It could be neatened up a bit, naturally, but should get you where you want to go.


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. $ifconfigin= `/sbin/ifconfig eth0`;
  3. @ifeth = split(/\n/, $ifconfigin);
  4. ($junk,$macaddr) = split(/ HWaddr /, $ifeth[0]);
  5. print "$macaddr\n";

Dave
http://www.flanigan.net
Reply