FastNetMon Advanced example API client which blocks and unblocks IP

This example script can create blackhole announce, list blocked hosts and remove it using UUID lookup.

To use this example, please enable API using this guide and install dependencies for it:

sudo apt-get install -y python-requests

And put this code into fastnetmon_api_client.py file:

import requests
import os
import sys
import time
import urllib
import random
auth_data = ('admin', 'your_password_replace_it')
# Create random IP tests
ip_to_blackhole = "127.0.0.%d" % random.randint(1,254)
print "Blackhole IP:", ip_to_blackhole

# Encode any values which have spaces or '/' inside them
ip_to_blackhole = urllib.quote_plus(ip_to_blackhole)

r = requests.put('' + ip_to_blackhole, auth=auth_data)
if r.status_code == 200:
    print "Correctly blackholed IP"
    print "Can't add new subnet", r.json()['error_text']

print "Wait 5 second to propagate changes"

# Show all blocked hosts
r = requests.get('', auth=auth_data)
if r.status_code != 200:
    print "API call failed", r.json()["error_text"]
# Let's find UUID for this IP address
blocked_hosts = r.json()["values"]

blackhole_uuid = ""

for host in blocked_hosts:
    if host["ip"] == ip_to_blackhole + "/32":
        blackhole_uuid = host["uuid"] 

if blackhole_uuid == "":
    print "Can't find UUID for blackholed host"
    print "Correctly found UUID for this IP", blackhole_uuid

r = requests.delete('' + blackhole_uuid, auth=auth_data)
if r.status_code != 200:
    print "Cannot delete blackhole announce"
    print "Correctly removed blackhole announce"

Enable execution flag:

chmod +x fastnetmon_api_client.py

And run it this way:


Valid output looks like:

Blackhole IP:
Correctly blackholed IP
Wait 5 second to propagate changes
Correctly found UUID for this IP 2518758c-a751-4984-9aeb-c5a935422e91
Correctly removed blackhole announce