Skip to main content

Python package to configure Fortigate (Fortios) devices using REST API and SSH

Project description

https://img.shields.io/pypi/v/fortigate-api.svg https://img.shields.io/pypi/pyversions/fortigate-api.svg https://img.shields.io/github/last-commit/vladimirs-git/fortigate-api

fortigate-api

Python package to configure Fortigate (Fortios) devices using REST API.

  • FortiGateAPI - Python connector to Fortigate API endpoints.

  • FortiGate - Python wrapper for the FortiOS REST API.

Checked with FortiOS = v6.4.14. Fully documented on Read the Docs.


Quickstart

Install the package from pypi.org

pip install fortigate-api

or from github.com repository

pip install git+https://github.com/vladimirs-git/fortigate-api

"""Quickstart FortiGateAPI.

- Create address in the Fortigate
- Get all addresses from the Fortigate vdom root
- Get address by name (unique identifier)
- Filter address by operator contains `=@`
- Update address data in the Fortigate
- Delete address from the Fortigate
"""

from pprint import pprint

from fortigate_api import FortiGateAPI

HOST = "host"
USERNAME = "username"
PASSWORD = "password"

api = FortiGateAPI(host=HOST, username=USERNAME, password=PASSWORD)

# Create address in the Fortigate
data = {
    "name": "ADDRESS",
    "obj-type": "ip",
    "subnet": "127.0.0.100 255.255.255.252",
    "type": "ipmask",
}
response = api.cmdb.firewall.address.create(data)
print(f"address.create {response}")  # address.create <Response [200]>

# Get all addresses from the Fortigate vdom root
items = api.cmdb.firewall.address.get()
print(f"All addresses count={len(items)}")  # All addresses count=14

# Get address by name (unique identifier)
items = api.cmdb.firewall.address.get(name="ADDRESS")
print(f"addresses count={len(items)}")  # addresses count=1
pprint(items)
#  [{"comment": "",
#    "name": "ADDRESS",
#    "subnet": "127.0.0.100 255.255.255.252",
#    "uuid": "a386e4b0-d6cb-51ec-1e28-01e0bc0de43c",
#    ...
#    }]

# Filter address by operator contains `=@`
items = api.cmdb.firewall.address.get(filter="subnet=@127.0")
print(f"Filtered by `=@`, count={len(items)}")  # Filtered by `=@`, count=2

# Update address data in the Fortigate
data = {"name": "ADDRESS", "subnet": "127.0.0.255 255.255.255.255"}
response = api.cmdb.firewall.address.update(data)
print(f"address.update {response}")  # address.update <Response [200]>

# Delete address from the Fortigate
response = api.cmdb.firewall.address.delete("ADDRESS")
print(f"address.delete {response}")  # address.delete <Response [200]>

api.logout()
"""Quickstart FortiGate.

- Creates address in the Fortigate
- Get address by name (unique identifier)
- Updates address data in the Fortigate
- Delete address from the Fortigate
"""

from pprint import pprint

from fortigate_api import FortiGate

HOST = "host"
USERNAME = "username"
PASSWORD = "password"

fgt = FortiGate(host=HOST, username=USERNAME, password=PASSWORD)

# Creates address in the Fortigate
data = {
    "name": "ADDRESS",
    "obj-type": "ip",
    "subnet": "127.0.0.100 255.255.255.252",
    "type": "ipmask",
}
response = fgt.post(url="api/v2/cmdb/firewall/address/", data=data)
print(f"POST {response}", )  # POST <Response [200]>

# Get address by name (unique identifier)
response = fgt.get(url="api/v2/cmdb/firewall/address/ADDRESS")
print(f"GET {response}", )  # POST <Response [200]>
result = response.json()["results"]
pprint(result)
#  [{"name": "ADDRESS",
#    "subnet": "127.0.0.100 255.255.255.252",
#    "uuid": "a386e4b0-d6cb-51ec-1e28-01e0bc0de43c",
#    ...
#    }]

# Updates address data in the Fortigate
data = {"name": "ADDRESS", "subnet": "127.0.0.255 255.255.255.255"}
response = fgt.put(url="api/v2/cmdb/firewall/address/ADDRESS", data=data)
print(f"PUT {response}")  # PUT <Response [200]>

# Delete address from the Fortigate
response = fgt.delete(url="api/v2/cmdb/firewall/address/ADDRESS")
print(f"DELETE {response}", )  # DELETE <Response [200]>

fgt.logout()

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

fortigate_api-2.0.1.tar.gz (3.2 MB view details)

Uploaded Source

Built Distribution

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

fortigate_api-2.0.1-py3-none-any.whl (3.5 MB view details)

Uploaded Python 3

File details

Details for the file fortigate_api-2.0.1.tar.gz.

File metadata

  • Download URL: fortigate_api-2.0.1.tar.gz
  • Upload date:
  • Size: 3.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for fortigate_api-2.0.1.tar.gz
Algorithm Hash digest
SHA256 07354e148c96836d5e5dfd7431222d8021a15ccd0e7f5c5a95ac4825f2c5cb3a
MD5 60936830b2525ca9614bccad56c62649
BLAKE2b-256 fdbab1a7ad01b9891b2cd76a3b97a5800e3b0f4d7cd8732527882c1208703883

See more details on using hashes here.

File details

Details for the file fortigate_api-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: fortigate_api-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for fortigate_api-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e231c123d0f760176e92c7e4209621eae7801d40446ff6dea2d91d32977e674
MD5 dcf7a8878538af7f4c067c09c2253cba
BLAKE2b-256 7a276e018ab9f2772d5d1cfb8489b446d001a69044a8ea1963dfca920569e9ac

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