summaryrefslogblamecommitdiff
path: root/src/walu/arp.py
blob: 5c28a5a7db110e1c06ad2b19d1c1d1f375a48d62 (plain) (tree)
1
2
3

                      
                      
































































                                                         
#!/usr/bin/env python3

from walu 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"]