Hi everyone
I am trying to use the REST API to add IP addresses on our Firewall.
I have this python code:
import os
import requests
import urllib3
import getpass
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
baseURL = "REDACTED URL"
apiURL = "/api/v2/cmdb/firewall/address"
token = "REDACTED TOKEN"
headers = {'Authorization':'Bearer ' + token,}
params = {
"datasource":1,
"vdom": "root"
}
def createNewIpAddressData(ipAddress):
data = {
"name":"ext-" + ipAddress,
"subnet":ipAddress + "/32",
"comment": "New IP",
"color":"0"
}
return data
def addNewIpAddress(postData):
try:
r = requests.post(baseURL + apiURL, headers=headers, params=params, verify=False, data=postData, timeout=5)
except requests.exceptions.Timeout:
print("Timeout!")
return None
if r.status_code != 200:
print("Error code:{}".format(r.status_code))
print(r.content)
return None
return r.json
result = addNewIpAddress(createNewIpAddressData("5.153.183.99"))
print (str(result))
When I execute this, I receive this error with the htpp_status 424:
"http_method":"POST",\n "revision":"REDACTED",\n "revision_changed":false,\n "status":"error",\n "http_status":424,\n "vdom":"root",\n "path":"firewall",\n "name":"address",\n "serial":"REDACTED",\n "version":"v7.0.12",\n "build":523\n
I've used the API code from my FortiGate and according to the documentation, it says that it misses some dependencies, but I can't figure out what it misses.
Can someone point me in the right direction?
Thanks.
Solved! Go to Solution.
Ok I found the issue. I had to encode it to json before passing it to the post request.
Here the updated code, if someone needs it.
import os
import requests
import urllib3
import getpass
import json
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
baseURL = "REDACTED"
apiURL = "/api/v2/cmdb/firewall/address"
token = "REDACTED"
headers = {'Authorization':'Bearer ' + token,}
params = {
"datasource":1,
"vdom": "root"
}
def createNewIpAddressData(ipAddress):
data = [{
"name":"ext-" + ipAddress,
"subnet":ipAddress + "/32",
"comment": "New IP",
"color":"0"
}]
jsondata = json.dumps(data)
return jsondata
def addNewIpAddress(postData):
try:
r = requests.post(baseURL + apiURL, headers=headers, params=params, verify=False, data=postData, timeout=5)
except requests.exceptions.Timeout:
print("Timeout!")
return None
if r.status_code != 200:
print("Error code:{}".format(r.status_code))
print(r.content)
return None
return r.json
result = addNewIpAddress(createNewIpAddressData("5.153.183.99"))
print (str(result))
Ok I found the issue. I had to encode it to json before passing it to the post request.
Here the updated code, if someone needs it.
import os
import requests
import urllib3
import getpass
import json
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
baseURL = "REDACTED"
apiURL = "/api/v2/cmdb/firewall/address"
token = "REDACTED"
headers = {'Authorization':'Bearer ' + token,}
params = {
"datasource":1,
"vdom": "root"
}
def createNewIpAddressData(ipAddress):
data = [{
"name":"ext-" + ipAddress,
"subnet":ipAddress + "/32",
"comment": "New IP",
"color":"0"
}]
jsondata = json.dumps(data)
return jsondata
def addNewIpAddress(postData):
try:
r = requests.post(baseURL + apiURL, headers=headers, params=params, verify=False, data=postData, timeout=5)
except requests.exceptions.Timeout:
print("Timeout!")
return None
if r.status_code != 200:
print("Error code:{}".format(r.status_code))
print(r.content)
return None
return r.json
result = addNewIpAddress(createNewIpAddressData("5.153.183.99"))
print (str(result))
Select Forum Responses to become Knowledge Articles!
Select the “Nominate to Knowledge Base” button to recommend a forum post to become a knowledge article.
User | Count |
---|---|
1737 | |
1107 | |
752 | |
447 | |
240 |
The Fortinet Security Fabric brings together the concepts of convergence and consolidation to provide comprehensive cybersecurity protection for all users, devices, and applications and across all network edges.
Copyright 2024 Fortinet, Inc. All Rights Reserved.