Friday, September 16, 2016

bgp peering with exabgp

exabgp is application that provide us a way to grammatically configure/control bgp. it always have http api to interactive with bgp.

1- router (quagga) bgp configuration
router bgp 1
 bgp router-id 10.0.1.1
 network 10.0.0.0/8
 redistribute connected
 neighbor n3 peer-group
 neighbor n3 remote-as 3
 neighbor n3 soft-reconfiguration inbound
 neighbor 10.0.0.2 peer-group n3
 exit   



2- the goal is we will form neighbor with the router above and advertise network 192.168.202.0/24

they are two to perform on exabgp, 1- run script directly in the configuration file 2- update router using http api


- method 1 (run script directly in the configuration file )

a- create exabgp.conf in ~/exabgp.conf with the following content
group n1 {
        router-id 10.0.0.2;
       
        neighbor 10.0.0.1{
                local-address 10.0.0.2;
                local-as 3;
                peer-as 1;
        }
    process add-routes {
        run ~/addroute.py;
    }
}


b- create a script call addrouter.py in ~/addrouter.py with the following content
#!/usr/bin/python

from sys import stdout
from time import sleep

messages = ['announce route 192.168.202.0/24 next-hop self']

sleep(2)
for message in messages:
    stdout.write( message + "\n")
    stdout.flush()
    sleep(1)
   

while True:
    sleep(1)


c- add execute permission to our script
$ chmod 755 ~/addrouter.py

d- run exabpg
$ exabgp ~/exabgp.conf

- method 2 (update router using http api )
a- create exabgp.conf in ~/exabgp.conf with the following content
group n1 {
        router-id 10.0.0.2;
       
        neighbor 10.0.0.1{
                local-address 10.0.0.2;
                local-as 3;
                peer-as 1;
        }
    process http-api {
        run /home/tin/httpapi.py;
    }

}


b- create a script call httpapi.py in ~/httpapi.py with the following content
#!/usr/bin/python
from flask import Flask, request
from sys import stdout

app = Flask(__name__)

# Setup a command route to listen for prefix advertisements
@app.route('/', methods=['POST'])
def command():

        command = request.form['command']
        stdout.write('%s\n' % command)
        stdout.flush()

        return '%s\n' % command

if __name__ == '__main__':
    app.run()



c- add execute permission to our script
$ chmod 755 ~/httpapi.py

d- run exabpg
$ exabgp ~/exabgp.conf

e- add route
$ curl --form "command=announce route 192.168.202.0/24 next-hop self" http://localhost:5000/

and if you want to remove the route

$ curl --form "command=withdraw route 192.168.202.0/24 next-hop self" http://localhost:5000/


3- when we view route on router n1 we get route we advertise using exabgp

n1# show ip route
Codes: K - kernel route, C - connected, S - static, R - RIP,
       O - OSPF, I - IS-IS, B - BGP, P - PIM, A - Babel,
       > - selected route, * - FIB route

C>* 10.0.0.0/24 is directly connected, eth0
C>* 10.0.1.0/24 is directly connected, eth1
C>* 127.0.0.0/8 is directly connected, lo
B>* 192.168.202.0/24 [20/0] via 10.0.0.2, eth0, 00:13:56

No comments:

Post a Comment