39215

Python: get MAC address of default gateway

Is there any quick way in Python to get the <strong>MAC address</strong> of the <strong>default gateway</strong>?

I can't make any <strong>ARP requests</strong> from the <strong>Linux</strong> machine I'm running my code on, so it has to come directly from the <strong>ARP table</strong>.

Answer1:

The following DevFS files will provide this information:

/proc/net/arp /proc/net/route

Find the route entry with 0/0 as the host/mask:

Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT eth0 00000000 B91A210A 0003 0 0 100 00000000 0 0 0

and convert the Gateway field (it's in little-endian hex... grr):

import struct from IPy import IP address = IP(struct.unpack('<I', struct.pack('>I', int(i, 16)))[0]) #Converts 'B91A210A' to IP('10.33.26.185')

From there, you can find your default gateway in the arp table:

IP address HW type Flags HW address Mask Device 10.33.26.185 0x1 0x2 e6:92:ec:f5:af:f7 * eth0

If it doesn't show up, issue a single ping, then check again, then fail.

Answer2:

Are you using Linux? You could parse the /proc/net/arp file. It contains the HW address of your gateway.

Answer3:

You can read from /proc/net/arp and parse the content, that will give you couples of known IP-MAC addresses.

The gateway is probably known at all times, if not you should ping it, and an ARP request will be automatically generated.

You can find the default gw in /proc/net/route

Recommend

  • python TypeError: __init__() takes exactly 4 arguments (1 given)
  • Ninject does not trigger method when binding to method?
  • Unexpected top level exception: bad class file magic, in Android
  • package org.apache.thrift does not exist
  • Android emulator loads but not the program?
  • Sending PublicKey within packet payload
  • jQuery accordion - Open item on page load
  • aggregate list with linq with sum
  • find the space occupied by records in mysql
  • Animate using a pixmap or image sequence in Python with QT4
  • Google Analytics API deviceCategory
  • DropShadowPanel adapt to button template style
  • Connecting Couchbase sync adapter to couchdb server
  • Excel rendered from SSRS loses currency symbold when viewed on iPhone in UK
  • D3 Tree layout visualization - Inherit child with multiple parents
  • How to integrate CCAvenue to Ionic app?
  • closing WCF proxy
  • Replace any string in columns with 1
  • Difficulties implementing the Hysteresis step of Canny Algorithm in Halide without define_extern fun
  • Unable to use dot layout (graphviz as a library)
  • Why do GeoJSON features appear like a negative photo of the features themselves?
  • Cannot connect to native local socket on android 5.1
  • Android NFC read Tags issue. Activity starts each time on data received
  • openssl handshake failed
  • Unique SMS sender id?
  • How to resolve docker host names (/etc/hosts) in containers
  • Unix Network Programming Clarification
  • Thread synchronization with syncwarp
  • Google Maps api v3 get start and end coordinates of a street
  • Is it possible to run clang with llc flags
  • Django model for a Postgres view
  • Clear activity stack before launching another activity
  • Insert new calendar with SyncAdapter- Calendar API Android
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • Swift: Switch statement fallthrough behavior
  • How to test if a URL from an Eclipse bundle is a directory?
  • What do the 'size' numbers mean in the windbg !heap output?
  • Android Studio and gradle
  • How to set the response of a form post action to a iframe source?
  • need help with bizarre java.net.HttpURLConnection behavior