def main(): logger = get_module_logger(__name__, 'DEBUG') devices = {'juniper.lab': '10.3.3.3'} logger.info('Device to connect : %s' % (devices)) for name in devices: try: logger.info('Connecting to : %s' % devices[name]) dev = junos.Device(host=devices[name], user='******', password='******', port='830', gather_facts=False) dev.open() dev.timeout = 600 logger.info('Requesting data from device: %s' % devices[name]) ospf_table = TedTable(dev).get() logger.info('Data %s received from device: %s' % (ospf_table, devices)) dev.close() except Exception as e: logger.exception('Got exception while trying to connect') if len(ospf_table) >= 1: ospf_db = [] i = 0 logger.info('Iterate over data %s received from device: %s' % (ospf_table, devices)) for entry in ospf_table: source = str(entry).split(':')[1] for n in entry.link: a = (source, n.remoteRTR, n.metric, n.localADD, n.remoteADD) ospf_db.insert(i, (a)) logger.info('Create panda') labels = ['source', 'target', 'metric', 'l_ip', 'r_ip'] df = pd.DataFrame.from_records(ospf_db, columns=labels) logger.info('Generate l_ip r_ip pair') df.loc[:, 'l_ip_r_ip'] = pd.Series([ tuple(sorted(each)) for each in list( zip(df.l_ip.values.tolist(), df.r_ip.values.tolist())) ]) logger.info('Set l_ip r_ip pair as string') df['l_ip_r_ip'] = df['l_ip_r_ip'].astype(str) logger.info('Get source and target names') df['source'] = df.apply(lambda row: get_hostname(row['source']), axis=1) df['target'] = df.apply(lambda row: get_hostname(row['target']), axis=1) logger.info('Fill NA values with 0') try: logger.info('Write to database') disk_engine = create_engine( 'sqlite:////opt/lnetd/web_app/database.db') df.to_sql('rpc_links', disk_engine, if_exists='replace') logger.info('All done') logger.debug('here is the resulting info :\n %s' % (df)) except Exception: logger.exception('Got error writing to sqlite3 db') else: logger.error("No data received from device")
def main(): logger = get_module_logger(__name__, 'DEBUG') devices = {'juniper.lab': '10.3.3.3'} logger.info('Device to connect : %s' % (devices)) for name in devices: try: logger.info('Connecting to : %s' % devices[name]) dev = junos.Device(host=devices[name], user='******', password='******', port='830', gather_facts=False) dev.open() dev.timeout = 600 logger.info('Requesting data from device: %s' % devices[name]) isis_table = isisTable(dev).get() logger.info('Data %s received from device: %s' % (isis_table, devices)) dev.close() except Exception as e: logging.exception('Got exception while trying to connect') if len(isis_table) >= 1: isis_db = [] i = 0 logger.info('Iterate over data %s received from device: %s' % (isis_table, devices)) for isis in isis_table: for entry in isis.levelTable: for entry1 in entry.remoteTable: for entry2 in entry1.reachability: a = (entry.lsp_id[:-6], entry2.remoteRTR[:-3], entry2.metric, entry2.local_ip, entry2.remote_ip) isis_db.insert(i, (a)) logger.info('Create panda') labels = ['source', 'target', 'metric', 'l_ip', 'r_ip'] df = pd.DataFrame.from_records(isis_db, columns=labels) logger.info('Generate l_ip r_ip pair') df.loc[:, 'l_ip_r_ip'] = pd.Series([ tuple(sorted(each)) for each in list( zip(df.l_ip.values.tolist(), df.r_ip.values.tolist())) ]) logger.info('Set l_ip r_ip pair as string') df['l_ip_r_ip'] = df['l_ip_r_ip'].astype(str) logger.info('Fill NA values with 0') df = df.fillna(0) try: logger.info('Write to database') disk_engine = create_engine( 'sqlite:////opt/lnetd/web_app/database.db') df.to_sql('rpc_links', disk_engine, if_exists='replace') logger.info('All done') logger.debug('here is the resulting info :\n %s' % (df)) except Exception: logging.exception('Got error writing to sqlite3 db') else: logger.error("No data received from device")
def main(): logger = get_module_logger(__name__, 'DEBUG') devices = {'juniper.lab': '10.3.3.3'} logger.info('Device to connect : %s' % (devices)) for name in devices: try: logger.info('Connecting to : %s' % devices[name]) dev = junos.Device(host=devices[name], user='******', password='******', port='830', gather_facts=False) dev.open() dev.timeout = 600 logger.info('Requesting data from device: %s' % devices[name]) isis_table = isisTable(dev).get() logger.info('Data %s received from device: %s' % (isis_table, devices)) dev.close() except Exception as e: logging.exception('Got exception while trying to connect') if len(isis_table) >= 1: isis_db = [] i = 0 logger.info('Iterate over data %s received from device: %s' % (isis_table, devices)) for isis in isis_table: for level in isis.levelTable: for tlv in level.tlvTable: a = (level.lsp_id[:-6], tlv.address) isis_db.insert(i, (a)) logger.info('Create panda') labels = ['name', 'ip'] df = pd.DataFrame.from_records(isis_db, columns=labels) # create country entry from first 2 letters of hostname df.loc[:, 'country'] = df['name'].str[0:2] df2 = df.fillna(0) try: logger.info('Write to database') # write to sql db disk_engine = create_engine( 'sqlite:////opt/lnetd/web_app/database.db') df2.to_sql('rpc_prefixes', disk_engine, if_exists='replace') logger.debug('all done - this is the final panda : \n %s' % df2) except Exception: logging.exception('Got error writing to sqlite3 db') else: logger.error("No data received from device")
def main(): logger = get_module_logger(__name__,'DEBUG') devices = {'juniper.lab': '10.3.3.3'} logger.info('Device to connect : %s' %(devices)) for name in devices: try: logger.info('Connecting to : %s' %devices[name]) dev = junos.Device(host=devices[name], user='******', password='******',port='830', gather_facts=False) dev.open() dev.timeout = 600 logger.info('Requesting data from device: %s' %devices[name]) isis_table = isisTable(dev).get() logger.info('Data %s received from device: %s' %(isis_table,devices)) dev.close() except Exception,e: logging.exception('Got exception while trying to connect')
from jnpr import junos from jnpr.junos.exception import ConnectAuthError, ConnectRefusedError, ConnectTimeoutError from isis import isisTable import pandas as pd devices = {'juniper.lab': '192.168.1.6'} for name in devices: print '---------------------------------------------------------------------------' print devices[name] print '---------------------------------------------------------------------------' try: dev = junos.Device(host=devices[name], user='******', password='******', gather_facts=False) dev.open() isis_table = isisTable(dev).get() dev.close() except ConnectAuthError: print 'ConnectAuthError' continue except ConnectRefusedError: print 'ConnectRefusedError' continue except ConnectTimeoutError: print 'ConnectTimeoutError' continue isis_db =[] i = 0 print isis_table for isis in isis_table: