Skip to content

Commit

Permalink
Added Api Bindings for Script Executions on Fortimanager
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaymane920 committed Nov 27, 2021
1 parent 3b93a7a commit 9c168ef
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 3 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,55 @@ Optional settings:

* :param name: Specify a name for the script tha need to be deleted.

### 34) Run a script on FortiManager's Database/ FortiGate's Remote CLI.
```python
>>> fortimngr.run_script_on_single_device(script_name="test_script",
device_name="FortiGate-VM64",
vdom="root")
```
- ## Parameters

* :param device_name: Specify device name.
* :param vdom: Specify the Vdom
* :param script_name: Specify the script name that should be executed on the specified devices


```python
>>> fortimngr.run_script_on_multiple_devices(script_name="test_script",
devices=[{"name":"FortiGate-VM64", "vdom": "root"},
{"name":"Test-FortiGate-VM64", "vdom": "global"},
{"name":"Test-2-FortiGate-VM64", "vdom": "Test"}])
```
- ## Parameters

* :param devices: Specify devices in a list of dictionaries.
```
eg. devices=[{"name": "FortiGateVM64-1", "vdom": "root"},
{"name": "FortiGateVM64-2", "vdom": "test"}
{"name": "FortiGateVM64-3", "vdom": "root"}]
```
* :param script_name: Specify the script name that should be executed on the specified devices



### 35) Backup FortiGate's configuration from FortiManager and store it in TFTP server.
```python
>>> fortimngr.backup_config_of_fortiGate_to_tftp(tftp_ip="1.1.1.1",
path="/FortiGate_backups",
filename="FortiGate.conf",
device_name="FortiGate-VM64", vdom="root")

```
####A small function to back up configuration on FortiGates from FortiManager and store it in TFTP Server.
This function leverages **_create_script()_** and **_run_script_on_single_device()_** methods from this package to backup the config.
- ## Parameters

* :param tftp_ip: Specify TFTP Server IP
* :param path: Specify the path to store the config
* :param filename: Specify the name of the backup file
* :param device_name: Specify the name of the device
* :param vdom: Specify the Vdom

## Contributing
- Being new to Python and this being my first publish, to get this module fully working for all of us, the Pull requests are welcome.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
setup(
name='pyFortiManagerAPI',
description='A Python wrapper for the FortiManager REST API',
version='0.1.4',
version='0.1.5',
py_modules=["pyFortiManagerAPI"],
package_dir={'': 'src'},
keywords=['Fortimanager', 'RestAPI', 'API', 'Fortigate', 'Fortinet', "python", "Fortimanager API",
Expand Down
77 changes: 75 additions & 2 deletions src/pyFortiManagerAPI.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
__author__ = "Akshay Mane"

import datetime
import json

import requests
import urllib3
import logging
from typing import List
from os.path import join, normpath

# Disable insecure connections warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
Expand Down Expand Up @@ -939,3 +939,76 @@ def delete_script(self, name: str):
delete_script = session.post(
url=self.base_url, json=payload, verify=self.verify)
return delete_script.json()["result"]

def run_script_on_multiple_devices(self, script_name: str, devices: List[dict]):
"""
Create a script template and store it on FortiManager
:param devices: Specify devices in a list of dictionaries.
eg. devices=[{"name": "FortiGateVM64-1", "vdom": "root"},
{"name": "FortiGateVM64-2", "vdom": "test"}
{"name": "FortiGateVM64-3", "vdom": "root"}]
:param script_name: Specify the script name that should be executed on the specified devices
"""

session = self.login()
payload = \
{
"method": "exec",
"params": [{
"data": {"adom": self.adom,
"scope": devices,
"script": script_name},
"url": "/dvmdb/adom/root/script/execute"}],
"session": self.sessionid
}
run_script = session.post(
url=self.base_url, json=payload, verify=self.verify)
return run_script.json()["result"]

def run_script_on_single_device(self, script_name: str, device_name: str, vdom: str):
"""
Create a script template and store it on FortiManager
:param device_name: Specify device name.
:param vdom: Specify the Vdom
:param script_name: Specify the script name that should be executed on the specified devices
"""

session = self.login()
payload = \
{
"method": "exec",
"params": [{
"data": {"adom": self.adom,
"scope": {"name": device_name, "vdom": vdom},
"script": script_name},
"url": "/dvmdb/adom/root/script/execute"}],
"session": self.sessionid
}

run_script = session.post(
url=self.base_url, json=payload, verify=self.verify)
return run_script.json()["result"]

def backup_config_of_fortiGate_to_tftp(self, tftp_ip, path, filename, device_name, vdom="root"):
"""
A small function to backup configuration on FortiGates from FortiManager and store it in TFTP Server.
:param tftp_ip: Specify TFTP Server IP
:param path: Specify the path to store the config
:param filename: Specify the name of the backup file
:param device_name: Specify the name of the device
:param vdom: Specify the Vdom
"""
result = []
script_name = "backup_config_script"
full_path = normpath(join(path, filename)).replace("\\", "/")
cli_command = f"execute backup config tftp {full_path} {tftp_ip}"
logging.info("Creating a Script Template in FortiManager")
result.append(
{"backup_script_template_creation_result": self.create_script(name=script_name,
script_content=cli_command, target=1)})
result.append({"backup_script_execution_result": self.run_script_on_single_device(script_name=script_name,
device_name=device_name,
vdom=vdom
),
"device": device_name, "vdom": vdom})
return json.dumps(result, indent=4)

0 comments on commit 9c168ef

Please sign in to comment.