FastNetMon Advanced example API client which adds and removes networks

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 urllib
import random

auth_data = ('admin', 'your_password_replace_it')

# Create random network for tests
new_network = "%d.%d.%d.%d/22" % (random.randint(1,254), random.randint(1,254), random.randint(1,254), random.randint(1,254))  

print "Add random network:", new_network
# Encode any values which have spaces or '/' inside them
new_network_encoded = urllib.quote_plus(new_network)

# Create networks_list entry
r = requests.put('' + new_network_encoded, auth=auth_data)

if r.status_code == 200:
    print "Correctly added new subnet"
    print "Can't add new subnet", r.json()['error_text']

# Show all networks list entries
r = requests.get('', auth=auth_data)

if r.status_code != 200:
    print "API call failed"

print "Networks list with new network:", r.json()

r = requests.delete('' + new_network_encoded, auth=auth_data)

if r.status_code != 200:
    print "Cannot delete network from networks_list"
    print "Correctly removed new subnet"

Enable execution flag:

chmod +x fastnetmon_api_client.py

And run it this way:


Valid output looks like:

Add random network:
Correctly added new subnet
Networks list with new network: {u'values': [u''], u'error_text': u'', u'success': True}
Correctly removed new subnet