#!/usr/bin/python3
import requests
import json
import sys
from time import gmtime, strftime
def x2_login(board_addr, username, password):
# Create request
url = "https://" + board_addr + "/api2/tokens"
data = {}
data['username'] = username
data['password'] = password
# Send Request
headers = {'content-type': 'application/json'}
r = requests.post(url, headers=headers, json=data, verify=False)
print(r.status_code)
print(r.content)
return json.loads(r.content)
def x2_create_ruleset(board_addr, token, set_name):
# Prepare request
url = "https://" + board_addr + "/api2/tm/rulesets"
data = {}
data['name'] = set_name
data['description'] = "A simple RuleSet crated by Python using Profitap API"
headers={}
headers['content-type'] = 'application/json'
headers['Authorization'] = token
# Check result
r = requests.post(url, headers=headers, json=data, verify=False)
print(r.status_code)
print(r.content)
# Return set id
resp = json.loads(r.content)
return resp['data']
def x2_create_rules(board_addr, token, set_id):
# Prepare request
url = "https://" + board_addr + "/api2/tm/rulesets/" + str(set_id) + "/rules/"
headers={}
headers['content-type'] = 'application/json'
headers['Authorization'] = token
# Set static data
src_ports = []
src_ports.append({})
src_ports[0] = {}
src_ports[0]['id'] = 1
src_ports[0]['type'] = 'INF'
src_ports.append({})
src_ports[1] = {}
src_ports[1]['id'] = 5
src_ports[1]['type'] = 'INF'
dst_ports = []
dst_ports.append({})
dst_ports[0] = {}
dst_ports[0]['id'] = 9
dst_ports[0]['type'] = 'INF'
dst_ports.append({})
dst_ports[1] = {}
dst_ports[1]['id'] = 13
dst_ports[1]['type'] = 'INF'
data = {}
data['src_ports'] = src_ports;
data['dst_ports'] = dst_ports;
data['enable_counter']: False;
data['filter'] = {}
data['filter']['l2'] = {}
data['filter']['l2']['eth_type'] = {}
data['filter']['l2']['eth_type']['type'] = 'hex16'
data['filter']['l2']['eth_type']['val'] = '0800'
data['filter']['l2']['eth_type']['mask'] = 'FFFF'
data['filter']['l3'] = {}
data['filter']['l3']['dst_addr'] = {}
data['filter']['l3']['dst_addr']['type'] = 'ipv4'
# data['filter']['l3']['dst_addr']['val'] = 'ipv4'
data['filter']['l3']['dst_addr']['mask'] = '255.255.255.255'
data['filter']['l4'] = {}
# Define list of destionation IP addresses
dst_ip_addrs = ["192.168.1.200", "192.168.10.200", "8.8.8.8", "10.1.1.1"]
for ip_addr in dst_ip_addrs:
data['name'] = "Rule_ip_" + ip_addr.replace(".", "_")
data['filter']['l3']['dst_addr']['val'] = ip_addr
r = requests.post(url, headers=headers, json=data, verify=False)
print(r.status_code)
print(r.content)
board_url="profitap_board_demo_address"
username="user"
password="user_password"
auth_reply = x2_login(board_url, username, password)
rule_set = x2_create_ruleset(board_url, auth_reply['token'], 'RuleSet_' + strftime("%Y_%m_%d_%H_%M_%S", gmtime()))
x2_create_rules(board_url, auth_reply['token'], rule_set['id'])