May - 2015 (~5 minutes read time)

We've covered how to setup ExaBGP and peer with a router, and then how to use python to add  and remove advertised routes in BGP either with static definitions or dynamically through health checking. There may be some of you out there with some sort of application that is already monitoring routes and you're trying to figure out how to connect it with ExaBGP for the actual interaction part? Well, what if we add an HTTP API to ExaBGP to give programmatic access to ExaBGP from some external utility? I'll go over two ways to do this using the Python built-in SimpleHTTPServer or Flask.

Using SimpleHTTPServer

This option is great since you don't need to install any extra modules. It seems to be pretty lightweight and should be enough for some basic HTTP interaction with ExaBGP. The basic functions that we need this API to do are receive a form via HTTP POST and print that to STDOUT. Since ExaBGP is executing the python script, the STDOUT output will be visible to the ExaBGP process. After the ExaBGP command is printed, we'll return the command to the browser as confirmation that the call was successful. Here's the example:

import cgi
import SimpleHTTPServer
import SocketServer
from sys import stdout

PORT = 5001

class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def createResponse(self, command):
        """ Send command string back as confirmation """
        self.send_header('Content-Type', 'application/text')

    def do_POST(self):
        """ Process command from POST and output to STDOUT """
        form = cgi.FieldStorage(
        command = form.getvalue('command')
        stdout.write('%s\n' % command)
        self.createResponse('Success: %s' % command)

handler = ServerHandler
httpd = SocketServer.TCPServer(('', PORT), handler)
stdout.write('serving at port %s\n' % PORT)

Using Flask

If you have bigger plans for the HTTP side of things and want to work with a web framework like Flask, this example is for you. The biggest difference is that you will have to install Flask and its dependencies, although that's easy:

$ pip install flask

Now we'll create a Flask file to listen for prefix commands via HTTP POST calls and print them to STDOUT so ExaBGP can do its magic:

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)

	return '%s\n' % command

if __name__ == '__main__':

Before you run off and put this on your production systems I have a couple disclaimers:

Hooking the HTTP API up to the ExaBGP process

Update the conf.ini file to run this script instead of our previous health check example:

group test {                         # An arbitrary group name
    router-id;            # Our local router-id
    neighbor {          # Remote neighbor to peer with
        local-address;    # Our local update-source
        local-as 65000;              # Our local AS
        peer-as 65000;               # Peer's AS
    process http-api {
        run \path\to\python \path\to\;

And now when we run $ exabgp conf.ini, you'll see the confirmation of running the python script at the end of the output, along with the debug output of both Flask or SimpleHTTPServer:

... | INFO     | 20432  | reactor       | New peer setup: neighbor local-ip local-as 65000 peer-as 65000 router-id family-allowed in-open
... | WARNING  | 20432  | configuration | Loaded new configuration successfully
... | INFO     | 20432  | processes     | Forked process http-api
 * Running on (Press CTRL+C to quit)

Once either HTTP service is running (somewhat wrapped by the ExaBGP process), we can make HTTP POST calls via command line (curl, wget) or via a GUI HTTP tool (Postman). I'll show a quick example of both:


packetgeek:~ mat$ curl --form "command=announce route next-hop self" http://localhost:5000/ 
announce route next-hop self

Postman: ~

When we run show bgp on the router, we can see our advertised network:

CRS1#sh bgp | b Network
     Network          Next Hop            Metric LocPrf Weight Path
 *>                  0         32768 i
 *>i                    100      0 i

So, there's a real quick and dirty way to allow external calls to ExaBGP for your RIB manipulation. The next post will cover more advanced peering and advertising options with ExaBGP.