get_dhcp_binding.py 1.36 KB
Newer Older
Dmitry Volodin's avatar
Dmitry Volodin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding: utf-8 -*-
##----------------------------------------------------------------------
## Cisco.IOS.get_dhcp_binding
##----------------------------------------------------------------------
## Copyright (C) 2007-2009 The NOC Project
## See LICENSE for details
##----------------------------------------------------------------------
"""
"""
import noc.sa.script
from noc.sa.interfaces import IGetDHCPBinding
import re,datetime

rx_line=re.compile(r"^(?P<ip>\d+\.\d+\.\d+\.\d+)\s+(?P<mac>\S+)\s+(?P<expire>.+?)\s+(?P<type>Automatic|Manual)$",re.IGNORECASE)

class Script(noc.sa.script.Script):
    name="Cisco.IOS.get_dhcp_binding"
    implements=[IGetDHCPBinding]
    def execute(self):
        self.cli("terminal length 0")
        vlans=self.cli("show ip dhcp binding")
        r=[]
        for l in vlans.split("\n"):
            match=rx_line.match(l.strip().lower())
            if match:
                d=match.group("expire")
                if d=="infinite":
                    expire=d
                else:
                    expire=datetime.datetime.strptime(d,"%b %d %Y %I:%M %p")
                r.append({
                    "ip"         : match.group("ip"),
                    "mac"        : match.group("mac"),
                    "expiration" : expire,
                    "type"       : match.group("type")[0].upper(),
                })
        return r