Skip to content

Instantly share code, notes, and snippets.

@danielfeitopin
Created December 3, 2024 10:42
Show Gist options
  • Save danielfeitopin/8b62e5115babcd144b17f4f24955524a to your computer and use it in GitHub Desktop.
Save danielfeitopin/8b62e5115babcd144b17f4f24955524a to your computer and use it in GitHub Desktop.
Python get ARP table
import os
import re
def get_arp_table() -> dict[str, str]:
arp_table = {}
try:
with os.popen("arp -a") as arp_output:
output = arp_output.read()
for line in output.splitlines():
match = re.search(r"(\d{1,3}(?:\.\d{1,3}){3}).*?([\da-fA-F]{2}(?:(?::|[\-])[\da-fA-F]{2}){5})", line)
if match:
ip, mac = match.groups()
arp_table[ip] = mac
except Exception as e:
print(f"Error reading ARP table: {e}")
return arp_table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment