Ejemplo n.º 1
0
 def parse (self) :
     root = self.tree.getroot ()
     self.if_by_name = {}
     self.ips        = {}
     for div in root.findall (".//%s" % tag ("div")) :
         self.try_get_version (div)
         if div.get ('id') == 'maincontent' and not self.if_by_name :
             tbl = div.find (".//%s" % tag ("table"))
             for n, tr in enumerate (tbl) :
                 if tr [0].tag == tag ('th') :
                     assert tr [0].text in ('Interface', 'Schnittstelle') \
                         , tr [0].text
                     continue
                 name, status, mtu, wlan, ip, mask, bcast = \
                     (x.text for x in tr)
                 if name in self.if_by_name :
                     iface = self.if_by_name [name]
                 else :
                     iface = Interface (n, name, mtu)
                     iface.is_wlan = self.yesno.get (wlan, False)
                 if status == 'DOWN' :
                     continue
                 # append IP address to interface if there is one
                 if ip is not None :
                     if ':' in ip :
                         i6 = Inet6 (ip, mask, bcast, iface = name)
                         iface.append_inet6 (i6)
                     else :
                         i4 = Inet4 (ip, mask, bcast, iface = name)
                         iface.append_inet4 (i4)
                         if not unroutable (i4.ip) :
                             self.if_by_name [name] = iface
                             self.ips [i4] = True
     self.set_version (root)
     if not self.if_by_name :
         raise ValueError, "No interface config found"
     bfw = Backfire_WLAN_Config (site = self.site)
     for d in bfw.wlans :
         if d.name in self.if_by_name :
             iface = self.if_by_name [d.name]
             iface.wlan_info = d
Ejemplo n.º 2
0
 def parse (self) :
     root = self.tree.getroot ()
     self.if_by_name = {}
     self.ips        = {}
     for div in root.findall (".//%s" % tag ("div")) :
         self.try_get_version (div)
         if div.get ('id') == 'maincontent' and not self.if_by_name :
             tbl = div.find (".//%s" % tag ("table"))
             for n, tr in enumerate (tbl) :
                 if tr [0].tag == tag ('th') :
                     assert tr [0].text in ('Interface', 'Schnittstelle') \
                         , tr [0].text
                     continue
                 name, status, mtu, wlan, ip, mask, bcast = \
                     (x.text for x in tr)
                 if name in self.if_by_name :
                     iface = self.if_by_name [name]
                 else :
                     iface = Interface (n, name, mtu)
                     iface.is_wlan = self.yesno.get (wlan, False)
                 if status == 'DOWN' :
                     continue
                 # append IP address to interface if there is one
                 if ip is not None :
                     if ':' in ip :
                         i6 = Inet6 (ip, mask, bcast, iface = name)
                         iface.append_inet6 (i6)
                     else :
                         i4 = Inet4 (ip, mask, bcast, iface = name)
                         iface.append_inet4 (i4)
                         if not unroutable (i4.ip) :
                             self.if_by_name [name] = iface
                             self.ips [i4] = True
     self.set_version (root)
     if not self.if_by_name :
         raise ValueError, "No interface config found"
     bfw = Backfire_WLAN_Config (site = self.site)
     for d in bfw.wlans :
         if d.name in self.if_by_name :
             iface = self.if_by_name [d.name]
             iface.wlan_info = d
Ejemplo n.º 3
0
class Interface_Config(Parser):
    re_assign = re.compile(r'^([a-zA-Z0-9_]+)\s*=\s*([a-zA-Z0-9 ]*)$')
    re_brhead = re.compile(r'^bridge name.*$')
    re_bridge = re.compile(r'^(\S+)\s*(\S+)\s*(\S+)\s*(\S+)$')
    re_ifonly = re.compile(r'^\s+(\S+)$')
    re_if     = re.compile \
        (r'^(\d+):\s*(\S+):\s*mtu\s*(\d+)\s*qdisc\s*(\S+)(?:qlen\s*(\d+))?')
    pt_mac = pt_mac
    re_link   = re.compile \
        (r'^\s*link/(\S+)(?:\s*(\S+)\s*brd\s*(\S+))?$' % locals ())
    pt_ip = r'((?:\d{1,3}[.]){3}\d{1,3})'
    re_inet   = re.compile \
        ( r'^\s*inet\s+%(pt_ip)s/(\d+)\s+'
          r'(?:brd\s+%(pt_ip)s\s+)?scope\s+(\S+)\s+(\S+)$'
        % locals ()
        )
    pt_ip6 = r'([0-9a-fA-F:]+)'
    re_inet6  = re.compile \
        ( r'^\s*inet6\s+%(pt_ip6)s/(\d+)\s+scope\s+(\S+)$' % locals ())
    matrix = \
        [ ["init",   re_assign, "init",   "assign"]
        , ["init",   re_brhead, "bridge", None]
        , ["init",   re_if,     "iface",  "iface"]
        , ["init",   None,      "init",   None]
        , ["bridge", re_bridge, "bridge", "bridge"]
        , ["bridge", re_ifonly, "bridge", None]
        , ["bridge", '',        "init",   None]
        , ["iface",  re_link,   "iface",  "link"]
        , ["iface",  re_inet,   "iface",  "inet"]
        , ["iface",  re_inet6,  "iface",  "inet6"]
        , ["iface",  None,      "init",   "pop"]
        ]

    def __init__(self, **kw):
        self.assignments = {}
        self.bridges = []
        self.interfaces = []
        self.if_by_name = {}
        self.__super.__init__(**kw)

    # end def __init__

    def assign(self, state, new_state, match):
        self.assignments[match.group(1)] = match.group(2)

    # end def assign

    def bridge(self, state, new_state, match):
        self.bridges.append(match.groups())

    # end def bridge

    def iface(self, state, new_state, match):
        self.interface = Interface(*match.groups())
        self.interfaces.append(self.interface)
        self.if_by_name[self.interface.name] = self.interface
        self.push(state, new_state, match)

    # end def iface

    def link(self, state, new_state, match):
        self.interface.link = Net_Link(*match.groups())

    # end def link

    def inet(self, state, new_state, match):
        self.interface.append_inet4(Inet4(*match.groups()))

    # end def inet

    def inet6(self, state, new_state, match):
        self.interface.append_inet6(Inet6(*match.groups()))

    # end def inet6

    def __str__(self):
        r = []
        for k, v in self.assignments.iteritems():
            r.append("%(k)s = %(v)s" % locals())
        for b in self.bridges:
            r.append("Bridge: %s" % str(b))
        for i in self.interfaces:
            r.append(str(i))
        return '\n'.join(r)

    # end def __str__
    __repr__ = __str__
Ejemplo n.º 4
0
Archivo: freifunk.py Proyecto: FFM/FFM
class Interface_Config (Parser) :
    re_assign = re.compile (r'^([a-zA-Z0-9_]+)\s*=\s*([a-zA-Z0-9 ]*)$')
    re_brhead = re.compile (r'^bridge name.*$')
    re_bridge = re.compile (r'^(\S+)\s*(\S+)\s*(\S+)\s*(\S+)$')
    re_ifonly = re.compile (r'^\s+(\S+)$')
    re_if     = re.compile \
        (r'^(\d+):\s*(\S+):\s*mtu\s*(\d+)\s*qdisc\s*(\S+)(?:qlen\s*(\d+))?')
    pt_mac    = pt_mac
    re_link   = re.compile \
        (r'^\s*link/(\S+)(?:\s*(\S+)\s*brd\s*(\S+))?$' % locals ())
    pt_ip     = r'((?:\d{1,3}[.]){3}\d{1,3})'
    re_inet   = re.compile \
        ( r'^\s*inet\s+%(pt_ip)s/(\d+)\s+'
          r'(?:brd\s+%(pt_ip)s\s+)?scope\s+(\S+)\s+(\S+)$'
        % locals ()
        )
    pt_ip6    = r'([0-9a-fA-F:]+)'
    re_inet6  = re.compile \
        ( r'^\s*inet6\s+%(pt_ip6)s/(\d+)\s+scope\s+(\S+)$' % locals ())
    matrix = \
        [ ["init",   re_assign, "init",   "assign"]
        , ["init",   re_brhead, "bridge", None]
        , ["init",   re_if,     "iface",  "iface"]
        , ["init",   None,      "init",   None]
        , ["bridge", re_bridge, "bridge", "bridge"]
        , ["bridge", re_ifonly, "bridge", None]
        , ["bridge", '',        "init",   None]
        , ["iface",  re_link,   "iface",  "link"]
        , ["iface",  re_inet,   "iface",  "inet"]
        , ["iface",  re_inet6,  "iface",  "inet6"]
        , ["iface",  None,      "init",   "pop"]
        ]

    def __init__ (self, **kw) :
        self.assignments = {}
        self.bridges     = []
        self.interfaces  = []
        self.if_by_name  = {}
        self.__super.__init__ (**kw)
    # end def __init__

    def assign (self, state, new_state, match) :
        self.assignments [match.group (1)] = match.group (2)
    # end def assign

    def bridge (self, state, new_state, match) :
        self.bridges.append (match.groups ())
    # end def bridge

    def iface (self, state, new_state, match) :
        self.interface = Interface (* match.groups ())
        self.interfaces.append (self.interface)
        self.if_by_name [self.interface.name] = self.interface
        self.push (state, new_state, match)
    # end def iface

    def link (self, state, new_state, match) :
        self.interface.link = Net_Link (* match.groups ())
    # end def link

    def inet (self, state, new_state, match) :
        self.interface.append_inet4 (Inet4 (* match.groups ()))
    # end def inet

    def inet6 (self, state, new_state, match) :
        self.interface.append_inet6 (Inet6 (* match.groups ()))
    # end def inet6

    def __str__ (self) :
        r = []
        for k, v in self.assignments.iteritems () :
            r.append ("%(k)s = %(v)s" % locals ())
        for b in self.bridges :
            r.append ("Bridge: %s" % str (b))
        for i in self.interfaces :
            r.append (str (i))
        return '\n'.join (r)
    # end def __str__
    __repr__ = __str__