def add_dhcp_info(filelist): regexp = re.compile('(\S+) +(\S+) +\d+ +\S+ +(\d+) +(\S+)') result = [] conn = connect_db("dhcp_snooping.db") for files in filelist: device = re.search('(\S+?)_\S+', os.path.basename(files)).group(1) with open(files) as f: for line in f: match = regexp.search(line) if match: resstring = list(match.group(1, 2, 3, 4)) resstring.append(device) result.append(tuple(resstring)) query = f"update dhcp set active = ? where switch like '{device}'" write_to_db(conn, query, '0') insert = 'insert into dhcp values (?, ?, ?, ?, ?, 1, datetime("now"))' update = "update dhcp set ip = ?, vlan = ?, interface = ?, switch = ?, active = 1, last_active = datetime('now') where mac = ?" for line in result: if not write_to_db(conn, insert, line): print(f"string added to the dhcp table: {line}") else: tmp = list(line)[1::] tmp.append(line[0]) line = tuple(tmp) print( f"updating values for mac {line[-1]}. New values: {line[0:-1:]}" ) write_to_db(conn, update, line) conn.commit()
def add_switches_info(source): with open(source) as f: switch_dict = yaml.safe_load(f) query = "INSERT into switches values (?, ?)" result = [] for key in switch_dict: for items in switch_dict[key].items(): result.append(items) print(result) print('adding data to switches table') write_to_db('dhcp_snooping.db', query, result)
def add_dhcp_info(filelist): regexp = re.compile('(\S+) +(\S+) +\d+ +\S+ +(\d+) +(\S+)') result = [] for files in filelist: device = re.search('(\S+?)_\S+', files).group(1) with open(files) as f: for line in f: match = regexp.search(line) if match: resstring = list(match.group(1, 2, 3, 4)) resstring.append(device) result.append(tuple(resstring)) query = 'insert into dhcp values (?, ?, ?, ?, ?)' print('adding data to dhcp table') write_to_db('dhcp_snooping.db', query, result)
def add_switches_info(source): with open(source) as f: switch_dict = yaml.safe_load(f) query = "INSERT into switches values (?, ?)" result = [] for key in switch_dict: for items in switch_dict[key].items(): result.append(items) conn = connect_db("dhcp_snooping.db") for line in result: if not write_to_db(conn, query, line): print(f'line added to switches table "{line[0]} {line[1]}"') else: print(f"{line[0]} info already present in the table") conn.commit()