summaryrefslogtreecommitdiff
path: root/src/walu/arp.py
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-02-11 15:58:43 +0530
committersinanmohd <sinan@sinanmohd.com>2024-02-15 19:25:05 +0530
commit102429b18255f0364426fd377ac988c3d4624e6f (patch)
treef5492495989fba5016857f9eaf44da00e0ab210e /src/walu/arp.py
repo: init
Diffstat (limited to 'src/walu/arp.py')
-rw-r--r--src/walu/arp.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/walu/arp.py b/src/walu/arp.py
new file mode 100644
index 0000000..82f8445
--- /dev/null
+++ b/src/walu/arp.py
@@ -0,0 +1,68 @@
+#!/usr/bin/env python3
+
+import names
+
+class arp:
+ table = {}
+
+ def __init__(self):
+ self.refresh()
+
+ @staticmethod
+ def flag_to_status(flag):
+ flagmap = {
+ "0x0" : "offline",
+ "0x2" : "online",
+ "0x6" : "manual",
+ }
+
+ try:
+ status = flagmap[flag]
+ except:
+ return "chilling with penguin"
+ else:
+ return status
+
+ def refresh(self):
+ self.table = {}
+
+ with open("/proc/net/arp") as f:
+ f.readline() # skip table header
+
+
+ for line in f:
+ row = line.split()
+
+ ip = row[0]
+ flag = row[2]
+ mac = row[3]
+ dev = row[5]
+
+ name = names.iptoname(ip)
+ if not name:
+ name = names.mactoname(mac)
+ if not name:
+ name = "Anon"
+
+ self.table[ip] = {
+ "name" : name,
+ "mac" : mac,
+ "dev" : dev,
+ "status" : self.flag_to_status(flag),
+ }
+
+ def ip_to_status(self, ip):
+ try:
+ status = self.table[ip]
+ except:
+ pass
+ else:
+ return status
+
+ def ip_to_mac(self, ip):
+ try:
+ tab = self.table[ip]
+ except:
+ pass
+ else:
+ return tab["mac"]