-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbgp_build.py
More file actions
executable file
·83 lines (74 loc) · 2.52 KB
/
bgp_build.py
File metadata and controls
executable file
·83 lines (74 loc) · 2.52 KB
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
#!/usr/bin/python
import requests
import json
from netmiko import ConnectHandler
import getpass
myusername = raw_input('Please enter your username: ')
mypassword = getpass.getpass()
def ripepull(routefamily):
index = 0
payload = {'source': 'ripe', 'inverse-attribute': 'origin', 'type-filter': routefamily, 'query-string': 'AS49182'}
try:
r = requests.get('http://rest.db.ripe.net/search.json', params=payload)
routejson = json.loads(r.text)
except:
print "API Call to the RIPE Database failed"
for elements in routejson['objects']['object']:
for those in elements['attributes']['attribute']:
if "route" in those['name']:
index += 10
route = those['value']
if routefamily=="route":
family = "IPv4"
elif routefamily=="route6":
family = "IPv6"
route = those['value']
prefix_build(route, family, index)
def prefix_build(inroute, infamily, inindex):
if infamily=="IPv4":
v4routes.append(inroute)
ipaddr = inroute.split('/')[0]
prefixlen = inroute.split('/')[1]
conf.append("ip prefix-list EGRESS index %s permit %s %s" % (inindex, ipaddr, prefixlen))
if infamily=="IPv6":
v6routes.append(inroute)
ipaddr = inroute.split('/')[0]
prefixlen = inroute.split('/')[1]
conf.append("ipv6 prefix-list EGRESS index %s permit %s %s" % (inindex, ipaddr, prefixlen))
def hp_connect_to_device(dtype,dip,prefixlist,v4routelist,v6routelist):
net_connect = ConnectHandler(device_type=dtype, ip=dip, username=myusername, password=mypassword)
try:
net_connect.send_command("sys\n")
for pfxlst in prefixlist:
net_connect.send_command(pfxlst)
net_connect.send_command('bgp 65000')
net_connect.send_command('address-family ipv4 unicast')
for routes in v4routelist:
pfxip = routes.split('/')[0]
pfxlen = routes.split('/')[1]
net_connect.send_command('aggregate %s %s detail-suppressed' % (pfxip, pfxlen))
net_connect.send_command('address-family ipv6 unicast')
for routes in v6routelist:
pfxip = routes.split('/')[0]
pfxlen = routes.split('/')[1]
net_connect.send_command('aggregate %s %s detail-suppressed' % (pfxip, pfxlen))
net_connect.send_command('save f')
net_connect.disconnect()
except:
print "Failed to connect to device"
def main():
hosts = {'hp_comware': '192.168.253.30'}
global conf
conf = []
global v4routes
v4routes = []
global v6routes
v6routes = []
ripepull("route")
ripepull("route6")
for them in hosts:
if "hp_comware" in them:
dtype, dip = them, hosts[them]
hp_connect_to_device(dtype,dip,conf,v4routes,v6routes)
if __name__ == "__main__":
main()