Friday, February 8, 2013

Get MAC-Address of default gateway

I used this to find out which router was used as the default gateway. It's very simple and may need some tweaking, but it worked for my needs.
#! /usr/bin/perl -w

use strict;
use warnings;

my $route = `ip route`;
$route =~ /^default via ((\d+\.?){4})/;

my $router_ip = $1 if $1;

die "unknown\n" unless $router_ip;

my $arp = `cat /proc/net/arp | grep $router_ip`;

my ($ip, $type, $flags, $mac, $mask, $device) = split /\s+/, $arp;

print $mac."\n";

 

 

No comments:

Post a Comment