Description | This article describes another way on how to get the backup configuration file on FortiGate using HTTPS RestAPI calls from a Python script. |
Scope | FortiGate. |
Solution |
config system api-user
Import requests:
import requests api_url = 'https://10.191.20.122/api/v2/monitor/system/config/backup?scope=global&access_token=Api_Key_Generate...'
requests.packages.urllib3.disable_warnings() data = requests.get(api_url, verify=False) with open('/home/api_configbackup.conf' ,'wb') as f: for line in data: f.write(line)
Where 10.191.20.122 is the IP of the FortiGate.
For FortiOS 7.4.5 + the authentication needs to be passed in the header, and Api_Key_Generated needs to be moved from the link inside the get request header. Script will be:
import requests api_url = 'https://10.191.20.122/api/v2/monitor/system/config/backup?scope=global' requests.packages.urllib3.disable_warnings() with open('/home/api_configbackup.conf' ,'wb') as f: for line in data: f.write(line)
The scope is global for the global configuration of the FortiGate.
Api_Key_Generated is the value of the token we previously generated. /home/api_configbackup.conf is the place in the Linux machine where to save the backup file.
The file api_configbackup will be created in the specified directory with the configurations of the Fortigate included.
For chassis-based products (such as 6K - 7K) it may be necessary to set the scope to Global in the account profile to make this work as expected:
config system accprofile |