65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
#!/usr/bin/python3
|
|
|
|
import json
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
|
|
try:
|
|
ip = sys.argv[1]
|
|
community = sys.argv[2]
|
|
porta = sys.argv[3]
|
|
except:
|
|
print('\n')
|
|
print(' Dicovery BGP Peer Huawei')
|
|
print(' by IPv0')
|
|
print(' ---')
|
|
print(' Use: python3 discovery_bgp_peers_hw.py <ip> <community> <porta>')
|
|
print('\n')
|
|
exit(1)
|
|
|
|
# Criando saida do walk com os asns
|
|
saida_asns = subprocess.check_output(f'snmpwalk -On -v2c -c {community} {ip}:{porta} .1.3.6.1.4.1.2011.5.25.177.1.1.2.1.2.0', shell=True).decode()
|
|
|
|
# Criando saida do walk com os peers
|
|
saida_peers = subprocess.check_output(f'snmpwalk -On -v2c -c {community} {ip}:{porta} .1.3.6.1.4.1.2011.5.25.177.1.1.2.1.4.0', shell=True).decode()
|
|
|
|
# Criando lista de ASN e index
|
|
lista_asns_index = re.findall(r'\.1\.3\.6\.1\.4\.1\.2011\.5\.25\.177\.1\.1\.2\.1\.2\.0\.([0-9\.]+)\s=\sGauge32:\s([0-9]+)', saida_asns)
|
|
|
|
# Criando lista de ASN
|
|
lista_asns = []
|
|
for asn in lista_asns_index:
|
|
if asn:
|
|
lista_asns.append(asn[1])
|
|
|
|
lista_asnames = []
|
|
|
|
# Criando lista com o nome dos ASNs
|
|
for asn in lista_asns:
|
|
var = subprocess.check_output(f'whois -h whois.cymru.com "AS{asn}" | grep -v "AS Name" | awk -F", " "'"{print $2}"'"', shell=True).decode()
|
|
lista_asnames.append(var.strip())
|
|
|
|
# Criando listas de peers e index
|
|
lista_peers_index = re.sub(r'\"', '', saida_peers)
|
|
lista_peers_index = re.findall(r'\.1\.3\.6\.1\.4\.1\.2011\.5\.25\.177\.1\.1\.2\.1\.4\.0\.([0-9\.]+)\s=\sSTRING:\s([0-9A-Za-z\.\:]+)', lista_peers_index)
|
|
|
|
# Justando listas
|
|
lista_zip = zip(lista_asns_index, lista_peers_index, lista_asnames)
|
|
|
|
lista_peers_bgp = []
|
|
|
|
for i in lista_zip:
|
|
if i[0][0] == i[1][0]:
|
|
dic = {
|
|
"{#SNMPINDEX}": i[0][0],
|
|
"{#BGPPEER}": i[1][1],
|
|
"{#ASNUM}": i[0][1],
|
|
"{#PEERNAME}": i[2]
|
|
}
|
|
|
|
lista_peers_bgp.append(dic)
|
|
|
|
saida_json = json.dumps(lista_peers_bgp, indent=2)
|
|
print(saida_json)
|