usr bin env python coding utf-8 ASN 49373 LPREF_TO_COMMUNITY_M AP 200

  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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
# -*- coding: utf-8 -*-
ASN = 49373
LPREF_TO_COMMUNITY_MAP = {200: 'FT_CUSTOMER', 190: 'FT_PEER_MSK', 180: 'FT_PEER_RUSSIA', 175: 'FT_PEER_UKRAINE', 170: 'FT_UPLINK_RUSSIA', 160: 'FT_PEER_INT', 150: 'FT_UPLINK_INT', 140: 'FT_BACKUP'}
import os
import re
import sys
RPSL = '^(import|export): (?:from|to) AS([0-9]+) ((?:[0-9]+\.){3}[0-9]+) at ((?:[0-9]+\.){3}[0-9]+)(?: action pref=([0-9]+);)? (?:accept|announce) ([\w-]+)$'
AS_SET = {}
AS_NUM = {}
def as_set_to_as_nums(as_set, as_list, as_set_list):
as_set_list.append(as_set)
for child in os.popen('whois -h whois.ripe.net %s | grep ^members:' % as_set):
child = child.strip()
while ' ' in child: child = child.replace(' ', ' ')
child = child.replace(', ', ',')
child = child.split(': ', 1)[1]
if '#' in child: child = child.split('#')[0]
for child_object in child.split(','):
if '-' in child_object:
# Another AS-set
if not child_object in as_set_list:
as_set_to_as_nums(child_object, as_list, as_set_list)
else:
if not child_object in as_list:
as_list.append(child_object)
def process_as_num(as_num):
asn_prefix_list = []
for prefix in os.popen('whois -h whois.ripe.net -i origin %s | grep ^route:' % as_num):
prefix_object = prefix.strip()
while ' ' in prefix_object:
prefix_object = prefix_object.replace(' ', '')
while '\t' in prefix_object:
prefix_object = prefix_object.replace('\t', '')
prefix_object = prefix_object.split(':')[-1]
asn_prefix_list.append(prefix_object)
asn_prefix_list.sort()
return asn_prefix_list
command_set = {}
for line in os.popen('whois -h whois.ripe.net AS%i' % ASN).readlines():
if line.split(':', 1)[0] in ('import, export'):
while ' ' in line: line = line.replace(' ', ' ').strip()
rpsl_type, remote_as, neighbor, router, lpref, as_set = re.compile(RPSL).match(line).groups()
remote_as = int(remote_as)
if not lpref is None: lpref = int(lpref)
command_set.setdefault(router, [])
if rpsl_type == 'import':
# Blackhole community and local-prefs (only for customerss)
if lpref == 200:
command_set[router].append('route-map FROM-AS%i permit 10' % remote_as)
command_set[router].append('match community BLACKHOLE')
command_set[router].append('set interface Null 0')
seq = 20
lpref_keys = LPREF_TO_COMMUNITY_MAP.keys()
lpref_keys.sort()
lpref_keys.reverse()
for lpref_value in lpref_keys:
command_set[router].append('route-map FROM-AS%i permit %i' % (remote_as, seq))
command_set[router].append('match community %s' % LPREF_TO_COMMUNITY_MAP[lpref_value])
command_set[router].append('set local-preference %i' % lpref_value)
command_set[router].append('set comm-list %i:%i delete' % (ASN, lpref_value))
command_set[router].append('continue 30')
seq += 1
# Match prefix-list, set lpref and community
command_set[router].append('route-map FROM-AS%i permit 30' % remote_as)
command_set[router].append('match ip prefix-list IMPORT-%s' % as_set)
command_set[router].append('set local-preference %i' % lpref)
command_set[router].append('set community %i:%i' % (ASN, lpref))
command_set[router].append('exit')
if as_set == 'ANY':
networks = ['0.0.0.0/0 le 24']
else:
if '-' in as_set:
if not as_set in AS_SET:
AS_SET[as_set] = []
as_set_to_as_nums(as_set, AS_SET[as_set], [])
for as_num in AS_SET[as_set]:
if not as_num in AS_NUM:
AS_NUM[as_num] = process_as_num(as_num)
for prefix in AS_SET[as_num]:
if not prefix in networks: networks.append(prefix)
else:
if not as_set in AS_NUM:
AS_NUM[as_set] = process_as_num(as_set)
networks = AS_NUM[as_set]
print networks
for prefix in networks:
command_set[router].append('ip prefix-list IMPORT-%s permit %s' % (as_set, prefix))
for router in command_set:
print '\n'.join(command_set[router])
print