summaryrefslogblamecommitdiff
path: root/src/walu/arp.py
blob: 82f8445edc47a9c46e6e5352da525b77459681b3 (plain) (tree)



































































                                                         
#!/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"]