Technical Tip: Creating, deleting and execute of CLI script with API calls under FortiManager
Description
Â
This article describes how to use API calls to create, execute, and delete a CLI script from FortiManager.
Â
Scope
Â
FortiManager.
Â
Solution
Â
To create a session with an API user.
Â
{
"id": 1,
"method": "exec",
"params": [
{
"data": {
"user": "APIuser",
"passwd": "APIpass"
},
"url": "/sys/login/user"
}
]
}Â
To create a CLI script to device database.
Â
{
"id": 1,
"method": "set",
"params": [
{
"data": {
"target": "device_database",
"type": "cli",
"name": "AT-FW_411651021",
"content": "#1_CREATE_VLAN_IFACE
config system interface
edit VLAN2003
set vdom 'root'
set ip 10.91.3.254 255.255.255.0
set allowaccess ping
end
#END"
},
"url": "/dvmdb/adom/root/script"
}
],
"session": "8GdESeYyTt56Vm9=="
}Â
Possible targets are:
Â
device_database | Device Database. |
adom_database | Policy Package or ADOM Database. |
remote_device | Remote FortiGate Directly (via CLI). |
Â
In FortiManager v7.6.5 and higher, the create script request has changed:
Â
{
"id": 2,
"method": "set",
"params": [
{
"url": "/pm/config/adom/{{ADOM}}/obj/fmg/script",
"data": {
"content": "<script content>",
"name": "test_script",
"type": "cli", //or "jinja" or "tcl"
"target": "devicedb"
}
}
],
"session": "{{session}}"
}Â
Possible targets include:
Â
devicedb | Device Database. |
adomdb | Policy Package or ADOM Database. |
remote | Remote FortiGate Directly (via CLI). |
Â
To run the script against a device.
Â
{
"id": 1,
"method": "exec",
"params": [
{
"data": {
"adom": "root",
"package": "",
"scope": [
{
"name": "AT-FW",
"vdom": "root"
}
],
"script": "AT-FW_411651021"
},
"url": "/dvmdb/adom/root/script/execute"
}
],
"session": "8GdESeYyTt56Vm97=="
}Â
To run it against many devices, set the scope like this:
Â
"scope": [
{
"name": "dev_001",
"vdom": "global"
},
{
"name": "dev_002",
"vdom": "global"
}
]Â
To run it against a policy package.
Â
{
"data": {
"adom": "root",
"package": "pkg_001",
"script": "script_001"
}
}Â
To delete a script.
Â
{
"id": 1,
"method": "delete",
"params": [
{
"url": "/dvmdb/adom/root/script/",
"confirm": 1,
"filter": [
"name",
"in",
"AT-FW_411651021"
]
}
],
"session": "8GdESeYyTt56Vm97=="
}Â
To review the task that was created for the script's execution.
Â
{
"id": 1,
"method": "get",
"params": [
{
"url": "/task/task/79222"
}
],
"session": "8GdESeYyTt56Vm97=="
}Â
To view the Script Execution Summary of the script:
Â
First, obtain the log_id:
Â
{
"method": "get",
"params": [
{
"url": "/dvmdb/adom/{{ADOM}}/script/log/summary/device/{{FGT}}"
}
],
"id": "1",
"session": "8GdESeYyTt56Vm97=="
}Â
The output of the request will look similar to the following:
Â
{
"result": [
{
"data": [
{
"exec_time": "Wed Jan 28 15:15:50 2026",
"log_id": 135210,
"script_name": "Test_Script",
"seq": 1
},Â
Extract the latest 'log_id' (in this example, it is 135210).
Â
To view the results of the script execution, use the log_id to fetch the script log:
Â
{
"method": "get",
"params": [
{
"url": "/dvmdb/adom/{{ADOM}}/script/log/output/device/{{FGT}}/logid/135210"
}
],
"id": "1",
"session": "8GdESeYyTt56Vm97=="
}Â
To log out of a session, run the following:
Â
{
"id": 1,
"method": "exec",
"params": [
{
"url": "/sys/logout"
}
],
"session": "8GdESeYyTt56Vm97=="
}