May-10-2019, 10:00 AM
Hi All,
I am fetching all devices from SQL server, checking there reachability, if devices are not in specific IPNetwork, writing to file. again opening file to check model against each device.
netaddr package takes IP are str and easysnmp IP as bytes. required help to resolve error.
I am fetching all devices from SQL server, checking there reachability, if devices are not in specific IPNetwork, writing to file. again opening file to check model against each device.
netaddr package takes IP are str and easysnmp IP as bytes. required help to resolve error.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
import threading import MySQLdb import subprocess import sys from netaddr import IPNetwork, IPAddress from easysnmp import Session dslamsip = open ( 'dslamip.txt' , 'w' ) rechable_dslamsip = open ( 'rechable_dslamsip.txt' , 'w' ) #dslams = [] #rechable_dslams = [] switches_network = [ '10.104.75.0/24' , '10.17.100.0/24' , '10.16.100.0/24' , '10.14.100.0/24' ] def dslam_rechability_check(ip): ping_reply = subprocess.call([ 'ping' , '-c' , '2' , '-w' , '2' , '-q' , '-n' , ip],stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL) if ping_reply = = 0 : rechable_dslamsip.write(ip) rechable_dslamsip.write( '\n' ) ################### def dslam_model(ip): community = 'public' #sysDescr OID oid = 'SNMPv2-MIB::sysDescr.0' try : session = Session(hostname = ip, community = community, version = 2 ) snmp_get = session.get(oid) result = snmp_get.value.encode( 'ascii' ) if 'IES1248-51' in result: model = 'ZyXEL' elif 'ECI' in result: model = 'ECI' elif 'VES1724-56' in result: model = 'ZyXEL VDSL' else : model = 'UT' print ( '{} {}' . format (ip, model)) except Exception as excp: print (ip,excp) def db_connection(): global dslams db = MySQLdb.connect( "localhost" , "abc" , "xyz" , "xxxx" ) # prepare a cursor object using cursor() method cursor = db.cursor() sql = "SELECT * FROM ttml_ttsl_host_details \ WHERE access_group = '%s' " % ( 'TTML' ) try : # Execute the SQL command cursor.execute(sql) # Fetch all the rows in a list of lists. results = cursor.fetchall() for row in results: ip_addr = row[ 6 ] dslamsip.write(ip_addr) dslamsip.write( '\n' ) except : print ( "Error: unable to fecth data" ) # disconnect from server db.close() ### def rechability_create_threads(): threads = [] with open ( 'dslamip.txt' ) as ipfile: for sr_no, line in enumerate (ipfile, start = 1 ): host = line.strip() th = threading.Thread(target = dslam_rechability_check ,args = (host,)) th.start() threads.append(th) for thr in threads: thr.join() def dslam_model_threads(): model_threads = [] with open ( 'rechable_dslamsip.txt' ) as rechable_ipfile: for sr_no, line in enumerate (rechable_ipfile, start = 1 ): host = line.strip() #print(host) model_th = threading.Thread(target = dslam_model ,args = (host,)) model_th.start() model_threads.append(model_th) for model_thr in model_threads: model_thr.join() if __name__ = = "__main__" : db_connection() rechability_create_threads() dslam_model_threads() rechable_dslamsip.close() dslamsip.close() |
Error:10.111.3.254 a bytes-like object is required, not 'str'
10.211.128.53 a bytes-like object is required, not 'str'
10.111.3.172 a bytes-like object is required, not 'str'
10.112.1.171 a bytes-like object is required, not 'str'
10.112.3.76 a bytes-like object is required, not 'str'
10.112.3.56 a bytes-like object is required, not 'str'
10.111.1.113 a bytes-like object is required, not 'str'
10.11.2.17 a bytes-like object is required, not 'str'
10.112.1.130 a bytes-like object is required, not 'str'
10.111.1.162 a bytes-like object is required, not 'str'
10.111.3.174 a bytes-like object is required, not 'str'