Working with Scripts : Script samples : TCL scripts : TCL file IO
 
TCL file IO
You can write to and read from files using TCL scripts. For security reasons there is only one directory on the FortiManager where scripts can access files. For this reason, there is no reason to include the directory in the filename you are accessing. For example “/var/temp/myfile” or “~/myfile” will cause an error, but “myfile” or “/myfile” is OK.
The TCL commands that are supported for file IO are: file, open, gets, read, tell, seek, eof, flush, close, fcopy, fconfigure, and fileevent.
The TCL file command only supports delete subcommand, and does not support the -force option.
There is 10 MB of diskspace allocated for TCL scripts. An error will be reported if this size is exceeded.
These files will be reset when the following CLI commands are run: exec format, exec reset partition, or exec reset all. The files will not be reset when the firmware is updated unless otherwise specified.
To write to a file
Script
1
2
3
4
5
6
 
#!
 
set somefile {open “tcl_test” “w”}
puts $somefile "Hello, world!"
close $somefile
 
Output
 
 
Variations
 
 
Versions
 
4.0
To read from a file
Script
1
2
3
4
5
 
7
8
 
#!
 
set otherfile {open “tcl_test” “r”}
while {[gets $otherfile line] >= 0} {
puts [string length $line]
}
close $otherfile
 
 
Output
 
Hello, world!
Variations
 
 
These two short scripts write a file called tcl_test and then read it back.
Line 3 in both scripts opens the file either for reading (r) or writing (w) and assigns it to a filehandle (somefile or otherfile). Later in the script when you see these filehandles, its input or output passing to the open file.
When reading from the file, lines 4 and 5 loop through the file line by line until it reaches the end of the file. Each line that is read is put to the screen.
Both scripts close the file before they exit.