Скрипт:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from jinja2 import FileSystemLoader, Environment
import codecs
import sys
import re
import os
import ast
reload(sys)
print “Вас приветствует программа генерации конфигов для bgp сессий”
vlan=int(raw_input(“Введите номер vlan’а :”))
interface=raw_input(“Какой интерфейс будет использоваться? (xe-2/0/1):”)
peer_type=raw_input(“Какой тип подключения, ix или ip transit? (введите ix или isp):”)
peer_name=raw_input(“Введите имя пира латиницей прописными буквами:”‘\n’)
peer_ip=raw_input(“Введите ipv4 адрес пира:”)
local_ip=raw_input(“Введите локальный ipv4 адрес в формате x.x.x.x/nn:”)
peer_as=int(raw_input(“Введите номер as пира (например 44882):”))
prefix_limit_in=int(raw_input(“Введите лимит на количество принимаемых префиксов:”))
local_preference=int(raw_input(“Введите local preference для анонсируемых префиксов:”))
blackhole_community=raw_input(“Введите blackhole_community (0:666):”)
env = Environment(loader=FileSystemLoader(‘/home/tarasko/projects/doc_auto/templates’), trim_blocks=True)
template = env.get_template(‘doc_template_test.txt’)
data = {‘peer_name’:’%s’ % peer_name, ‘peer_ip’:’%s’ % peer_ip, ‘vlan’:’%s’ % vlan, ‘interface’: ‘%s’ % interface, ‘peer_type’:’%s’ % peer_type, ‘local_ip’:’%s’ % local_ip, ‘peer_as’:’%s’ % peer_as, ‘prefix_limit_in’:’%s’ % prefix_limit_in, ‘local_preference’:’%s’ % local_preference, ‘blackhole_community’:’%s’ % blackhole_community}
with open(‘/home/tarasko/projects/doc_auto/doc/project.txt’,’w’) as f:
f.write(template.render(data))
Шаблон:
set interfaces {{interface}} unit {{vlan}} description {{peer_name}}
set interfaces {{interface}} unit {{vlan}} vlan-id {{vlan}}
set interfaces {{interface}} unit {{vlan}} family inet filter input block_shit
set interfaces {{interface}} unit {{vlan}} family inet filter output block_shit_out
set interfaces {{interface}} unit {{vlan}} family inet sampling input
set interfaces {{interface}} unit {{vlan}} family inet address {{local_ip}}
set protocols bgp group {{peer_type}} neighbor {{peer_ip}} description {{peer_name}}
set protocols bgp group {{peer_type}} neighbor {{peer_ip}} import {{peer_name}}-IN
set protocols bgp group {{peer_type}} neighbor {{peer_ip}} family inet unicast prefix-limit maximum {{prefix_limit_in}}
set protocols bgp group {{peer_type}} neighbor {{peer_ip}} family inet unicast prefix-limit teardown 90
set protocols bgp group {{peer_type}} neighbor {{peer_ip}} export {{peer_name}}-OUT
set protocols bgp group {{peer_type}} neighbor {{peer_ip}} peer-as {{peer_as}}
set policy-options policy-statement {{peer_name}}-IN term BOGONS from protocol bgp
set policy-options policy-statement {{peer_name}}-IN term BOGONS from prefix-list BOGONS
set policy-options policy-statement {{peer_name}}-IN term BOGONS then reject
set policy-options policy-statement {{peer_name}}-IN term DEFAULT from protocol bgp
set policy-options policy-statement {{peer_name}}-IN term DEFAULT then local-preference {{local_preference}}
set policy-options policy-statement {{peer_name}}-IN term DEFAULT then accept
set policy-options policy-statement {{peer_name}}-OUT term BLACKHOLE from protocol static
set policy-options policy-statement {{peer_name}}-OUT term BLACKHOLE from tag 666
set policy-options policy-statement {{peer_name}}-OUT term BLACKHOLE then community set {{peer_name}}_Blackhole
set policy-options policy-statement {{peer_name}}-OUT term BLACKHOLE then accept
set policy-options policy-statement {{peer_name}}-OUT term VSEVNET from protocol static
set policy-options policy-statement {{peer_name}}-OUT term VSEVNET from policy OWN_PREFIXES
set policy-options policy-statement {{peer_name}}-OUT term VSEVNET then accept
set policy-options policy-statement {{peer_name}}-OUT then reject
set policy-options community {{peer_name}}_Blackhole members {{blackhole_community}}
Всё пока примитивно – забиваем данные, получаем конфиг на выходе.
Что сделать:
- Нужно добавить проверку вводимых данных (asn, ip)
- Возможность добавления двух rs
- отдельные циклы для ix и для ip transit
- Брать данные из ripedb