[Tutor] Retrieving arp of gateway

Adrian Maier am@fx.ro
Sun Jun 8 15:42:02 2003


Adam Shand (adam@personaltelco.net) a scris :
> Hi.
> 
> I'm playing around with writing my first Python network program and am 
> trying to figure out a platform neutral way to get the MAC of my 
> gateway.  It's fairly easy to do this with popen's to shell code but you 
> end up having to look for what OS is running and then run slightly 
> different commands for each OS.
> 
> Eg. on Linux
> 
> netstat = os.popen('netstat -rn | grep ^0.0.0.0').readlines()
> arp = os.popen('arp %s' % netstat[0].split()[1]).readlines()
> address = arp[1].split()[2]
> 
> Is there a python native way to query a computers arp table?  I haven't 
> been able to find much on network programming with either on python.org 
> or via google.

I'm not sure if this will help, but:

If i remember correctly, the arp table can be accessed through
the SNMP protocol. 
You need:
 - the pysnmp module (i have found it on 'the vaults of parnassus', 
   and it is hosted on sourceforge).
 - a snmp service running on the computer you want to query.
 
 - you need to find out the OID (object identifier) that
   corresponds to the arp table (i had the "MIB-II" rfc printed 
   somewhere, i think it's either rfc1213 or rfc3418 but i'm 
   not sure).


This approach might be too complicated, especially if you don't have 
a snmp service/daemon already. But with SNMP you could gather some 
other 'interesting' information about the host.


Cheers,

-- 
Adrian Maier
(am@fx.ro)