# Config filtering
from lxml.builder import E
get = E('configuration', E('protocols', E('bgp', E('group'))))
configXML = dev.rpc.get_config(get)
from jnpr.junos import Devicefrom pprint import pprintimport getpassimport xmldictfrom lxml.builder import E
from multiprocessing.dummy import Pool as ThreadPoolfrom multiprocessing import cpu_count
neighbors = []u = ""p = ""
# gets routers from H route reflectordef getRouters(u, p): dev = Device(host='<route reflector H ip>', user=u, password=p) dev.open(gather_facts=False) configXML = dev.rpc.get_configuration() dev.close()
configDic = xmldict.xml_to_dict(configXML)
ips = []
for i in configDic['configuration']['protocols']['bgp']['group']: if i['name'] == 'CEN-IBGP-RS-CLIENTS': for z in i['neighbor']: ips.append(z['name'])
print 'Got', len(ips), 'IPs' return ips
def getHosts(i): global u global p global neighbors dev = Device(host=i, user=u, password=p) if dev.probe(): print 'Can reach', i, 'resolving hostname' try: dev.open(gather_facts=False) configXML = dev.rpc.get_configuration() dev.close() hname = configXML.xpath('.//system/host-name')[0].text hname = hname.split('.')[1] neighbors.append({'ip': i, 'name': hname}) except: pass else: print 'Cant reach', i return
if __name__ == '__main__': global u global p global neighbors u = raw_input("User: ") p = getpass.getpass()
ips = getRouters(u, p) # 2 threads per core - can probably set this upwards of 16 pool = ThreadPool(cpu_count() * 2) pool.map_async(getHosts, ips) pool.close() pool.join() pprint(neighbors)#!/usr/bin/python
import sys
from pprint import pprint
import getpass
from lxml.builder import E
from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import cpu_count
from jnpr.junos import Device
from jnpr.junos.exception import ConnectError, RpcError
class test():
def __init__(self):
self.u = raw_input("User: ")
self.p = getpass.getpass()
# gets routers from H route reflector
def getRouters(self):
dev = Device(host='192.168.74.31', user=self.u, password=self.p)
print "Connecting to route reflector"
try:
dev.open(gather_facts=False)
get = E('configuration', E('protocols', E('bgp', E('group', E('name', 'CEN-IBGP-RS-CLIENTS')))))
conf = dev.rpc.get_config(get)
dev.close()
except ConnectError as err:
print "Unable to connect to route reflector!"
print err
sys.exit()
except RpcError as err:
print "Unable to get neighbors!"
print err
dev.close()
sys.exit()
neighbors = conf.findall('.//neighbor/name')
ips = []
for i in neighbors:
ips.append(i.text)
print 'Got {0} IPs'.format(len(ips))
return ips
def getHosts(self, i):
dev = Device(host=i, user=self.u, password=self.p)
if dev.probe():
print 'Can reach {0} - resolving hostname'.format(i)
try:
dev.open(gather_facts=False)
get = E('configuration', E('system', E('host-name')))
configXML = dev.rpc.get_config(get)
dev.close()
hname = configXML.findtext('.//host-name')
hname = hname.split('.')[1]
return {'ip': i, 'name': hname}
except:
pass
else:
print 'Cant reach {0}'.format(i)
return
if __name__ == '__main__':
main = test()
ips = main.getRouters()
# 2 threads per core - can probably set this upwards of 16
pool = ThreadPool(cpu_count() * 2)
results = pool.map_async(main.getHosts, ips)
pool.close()
pool.join()
neighbors = filter(None, results.get())
pprint(neighbors)
#!/usr/bin/pythonimport sysfrom pprint import pprintimport getpassfrom lxml.builder import Efrom multiprocessing.dummy import Pool as ThreadPoolfrom multiprocessing import cpu_countfrom jnpr.junos import Devicefrom jnpr.junos.exception import ConnectError, RpcError
class multiGetRouters():def __init__(self,u,p):self.usr = uself.pw = pself.ips = Noneself.results = []self.getRouters()self.multiGetHosts()
# gets routers from H route reflectordef getRouters(self):
dev = Device(host='<Route Reflector H IP>', user=self.usr, password=self.pw)
print "Connecting to route reflector"try:dev.open(gather_facts=False)get = E('configuration', E('protocols', E('bgp', E('group', E('name', 'CEN-IBGP-RS-CLIENTS')))))conf = dev.rpc.get_config(get)dev.close()except ConnectError as err:print "Unable to connect to route reflector!"print errsys.exit()except RpcError as err:print "Unable to get neighbors!"print errdev.close()sys.exit()neighbors = conf.findall('.//neighbor/name')ips = []for i in neighbors:ips.append(i.text)print 'Got {0} IPs'.format(len(ips))
self.ips = ipsdef getHosts(self, i):dev = Device(host=i, user=self.usr, password=self.pw)
if dev.probe():print 'Can reach {0} - resolving hostname'.format(i)try:dev.open(gather_facts=False)
configXML = dev.rpc.get_configuration()dev.close()hname = configXML.xpath('.//system/host-name')[0].texthname = hname.split('.')[1]
self.results.append( {'ip': i, 'name': hname, 'xml':configXML} )except:print 'Couldnt connect to',idef multiGetHosts(self):# 6 threads per core - dont set more than 6 for ~50% CPUpool = ThreadPool(cpu_count() * 6)results = pool.map_async(self.getHosts, self.ips)pool.close()pool.join()if __name__ == '__main__':
u = raw_input("User: ")p = getpass.getpass()
main = multiGetRouters(u,p)pprint(main.results)
...