Well depending on your OS there are a couple of ways you can do this. If it is a *nix machine you can do smoething like.
open (READ, "ifconfig fxp0 |") or die "$!";
while (<READ>) {
chomp;
if (/ether/) {
s/^\s+//;
my $mac = (split)[1];
}
}
close READ;
I haven't tested this code, but I am pretty sure that would do it. And if it is an NT machine you would just do something similar with ipconfig instead. Note you should replace fxp0 with the name of your network device.
Update: I just tested the code and it worked. | [reply] [d/l] |
| [reply] |
arp -a is not that good as you get the entire arp cache. ipconfig /all is much better as it allows you to parse data per interface.
I guess this is TIMTOWTDI, but using ipconfig /all allows you to write a reusable sub that returns all of the parsed data for you, and allows you to pick and choose which of that data you want to use. I guess I'm lazy and just do all the work up front and never have to deal with it again :)
"Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
| [reply] [d/l] [select] |
| [reply] [d/l] [select] |
If you are running this on a *NIX based platform, you should look into the IO::Interface module - This module gives you access to a wealth of information about network interface configurations. For example, from the documentation ...
use IO::Socket;
use IO::Interface qw(:flags);
my $s = IO::Socket::INET->new(Proto => 'udp');
my @interfaces = $s->if_list;
for my $f (@interfaces) {
print "interface = $if\n";
my $flags = $s->if_flags($if);
print "addr = ",$s->if_addr($if),"\n",
"broadcast = ",$s->if_broadcast($if),"\n",
"netmask = ",$s->if_netmask($if),"\n",
"dstaddr = ",$s->if_dstaddr($if),"\n",
"hwaddr = ",$s->if_hwaddr($if),"\n";
print "is running\n" if $flags & IFF_RUNNING;
print "is broadcast\n" if $flags & IFF_BROADCAST;
print "is p-to-p\n" if $flags & IFF_POINTOPOINT;
print "is loopback\n" if $flags & IFF_LOOPBACK;
print "is promiscuous\n" if $flags & IFF_PROMISC;
print "is multicast\n" if $flags & IFF_MULTICAST;
print "is notrailers\n" if $flags & IFF_NOTRAILERS;
print "is noarp\n" if $flags & IFF_NOARP;
}
If all that you are looking for is the MAC address of the interface card, then the $obj->if_hwaddr method is all you will need to worry about.
perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print' | [reply] [d/l] [select] |
Yep, this works really well, so I will be able to start coding. Thanks to everyone that offered suggestions. I didn't want to use an external program unless there was no other way. I'll post a snippet when I have something finished. There is a small typo though -
for my $f (@interfaces) {
should be:
for my $if (@interfaces) {
otherwise nothing prints out..
Mildew Hall.. Home of PurePostPro and other Perl goodies!
Not only oysters create Pe[a]rls | [reply] [d/l] [select] |
| [reply] |