vladimirs-git/fortigate-api

GitHub: vladimirs-git/fortigate-api

一个封装 FortiOS REST API 的 Python 库,用于通过编程方式对 FortiGate 防火墙进行地址、策略等配置的增删改查操作。

Stars: 90 | Forks: 28

.. image:: https://img.shields.io/pypi/v/fortigate-api.svg :target: https://pypi.python.org/pypi/fortigate-api .. image:: https://img.shields.io/badge/Python-3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11-blue.svg :target: https://pypi.python.org/pypi/logger-color .. image:: https://img.shields.io/github/last-commit/vladimirs-git/fortigate-api :target: https://pypi.python.org/pypi/fortigate-api # fortigate-api 一个使用 REST API 配置 Fortigate (FortiOS) 设备的 Python 包。 ``` - FortiGateAPI - Python connector to Fortigate API endpoints. - FortiGate - Python wrapper for the FortiOS REST API. ``` 已在 FortiOS = v6.4.14 环境下测试通过。 ## 完整文档请参见 `Read the Docs`_。 # 要求 Python >=3.8 # 快速入门 从 pypi.org 安装此包 .. code:: bash ``` pip install fortigate-api ``` 或者从 github.com 仓库安装 .. code:: bash ## pip install git+https://github.com/vladimirs-git/fortigate-api .. code:: python ``` """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) # 在 Fortigate 中创建 address 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 # 从 Fortigate vdom root 获取所有 address items = api.cmdb.firewall.address.get() print(f"All addresses count={len(items)}") # All addresses count=14 # 按名称(唯一标识符)获取 address 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", # ... # }] # 使用操作符 contains `=@` 过滤 address items = api.cmdb.firewall.address.get(filter="subnet=@127.0") print(f"Filtered by `=@`, count={len(items)}") # Filtered by `=@`, count=2 # 在 Fortigate 中更新 address 数据 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 # 从 Fortigate 删除 address response = api.cmdb.firewall.address.delete("ADDRESS") print(f"address.delete {response}") # address.delete api.logout() ``` .. code:: python ``` """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) # 在 Fortigate 中创建 address 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 # 按名称(唯一标识符)获取 address response = fgt.get(url="api/v2/cmdb/firewall/address/ADDRESS") print(f"GET {response}", ) # POST result = response.json()["results"] pprint(result) # [{"name": "ADDRESS", # "subnet": "127.0.0.100 255.255.255.252", # "uuid": "a386e4b0-d6cb-51ec-1e28-01e0bc0de43c", # ... # }] # 在 Fortigate 中更新 address 数据 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 # 从 Fortigate 删除 address response = fgt.delete(url="api/v2/cmdb/firewall/address/ADDRESS") print(f"DELETE {response}", ) # DELETE ``` ## fgt.logout() .. _`Read the Docs`: https://fortigate-api.readthedocs.io/en/latest/
标签:API封装, Docker 部署, Fortigate, FortiGateAPI, FortiOS, IT基础设施, Python, REST API, 地址管理, 无后门, 网络安全, 网络设备配置, 网络运维, 自动化运维, 逆向工具, 防火墙, 隐私保护