Created
November 2, 2017 19:14
-
-
Save XioNoX/26d7955b9bae56b6a6b94b2192283021 to your computer and use it in GitHub Desktop.
Send notifications to BGP peers using our old AS number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from napalm import get_network_driver | |
from pprint import pprint | |
from peeringdb import PeeringDB | |
import twentyc | |
import smtplib | |
driver = get_network_driver('junos') | |
device = driver('router-hostname', 'username', '') # No password, doing pubkey auth | |
device.open() | |
bgp_neighs = device.get_bgp_neighbors() | |
peers_as_list = [] | |
for peer,attributes in bgp_neighs['global']['peers'].iteritems(): | |
if attributes['local_as'] == 999999: # remplace with AS# | |
if not attributes['remote_as'] in peers_as_list: | |
peers_as_list.append(attributes['remote_as']) | |
#pprint(peers_as_list) | |
pdb = PeeringDB() | |
net = pdb.type_wrap('net') | |
poc = pdb.type_wrap('poc') | |
entity_list = [] | |
for asn in peers_as_list: | |
emails_to_contact = [] | |
found_contact = False | |
try: | |
network_id = net.all(asn=asn)[0]['id'] | |
network_name = net.all(asn=asn)[0]['name'] | |
POCs = poc.all(net_id=network_id) | |
for contact in POCs: | |
if 'noc' in contact['email'] or 'peering' in contact['email']: | |
found_contact = True | |
if not contact['email'] in emails_to_contact: | |
emails_to_contact.append(contact['email']) | |
break | |
for contact in POCs: | |
if found_contact: | |
break | |
if not contact['email'] in emails_to_contact: | |
emails_to_contact.append(contact['email']) | |
entity_list.append({"name":network_name,"asn":asn,"emails":emails_to_contact}) | |
except twentyc.rpc.client.NotFoundException: | |
print "Can't find peeringDB entry for AS " + str(asn) | |
message_body = """\ | |
Dear AMS-IX peer, | |
The body of the email. | |
Thank you, | |
-- | |
Arzhel Younsi | |
Network Engineer | |
Wikimedia Foundation | |
""" | |
server = smtplib.SMTP('smtp.gmail.com:587') | |
server.starttls() | |
server.login('email','password') | |
from_addr = '[email protected]' | |
for entity in entity_list: | |
try: | |
to_addr_list = entity['emails'] | |
cc_addr_list = [] | |
subject = 'Email subject to ' + entity['name'] | |
header = 'From: %s\n' % from_addr | |
header += 'To: %s\n' % ','.join(to_addr_list).encode('utf-8') | |
header += 'Cc: %s\n' % ','.join(cc_addr_list).encode('utf-8') | |
header += 'Subject: %s\n\n' % subject.encode('utf-8') | |
message = header + message_body.encode('utf-8') | |
problems = server.sendmail(from_addr, to_addr_list, message) | |
#pprint(problems) | |
print("Email sent to AS " + str(entity['asn']) + " - " + entity['name']) | |
except: | |
print "Failed to email AS " + str(entity['asn']) + " - " + entity['name'] | |
pass | |
server.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment