edit HOST-1 set type ipmask set subnet 111.111.111.111/255.255.255.255 set associated-interface OUTSIDE next edit HOST-2 set type ipmask set subnet 222.222.222.222/255.255.255.255 set associated-interface OUTSIDE next edit HOST-3 set type ipmask set subnet 333.333.333.333/255.255.255.255 set associated-interface OUTSIDE end
Solved! Go to Solution.
Nominating a forum post submits a request to create a new Knowledge Article based on the forum post topic. Please ensure your nomination includes a solution within the reply.
Bob - self proclaimed posting junkie!
See my Fortigate related scripts at: http://fortigate.camerabob.com
@echo off REM input: textfile addr.txt with IP,name,interface (one per line) REM values delimited by commas, comments start with # REM redirect output to a batch command file for uploading to a Fortigate echo config firewall address for /f " eol=# tokens=1-3 delims=," %%i in (addr.txt) do CALL :oneaddr %%i %%j %%k echo end goto :EOF :oneaddr echo edit %2 echo set type ipmask echo set subnet %1/32 set intf=%3 if [%3]==[] set intf=ANY echo set associated-interface %intf% echo nextwith this input file
# IP,Hostname,Interface 111.111.111.111,HOST-1,OUTSIDE 222.222.222.222,HOST-2 333.333.333.333,HOST-3,OUTSIDEthis output is produced:
config firewall address edit HOST-1 set type ipmask set subnet 111.111.111.111/32 set associated-interface OUTSIDE next edit HOST-2 set type ipmask set subnet 222.222.222.222/32 set associated-interface ANY next edit HOST-3 set type ipmask set subnet 333.333.333.333/32 set associated-interface OUTSIDE next end
hi,
step-by-step on a Windows PC:
assuming you copied and pasted my batch script into notepad and saved that as "mkadr.cmd".
Then you write down your addresses in notepad and save that as "addr.txt".
- this name is fixed! the script expects only this name, you cannot change it. -
Then you open a commandline: press the Windows key (lower left of keyboard, between Ctrl and Alt), and type "cmd.exe" into the search field. A DOS box/command line window should open.
Go into the directory where you saved the 2 files: cd "C:\users\blabla\downloads"
You should be able to list these files: "dir mkadr.cmd", "dir addr.txt"
Now generate the batchcommands for the Fortigate: "mkadr > newadr.bcmd"
Check the file: "dir newadr.bcmd", filesize should be > 0.
To upload to the Fortigate, in the GUI go to System > Config > Advanced, Scripts and upload the file.
Afterwards check the address objects in Firewall Objects > Addresses.
Bob - self proclaimed posting junkie!
See my Fortigate related scripts at: http://fortigate.camerabob.com
rwpatterson wrote:
That doesn' t look to be so difficult. You would still have to manually upload that into your unit though.
is the script below supposed to be run on the FGT unit or the windows PC and why?
@echo off
REM input: textfile addr.txt with IP,name,interface (one per line)
REM values delimited by commas, comments start with #
REM redirect output to a batch command file for uploading to a Fortigate
echo config firewall address
for /f " eol=# tokens=1-3 delims=," %%i in (addr.txt) do CALL :oneaddr %%i %%j %%k
echo end
goto :EOF
:oneaddr
echo edit %2
echo set type ipmask
echo set subnet %1/32
set intf=%3
if [%3]==[] set intf=ANY
echo set associated-interface %intf%
echo next
@echo off REM input: textfile addr.txt with IP,name,interface (one per line) REM values delimited by commas, comments start with # REM redirect output to a batch command file for uploading to a Fortigate echo config firewall address for /f " eol=# tokens=1-3 delims=," %%i in (addr.txt) do CALL :oneaddr %%i %%j %%k echo end goto :EOF :oneaddr echo edit %2 echo set type ipmask echo set subnet %1/32 set intf=%3 if [%3]==[] set intf=ANY echo set associated-interface %intf% echo nextwith this input file
# IP,Hostname,Interface 111.111.111.111,HOST-1,OUTSIDE 222.222.222.222,HOST-2 333.333.333.333,HOST-3,OUTSIDEthis output is produced:
config firewall address edit HOST-1 set type ipmask set subnet 111.111.111.111/32 set associated-interface OUTSIDE next edit HOST-2 set type ipmask set subnet 222.222.222.222/32 set associated-interface ANY next edit HOST-3 set type ipmask set subnet 333.333.333.333/32 set associated-interface OUTSIDE next end
ede_pfau wrote:
here you are with a rudimentary batch script:@echo offwith this input file
REM input: textfile addr.txt with IP,name,interface (one per line)
REM values delimited by commas, comments start with #
REM redirect output to a batch command file for uploading to a Fortigate
echo config firewall address
for /f " eol=# tokens=1-3 delims=," %%i in (addr.txt) do CALL :oneaddr %%i %%j %%k
echo end
goto :EOF
:oneaddr
echo edit %2
echo set type ipmask
echo set subnet %1/32
set intf=%3
if [%3]==[] set intf=ANY
echo set associated-interface %intf%
echo next# IP,Hostname,Interface 111.111.111.111,HOST-1,OUTSIDE 222.222.222.222,HOST-2 333.333.333.333,HOST-3,OUTSIDEthis output is produced:config firewall address edit HOST-1 set type ipmask set subnet 111.111.111.111/32 set associated-interface OUTSIDE next edit HOST-2 set type ipmask set subnet 222.222.222.222/32 set associated-interface ANY next edit HOST-3 set type ipmask set subnet 333.333.333.333/32 set associated-interface OUTSIDE next end
Hey thanks for this, just need lil help on transferring it to a txt file. I tried the filename > bulk.txt in your next reply but couldn't get it to work. Where do i add this line?
ede_pfau wrote:
here you are with a rudimentary batch script:@echo offwith this input file
REM input: textfile addr.txt with IP,name,interface (one per line)
REM values delimited by commas, comments start with #
REM redirect output to a batch command file for uploading to a Fortigate
echo config firewall address
for /f " eol=# tokens=1-3 delims=," %%i in (addr.txt) do CALL :oneaddr %%i %%j %%k
echo end
goto :EOF
:oneaddr
echo edit %2
echo set type ipmask
echo set subnet %1/32
set intf=%3
if [%3]==[] set intf=ANY
echo set associated-interface %intf%
echo next# IP,Hostname,Interface 111.111.111.111,HOST-1,OUTSIDE 222.222.222.222,HOST-2 333.333.333.333,HOST-3,OUTSIDEthis output is produced:config firewall address edit HOST-1 set type ipmask set subnet 111.111.111.111/32 set associated-interface OUTSIDE next edit HOST-2 set type ipmask set subnet 222.222.222.222/32 set associated-interface ANY next edit HOST-3 set type ipmask set subnet 333.333.333.333/32 set associated-interface OUTSIDE next end
what do you mean rudimentary? where do I run this script? the pc or the Fortigate unit? I have over 200 ip addresses I need to add to different vdoms on my unit, do you have any idea how I could go about this?
This is a Windows script, run in a command line window (cmd.exe).
If you have to ask, you probably can't change it - which would be necessary to adopt it to VDOMs.
'Rudimentary' because it's so simple. On a PC because at that time there was no scripting on a FGT. IMHO there's still not a decent shell in FOS.
Just create the output and upload it to the FGT via 'batch command'.
Im thinking I may need edit the script since your script only has provision for 3 addresses, right?...
I wanted to understand the mechanism, run a windows script using cli and it operates in the Fortigate, which means I have to be logged in to the FGT before running this script right?
No, not at all! Please re-read my posts...
This script is run on a Win PC. The output file (a text file) can be uploaded to the FGT via System > Advanced > Batch command. You will need admin access to the FGT for this, but not for the file creation.
You need to supply the 200 addresses in a CSV file (comma separated values), that is a text file where you put "ip address", comma, "hostname" on one line, with one line per host. My example just held only 3 addresses so not to bore the audience.
My go-to these days for this type of task is powershell. I create a "template" of what I want the commands to look like, and then import my CSV values and create the appropriate commands. I can then copy the commands out of the powershell output results, and paste them into my CLI session to the FGT.
For this task, here's my source command and for-loop that goes thru each line in the CSV file (my example uses c:\temp\test-fgt.csv) and substitutes the values and outputs the string.
$Sourcecommandtext = " edit `"%hostname%`" set type ipmask set associated-interface %interface% set subnet %IPAddress%/32 next " foreach ($line in (import-csv -path c:\temp\test-fgt.csv )) { $commandstext = $sourcecommandtext -replace "%hostname%",$line.hostname $commandstext = $commandstext -replace "%interface%",$line.interface $commandstext = $commandstext -replace "%IPAddress%",$line.IP $commandstext }The output of this is:
edit "host-1" set type ipmask set associated-interface OUTSIDE set subnet 111.111.111.111/32 next edit "host-2" set type ipmask set associated-interface OUTSIDE set subnet 222.222.222.222/32 next edit "Host-3" set type ipmask set associated-interface OUTSIDE set subnet 333.333.333.333/32 next
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 |
---|---|
1713 | |
1093 | |
752 | |
447 | |
231 |
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.