Skip to main content

Python API client library for Fortinet's FortiManager.

Project description

pyfortimanager

Python API client library for Fortinet's FortiManager.

It does not provide all endpoints or functionality available. We encourage to make a pull request with needed missing endpoints.

Note: This library has been built and tested for FortiManager v7.0.x.

Installation

To install run pip install pyfortimanager.

Alternatively, you can clone the repo and run python setup.py install.

Login handling

As the FortiManager API does not provide an expiration date/time when logging in, we've made some workarounds to handle login sessions without hitting the maximum login session limit in FortiManager. We do this by checking if our session is still valid by hitting an API endpoint, and if not we log out with our old token and log in again to retrieve a new token before executing the actual requested API endpoint. Additionally, we also use the standard Python module atexit to gracefully log out during a Python script executing.

It's therefore not needed to login and logout in your code, as this library handles that for you.

Quick Start

To begin, import pyfortimanager and instantiate the API.

We need to provide the IP or FQDN to the FortiManager instance and a user with access to the API. Optionally, its possible to set adom which defaults to root and verify which defaults to True.

Code

import pyfortimanager
fortimanager = pyfortimanager.api(
    host = "https://fortimanager.example.com",
    username = "apiuser",
    password = "secret",
)

Examples

List all FortiGates.

There is a ton of data for a single FortiGate. This code retrieves all of it, but only prints the name of the FortiGates.

Code

fmg_fortigates = fortimanager.fortigates.all()
for fmg_fortigate in fmg_fortigates['data']:
    print(fmg_fortigate['name'])

Output

FortiGate-VM64-1
FortiGate-VM64-2
FortiGate-VM64-3

Status object.

You can use the status object to check if the request is a success or not, and retrieve the error message.

Code

fmg_fortigate = fortimanager.fortigates.all(fortigate="FortiGate-VM64-4")
if fmg_fortigate['status']['code'] == 0:
    print(fmg_fortigate['data']['name'])
else:
    print(fmg_fortigate['status'])

Output

"status": {
    "code": -3,
    "message": "Object does not exist"
}

Adding a FortiGate

This creates a model device in the Device Manager with the minimum required fields.

Code

fmg_fortigate_add = fortimanager.fortigates.add(
    serial = "FGT60FTK1234ABCD",
    mr = 0,
    os_ver = 7
)
print(fmg_fortigate_add)

Output

{
    "data": {
        "device": {
            "beta": -1,
            "branch_pt": 516,
            "build": 516,
            "conn_mode": 1,
            "dev_status": 1,
            "flags": 67371040,
            "hostname": "FGT60FTK1234ABCD",
            "maxvdom": 10,
            "mgmt_id": 999918516,
            "mgmt_mode": 3,
            "mr": 0,
            "name": "FGT60FTK1234ABCD",
            "oid": 61594,
            "os_type": 0,
            "os_ver": 7,
            "patch": -1,
            "platform_id": 19,
            "platform_str": "FortiGate-60F",
            "sn": "FGT60FTK1234ABCD",
            "source": 1,
            "tab_status": "<unknown>",
            "version": 700
        }
    },
    "status": {
        "code": 0,
        "message": "OK"
    },
    "url": "/dvm/cmd/add/device"
}

Retrieve all connected Wi-Fi clients on a FortiGate

To retrieve all current active Wi-Fi clients on the FortiGate, we need to call the FortiOS API directly on the FortiGate through FortiManager's proxy API.

When making proxy calls, you'll retrieve two status objects. The first is for the FortiManager call and the the second is for the API call on the FortiGate.

Note: Proxy calls only works, if the FortiGate is online.

Code

fmg_wifi_clients = fortimanager.fortiaps_proxy.clients(fortigate="FortiGate-VM64-1")
print(fmg_wifi_clients)

Output

{
    "data": [
        {
            "response": {
                "action": "",
                "build": 523,
                "http_method": "GET",
                "name": "client",
                "path": "wifi",
                "results": [
                    {
                        "11k_capable": false,
                        "11r_capable": false,
                        "11v_capable": true,
                        "association_time": 1698143761,
                        "authentication": "pass",
                        "bandwidth_rx": 2493967,
                        "bandwidth_tx": 2564936,
                        "captive_portal_authenticated": 0,
                        "channel": 44,
                        "data_rate_bps": 573600000,
                        "data_rxrate_bps": 286800000,
                        "data_txrate_bps": 286800000,
                        "encrypt": 1,
                        "health": {
                            "band": {
                                "severity": "good",
                                "value": "5ghz"
                            },
                            "signal_strength": {
                                "severity": "good",
                                "value": -52
                            },
                            "snr": {
                                "severity": "good",
                                "value": 43
                            },
                            "transmission_discard": {
                                "severity": "good",
                                "value": 0
                            },
                            "transmission_retry": {
                                "severity": "good",
                                "value": 0
                            }
                        },
                        "host": "WINDOWS-PC",
                        "hostname": "WINDOWS-PC",
                        "idle_time": 1,
                        "ip": "10.10.10.10",
                        "ip6": [
                            "fe80::c28d:52e5:68a4:95ad"
                        ],
                        "lan_authenticated": false,
                        "mac": "aa:bb:cc:dd:ee:ff",
                        "manufacturer": "Microsoft",
                        "mimo": "2x2",
                        "noise": -95,
                        "os": "Windows",
                        "radio_type": "802.11ax-5G",
                        "security": 12,
                        "security_str": "wpa2_only_enterprise",
                        "signal": -52,
                        "snr": 43,
                        "ssid": "SSID",
                        "sta_maxrate": 286800,
                        "sta_rxrate": 286800,
                        "sta_rxrate_mcs": 11,
                        "sta_rxrate_score": 100,
                        "sta_txrate": 286800,
                        "sta_txrate_mcs": 11,
                        "sta_txrate_score": 100,
                        "tx_discard_percentage": 0,
                        "tx_retry_percentage": 0,
                        "user": "host/WINDOWS-PC.local.net",
                        "vap_name": "SSID",
                        "vci": "MSFT 5.0",
                        "vlan_id": 101,
                        "wtp_control_ip": "10.20.30.40",
                        "wtp_control_local_ip": "10.20.30.40",
                        "wtp_id": "FP431FTF12345678",
                        "wtp_ip": "10.20.30.40",
                        "wtp_name": "FAP-431F",
                        "wtp_radio": 2
                    }
                ],
                "serial": "FGT60FTK1234ABCD",
                "status": "success",
                "vdom": "root",
                "version": "v7.0.12"
            },
            "status": {
                "code": 0,
                "message": "OK"
            },
            "target": "FortiGate-VM64-1"
        }
    ],
    "status": {
        "code": 0,
        "message": "OK"
    },
    "url": "/sys/proxy/json"
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyfortimanager-1.0.4.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyfortimanager-1.0.4-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file pyfortimanager-1.0.4.tar.gz.

File metadata

  • Download URL: pyfortimanager-1.0.4.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pyfortimanager-1.0.4.tar.gz
Algorithm Hash digest
SHA256 8e8f365ca8c3a0e770a8316595ada07e7c0adf110682baddceae65d192102948
MD5 bd8a33f298995c179a057413e17ff5e4
BLAKE2b-256 fe539b463b7071691dd2e402c61645bbd0961481c2d7902848fcda5c3a5aa21a

See more details on using hashes here.

File details

Details for the file pyfortimanager-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: pyfortimanager-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pyfortimanager-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0e3c7283e81d2c61fa1391a0db9e9d1600d33db0a0168c6bc632eddf9cf607b1
MD5 a45ada2bd6addc684cb00c97d9fab4de
BLAKE2b-256 6e38f1ed89c2caef81edc1a132347b89c4f70d763fda1cca803d5f8eb1b08329

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page