REST API description: Difference between revisions

no edit summary
mNo edit summary
No edit summary
 
Line 616: Line 616:
with session.post(f"{host}/API/data/modules/capture/buffer/0", data=json.dumps(data)) as resp:
with session.post(f"{host}/API/data/modules/capture/buffer/0", data=json.dumps(data)) as resp:
     print(resp.text)
     print(resp.text)
</pre>
==== Python Script Example - Server ports used ====
This script generates a CSV file with all IPs and their server ports used.
The output format is as follows:
ip address,ip role,server port
The ip role is either asClient or asServer which indicates whether the IP address runs the server port (asServer) or contacts the server port (asClient).
<pre>
#! /usr/bin/python3
import requests
import csv
import sys
requests.packages.urllib3.disable_warnings()
# set host
host = "https://allegro-mm-xxxx"
session = requests.Session()
# set credential
session.auth = ("admin", "allegro")
session.verify = False  # disable ssl verification
csvoutput = csv.writer(sys.stdout)
# loop over list of IPs
with session.get(host + "/API/stats/modules/ip/ips") as resp:
    ip_list = resp.json()
    for ip in ip_list:
        # for each IP get peers_ports as CSV
        with session.get(host + f"/API/stats/modules/ip/ips/{ip}/peers_ports?csv=true", stream = True) as resp:
            if resp.status_code != 200:
                # IP not existing (anymore?)
                continue
            ports = {}
            # loop over all ports and accumulate
            for nr, line in enumerate(resp.iter_lines()):
                if nr == 0:
                    # skip header line
                    continue
                line = line.decode()
                if line:
                    for row in csv.reader([line], delimiter=',', quotechar='"'):
                        if len(row) < 7:
                            continue
                        port=row[1]
                        if row[6] == "client":
                            ports.setdefault("asServer",{})[port] = 1
                        else:
                            ports.setdefault("asClient",{})[port] = 1
            # now loop over accumulated results and create CSV lines
            for connection_type, type_ports in ports.items():
                res = list( map( lambda e: (ip, connection_type, e), type_ports.keys() ) )
                csvoutput.writerows(res)
</pre>
</pre>
546

edits