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:

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 http_api.py file to listen for prefix commands via HTTP POST calls and print them to STDOUT so ExaBGP can do its magic:

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

  • This script uses the built-in Flask debug HTTP server. It’s fine for lab use, but I would use gunicorn and nginx for real heavy lifting.
  • The script doesn’t do any validation of the command. A better script would make sure it’s a valid command and prefix.
  • Flask defaults to listening on the localhost address on TCP port 5000. You can change this though and I highly recommend reading Flask’s QuickStart article to familiarize yourself with the many options.

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:

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:

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:

Curl:

Postman:Postman - HTTP POST

 

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

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.

Series Navigation<< Using service health checks to automate ExaBGPAdvanced Router Peering and Route Announcement >>

This article has 2 comments

  1. Irek Romaniuk

    Very cool, I tested with Checkpoint Gaia, works fine (not sure why ‘Hidden’ route;)
    REP-WAL-01> show route all
    Codes: C – Connected, S – Static, R – RIP, B – BGP,
    O – OSPF IntraArea (IA – InterArea, E – External, N – NSSA),
    A – Aggregate, K – Kernel Remnant, H – Hidden, P – Suppressed,
    U – Unreachable, i – Inactive
     
    ….
    B    H i  100.10.0.0/24       via 10.29.1.102, eth0, cost -1, age 89
    ….
    but had to add
    family {
    ipv4 unicast;
    }

    because of error:
    ExaBGP stdout
    Fri, 05 Jun 2015 10:03:51 | INFO     | 10549  | network       | Peer      10.29.1.68 ASN 65000   out loop, peer reset, message [notification received (2,4)] error[OPEN message error / Unsupported Optional Parameter]
    Gaia /var/log/messages:
    Jun  5 11:06:35 2015 REP-WAL-01 routed[5434]: bgp_get_open(2594): peer 10.29.1.102+50908 (proto) mp capability unrecognized
    Jun  5 11:06:35 2015 REP-WAL-01 routed[5434]: NOTIFICATION sent to 10.29.1.102+50908 (proto): code 2 (OpenMessageError) subcode 4 (unsupported option code) data

Leave a Reply

Your email address will not be published. Required fields are marked *