Technical Tip: Automated scripts (auto-script). Execution, testing and verification explained with examples
Description
This article describes how to use the automated scripting on FortiGate.
Scope
FortiGate.
Solution
In FortiOS, it is possible to configure auto-scripts. This feature can be used for various purposes.
Important notes:
The auto-script output is stored in the RAM, so if running multiple scripts with a maximum of 10MB (set output-size), calculate and monitor the RAM usage. Improper use of the auto-script may trigger a conserve mode.
The output is also permanently stored on the flash memoryÂ
Some commands will not work with the auto-script on older firmware versions.
Support for further commands like 'diagnose test app xxx', 'diagnose wad xxx', 'diagnose ips xxx' was added in FortiOS v7.6.1 and above.
Â
If the output size is exceeded, the script will stop. Consider the auto-script as a temporary installation only; it is good for time-based troubleshooting.
Example 1: Auto-script for sending firewall configuration backup to a TFTP server:
Â
config system auto-script
   edit "backup"
       set interval 120
       set repeat 0
       set start auto
       set script "execute backup config ftp backup.conf 10.10.10.2 test test"
   next
end
In this example:
10.10.10.2 is the IP of the FTP server.
backup.conf is the name of the file.
test/test is the username and password of the FTP.
120 is the interval of time in seconds to execute the task.
0 is the time of repeats - an entry of 0 means "always". The default is 1.
'set start auto' means the process will be started by the system automatically.
Example 2: Auto-script for sending firewall configuration backup to an FTP server:
Â
config system auto-script
   edit "backup"
       set interval 120
       set repeat 0
       set start auto
       set script "
       config global
      execute backup config ftp backup.conf 10.10.10.2 test test"
   next
endÂ
Where:
10.10.10.2 is the IP of the FTP server.
backup.conf is the name of the file.
test/test is the username and password of the FTP.
Note:
It is possible to add multiple CLI commands in the auto-script.
Example 3: Checking the generic status of the device using multiple commands with the help of Auto-script:
Â
'get system status'Â and 'get system performance status'Â are the commands used for this.
Â
config system auto-script
  edit "status"
    set interval 300
    set repeat 0
    set start auto
    set script "         Â
    get system status          Â
    get system performance status"  Â
  next
end'set script "' <-- Press enter key here, add the first command.
'get system status'Â <-- Press the enter key here and add the second command in the next line.
'get system performance status"' Â <-- Make sure that the last command ends with a double quotation mark.
To check the script output stored in the file:
From the GUI:
Go to System -> Advanced -> Scheduled Script.
Select the 'Download' button from the 'Status' field for the selected script and open the file to read the output.

 Â
For a newer version:
This option is moved under Admin -> Configuration -> Scripts.

Â

Â
Note:
From v6.2.2, the System -> Advanced is removed; it is only possible to see the script scheduled via CLI. From v6.2.0, automation action is introduced as an alternative to auto-script.
From CLI.
To view the results of the script named 'status' (with no VDOMs).
Â
execute auto-script result status
Script status output:
########## script name: status ##########
========== #1, 2019-10-01 14:24:04 ==========
FGT $ get system status
Version: FortiGate-100D v6.2.1,build0932,190716 (GA)
Virus-DB: 72.00005(2019-10-01 03:19)
Extended DB: 1.00000(2018-04-09 18:07)
... output continues ...
To view the results of the script named 'status' (with VDOMs, enter it in global):
config global
execute auto-script result status
To stop or restart an auto-script, use the following CLI commands:
Â
execute auto-script start <script name>
execute auto-script stop <script name>Â
To stop all scripts, use the following CLI command:
Â
execute auto-script stopallÂ
To delete the results of an auto-script, use the following CLI command:
Â
execute auto-script delete <script name>Â
Modifying a running script will show an output (error) as shown below. So, the script must be stopped from running before making any modifications.
Â
Script can not be changed when it is running.
object set operator error, -14 discard the setting.
Command fail. Return code -14.
Â
Example 4: To get an FSSO user list every 5 seconds for a maximum size of 100MB, filtered for the IP 172.16.17.132.
Â
config system auto-script
  edit "firewall-user-list"
    set interval 5
    set repeat 0
    set script "diag firewall auth list | grep 172.16.17.132 -A 7"
      set output-size 100
   next
  edit "auth-user-list"
    set interval 5
    set repeat 0
    set script "diag debug auth fsso list | grep 172.16.17.132"
    set output-size 100
   next
endÂ
To start the above-created scripts, the following commands can be used:
execute auto-script start firewall-user-list
execute auto-script start auth-user-list  Â
Example 5:Â To get a session list every 10 seconds for the IP 10.10.10.48.
Â
config system auto-script
  edit "session-list"
    set interval 5
    set repeat 0
    set script "diag sys session filter src 10.10.10.48
         diagnose sys session list"
            set output-size 100
  next
endÂ
Example 6: Using an auto-script to restart the WAD process at a specific interval.
Â
config system auto-script
  edit "restart-wad"
    set interval 3600
    set repeat 65535
    set start auto
    set script "diag test app wad 99"
   next
endÂ
Example 7: Using auto-script to capture OSPF routing information.
Â
config system auto-script
  edit "OSPF_routing_script"
    set interval 5
    set repeat 50000
    set start auto
    set script "
    get sys stat
    get router info routing-table ospf
    get system arp
    get router info ospf neighbor
    "
endÂ
Example 8: Cleating DHCP lease using auto-script:
config system auto-script
  edit "clear_dhcp_lease"
    set interval 600
    set repeat 65535
    set start auto
    set script "
    execute dhcp lease-list
    execute dhcp lease-clear all
    "
  next
endÂ
Example 9: Clearing the device inventory list from FortiGate in a particular interval:
config system auto-script
  edit "clear_dhcp_lease"
    set interval 600Â
    set repeat 0
    set start auto
    set script "diagnose user device clear"
  next
endÂ
'set interval 600'Â <-- Interval of choice.
Example 10: Restarting and killing the IPS monitor process at a specific interval:
Â
config system auto-script
  edit "ipsrestart"
    set interval 43200 Â
    set repeat 0   Â
    set start autoÂ
    set script "         Â
    diagnose test application ipsmonitor 99
    fnsysctl killall ipsmonitor
"
  next
endÂ
'set interval 43200'Â <-- 12 hours.
'set repeat 0'Â Â Â Â Â Â Â Â <-- Infinite times, no limit.
'set start auto'Â Â Â Â Â Â <-- Automatic process by the system.
'set script "'Â Â Â Â Â Â Â Â <-- Script to restart the ipsmonitor.
Note:Â The 'diagnose test app xxx' commands might not work on older firmware versions when executed in the auto-script.
Â
In an HA setup, the auto-script configuration is automatically synchronised, so it only needs to be configured on the primary device. However, the script's command output is executed and captured individually on each firewall.
Refer to the screenshots below:Â

Â

Â
The auto-script will place temporary files on the disk.
To verify if the auto script is still running and also show the current size of the output of a specific script run:
Â
execute auto-script status
auto-script_test <-- Running, output file size: 567.3K.Â
When the configured max output size is reached, the script will stop and change the status from 'running' to 'executed'.
The command will then show the final file size.
Â
execute auto-script status
auto-script_test <-- Executed, output file size: 11.1M.Â
The temporary files of the auto-script are stored in '/tmp/$$auto-script$$/'.
The total size of all auto-script files can be checked with the following commands:
Â
fnsysctl df -k
fnsysctl df -h
fnsysctl ls -al /tmp/$$auto-script$$/
fnsysctl du -aLL /tmp/$$auto-script$$
Example outputs:
Â
fnsysctl df -h
Filesystem Size Used Available Use% Mounted on
none 1.4G 362.2M 1.0G 25% /tmp
... cut ...Â
fnsysctl ls -al /tmp/$$auto-script$$/
drwxr-xr-x 2 0 0 Tue Jan 7 10:22:52 2025 60 .
drwxrwxrwt 56 0 0 Tue Jan 7 10:30:14 2025 5060 ..
-rw-r--r-- 1 0 0 Tue Jan 7 10:22:48 2025 11617598 auto-script_test.out <- 11,08 MB.Â
fnsysctl du -aLL /tmp/$$auto-script$$
11348 /tmp/$$auto-script$$/auto-script_test.out
11348 /tmp/$$auto-script$$ <----- 11,08 MB.Â
Note:
The 'fnsysctl' command requires Super Admin access to execute. FortiGate will produce an error otherwise.
For further information, see Troubleshooting Tip: fnsysctl command returns Unknown action 0.
Note:
Starting from FortiOS v8.0.1-build 0185, the auto-script engine only allows information-gathering commands (get, diagnose); 'execute' commands are not permitted in auto-script. Thus, backup using the auto-script feature will fail with the message 'unknown action or command is unavailable in auto-script'.
Related documents:
Technical Tip: How to restart/kill all processes with the 'fnsysctl' command
Technical Tip: Configuring an automated script for daily FortiGate configuration backups
Technical Tip: How to clear the unit inventory list from FortiGate in particular interval
