diff options
Diffstat (limited to 'extras/fap/dhcpd/module_lease.py')
-rwxr-xr-x | extras/fap/dhcpd/module_lease.py | 78 |
1 files changed, 12 insertions, 66 deletions
diff --git a/extras/fap/dhcpd/module_lease.py b/extras/fap/dhcpd/module_lease.py index 0473579..a33ab61 100755 --- a/extras/fap/dhcpd/module_lease.py +++ b/extras/fap/dhcpd/module_lease.py @@ -19,10 +19,10 @@ import psycopg2.extras # settings settings = dict( db = dict( - user = 'fap', - password = '<sensored>', - dbname = 'fap', - host = 'localhost' + user = '<user>', + password = '<password>', + dbname = '<db>', + host = '<host>' ) ) @@ -31,66 +31,12 @@ connect_params = ("dbname='%s' user='%s' host='%s' password='%s'" % (settings['d conn = psycopg2.connect(connect_params) cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) -class lease(object): - debug = False - - def __init__(self, identifiers): - if len(identifiers) > 0: # 1 or more identifiers - we're good to go - - # build query string - where_pieces = [] - for x in identifiers.items(): - where_pieces.append(str(x[0]) + " = '" + str(x[1]) + "'") - where = ' AND '.join(where_pieces) - select = "SELECT * FROM switches WHERE " + where + " LIMIT 1" - - if self.debug is True: - print('Executing query: ' + select) - - cur.execute(select) - - rows = cur.fetchall() - if len(rows) is 1: - if self.debug is True: - print('returned from DB:') - for key, value in rows[0].items(): - print('%s: %s' % (key, value)) - - self.row = rows[0] - else: - self.row = False - else: - print('Missing identifier parameter') - exit() - - def get_ip(self): - if self.row is not False: - return self.row['ip'] - else: - print('identifiers (%s) not found' % self.row) - return False - - def get_config(self): - if self.row is not False: - return self.row['config'] - else: - print('identifiers (%s) not found' % self.row) - return False - - def get_dict(self): - if self.row is not False: - return self.row - else: - print('identifiers (%s) not found' % self.row) - return False - - # # TESTING - Bruker ID fra DB-en som identifier, og kjører en query per lease.get_x() # class lease2(object): debug = False - hostname = False + sysname = False identifiers = False # identifiers = dict of field/values @@ -105,7 +51,7 @@ class lease2(object): for identifier in identifiers.items(): where_pieces.append(str(identifier[0]) + " = '" + str(identifier[1]) + "'") where = ' AND '.join(where_pieces) - select = "SELECT hostname FROM switches WHERE " + where + " LIMIT 1" + select = "SELECT sysname FROM switches WHERE " + where + " LIMIT 1" if self.debug is True: print('Executing query: ' + select) @@ -117,19 +63,19 @@ class lease2(object): if self.debug is True: print('returned from DB:') print(rows[0][0]) - self.hostname = rows[0][0] + self.sysname = rows[0][0] else: - self.hostname = False + self.sysname = False else: print('Missing identifier parameter') exit() # Used to fetch fields from DB def get(self, field): - if self.hostname is not False: + if self.sysname is not False: cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) - query = "SELECT %s FROM switches WHERE hostname = '%s' LIMIT 1" % (field, self.hostname) + query = "SELECT %s FROM switches WHERE sysname = '%s' LIMIT 1" % (field, self.sysname) if self.debug is True: print('Query: %s' % query) @@ -156,9 +102,9 @@ class lease2(object): # Used to set fields in DB def set(self, field, value): - if self.hostname is not False: + if self.sysname is not False: cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) - query = "UPDATE switches SET %s = '%s' WHERE hostname = '%s'" % (field, value, self.hostname) + query = "UPDATE switches SET %s = '%s' WHERE sysname = '%s'" % (field, value, self.sysname) if self.debug is True: print('Query: %s' % query) try: |