Created
December 3, 2024 10:42
-
-
Save danielfeitopin/8b62e5115babcd144b17f4f24955524a to your computer and use it in GitHub Desktop.
Python get ARP table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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