Re: how to discover MAC address?

afarrior@vc.cc.tx.us
Tue, 15 Jul 1997 18:42:14 -0500

At 06:14 PM 7/14/97 -0500, you wrote:
>
>i'd like to determine the hardware address for given a host IP address and
>the appropriate router IP address.
>

ok, i made a VERY crude script if anyone is interested.

thanks for the info,
andy

#!/usr/bin/perl
# NAME
# getmac
# SYNOPSYS
# getmac <hostname>
# DESCRIPTION
# Getmac attempts to learn the MAC address from a router for
# a given IP address.
#
# getmac assumes:
# - the router can be accessed via SNMP.
# - the host can respond to traceroute packets.
# - the host is alive

$TRACEROUTE="/usr/sbin/traceroute -n";
$COMMUNITYNAME="public";
$SNMPGET="/usr/bin/snmpget";
# this is to get those hosts on the same wire
$DEFAULTROUTER="192.168.250.5";

unless ($ARGV[0]) {
print "Enter target hostname or IP address: ";
chomp($host=<STDIN>);
} else {
$host=$ARGV[0];
}

$router=&GetRouter($host);
$mac=&GetMAC($hostip,$router);
print "$mac\n";
exit;

################################################################3
sub GetMAC {
my($hostip,$router)=@_;

# let's see how many network interfaces are on the router
$object_identifer="interfaces.ifNumber.0";
chomp($interfaces=`$SNMPGET $router $COMMUNITYNAME $object_identifer`);
($interfaces)=(split(' = ',$interfaces))[1];
#print "Interfaces: $interfaces\n";

# since we don't know which interface the destination host
# is on, we need to go through each interface.
# ick... there's got to be a better way.
$interface=1;
while ($interface <= $interfaces) {

# just calling the snmpget command
$variable="at.atTable.atEntry.atPhysAddress";
$object_identifer="$variable.$interface.1.$hostip";
chomp(@stuff=`$SNMPGET $router $COMMUNITYNAME $object_identifer`);

# if we get an error, we need to check the next interface
if (/^Error/,$stuff[1]) {
$interface=$interface+1;
next;
}

# otherwise we need to extract the Hex address and kick out of the loop
($variable,$hex)=split(' = ',$stuff[0]);
($nic)=(split(':',$hex,2))[1];
$nic =~ tr/0-9a-fA-F//cd;
$interface=$interfaces+1;
}
return $nic;
}

sub GetRouter {
my($host)=@_;

#
# Trace route to destination host. This should place
# the physical address for the destination host in the arp cache
# of the last router.
#
# traceroute output:
#
# % traceroute -n 192.168.1.1
#
# 1 192.168.250.5 0.942 ms 0.82 ms 0.83 ms
# 2 192.168.252.154 2.97 ms 2.795 ms 2.864 ms
# 3 192.168.1.1 2.97 ms 2.795 ms 2.864 ms
#

@traceroute=`$TRACEROUTE $host 2>/dev/null`;
($lasthop,$ip)=split(' ',$traceroute[$#traceroute]);

# need to keep the IP address of the destination host.
$hostip=$ip;

if ($lasthop == 1) {
$router=$DEFAULTROUTER;
# or probe the local ARP cache
# print "$host is on this subnet\n";
} else {
($router)=(split(' ',$traceroute[$#traceroute-1]))[1];
# print "the router of interest is $router\n";
}
return $router;
}

--
!! This message is brought to you via the `tkined & scotty' mailing list.
!! Please do not reply to this message to unsubscribe. To subscribe or
!! unsubscribe, send a mail message to <tkined-request@ibr.cs.tu-bs.de>.
!! See http://wwwsnmp.cs.utwente.nl/~schoenw/scotty/ for more information.