By Manny Fernandez

November 14, 2019

Search and Add Line Script

The other day, a friend called me about a Cisco ASA configuration he migrated to Fortigate. They needed to add over 347 address objects to a group that could be used in an address group.  They wanted to then use that group in a static route.  The problem is that each address object needs to have the

set allow-routing enable

turned on. In order for the group to be able to turn on the feature.  Obviously, every member of the group has to have the set allow-routing enable turned on in order to enable it globally on the group object.

2019-11-14_09-38-01.png

From the GUI, the Static Route Configuration button is what adds the set allow-routing enabled to the command line.

2019-11-14_09-39-00.png

To show you how this looks, I created a test address object in my lab.  As you can see the name is InfoSecMonkey-Test & I have the Static Route Configuration selected.

2019-11-14_09-40-03.png

Now when you create a new static route, you can:

  1. Select the Named Address button on the top which will allow you to search the address objects with the Static Route Configuration selected.
  2. The drop-down shows the InfoSecMonkey-Test object I created.
  3. The destination interface; you will also need to add a gateway IP.
  4. Make sure it is enabled.

The Source File

Here is what the source file looks like

 edit "Spoke2-LAN"
set uuid 379d4b0e-ce5e-51e9-af32-5ead2c57a463
set color 6
set subnet 10.3.3.0 255.255.255.0 
set color 6
next
edit "Hub-LAN"
set uuid 4556e6f6-ce5e-51e9-77d2-8998088099fa
set color 18
set subnet 10.1.1.0 255.255.255.0 
set color 6
next
edit "Spoke1-LAN"
set uuid d7ef66fa-ce5e-51e9-81f4-3000592203be
set color 6
set subnet 10.2.2.0 255.255.255.0 
set color 6
next 
edit "SEC-LAN-1"
set uuid 2fb047fa-0042-51ea-ccf2-beae0044ff87
set color 18
set subnet 10.221.1.0 255.255.255.0 
set color 6
next 
edit "SEC-LAN-2"
set uuid 37103afa-0042-51ea-659c-2c2ceb431e0d
set color 18
set subnet 10.222.1.0 255.255.255.0 
set color 6
next 
edit "ASA-LOCAL-LAN"
set uuid 8bd0e474-0153-51ea-89d6-d4877711f1bc
set color 6
set subnet 2.2.2.0 255.255.255.0 
set color 6
next 
end

As you can see, there is no set allow-routing enable on the output above.

The Script

Here is the script

#!/bin/bash
#
#

tab_spacing="\\ \\ \\ \\ \\ \\ \\ \\ " 
command_to_add="set allow-routing enable"
command_to_add="${tab_spacing}${command_to_add}"

filename="$1"
#macOS sed really needs a NEWLINE character before and after the command you want to add!
sed -i '' "/^.*set subnet .* 255.255.255.0/a\\
${command_to_add}
" "$filename"

#If you want to add more commands, just copy the part and change the command.
#
#command_to_add="MY NEW COMMAND"
#command_to_add="${tab_spacing}${command_to_add}"
#sed -i '' "/^.*set subnet .* 255.255.255.0/a\\
#${command_to_add}
#" "$filename"

Here we can see the script.  We are searching for the line that contains set subnet .* 255.255.255.0 and we are going to add the line set allow-routing enable. You can modify the script to look for any pattern inside the file and replace it with the command_to_add.

How To Run The Script

To run the script, I normally make the script executable by running the following command:

chmod +x script.sh

Now we will run the script against the file

./script.sh Before.txt

2019-11-14_09-59-54.png

Call the script and tell it the name of the file you want to run the script against.

The Output

Here is the output from the command above.  You can now see the set allow-routing enable

 edit "Spoke2-LAN"
set uuid 379d4b0e-ce5e-51e9-af32-5ead2c57a463
set color 6
set subnet 10.3.3.0 255.255.255.0 
set allow-routing enable
set color 6
next
edit "Hub-LAN"
set uuid 4556e6f6-ce5e-51e9-77d2-8998088099fa
set color 18
set subnet 10.1.1.0 255.255.255.0 
set allow-routing enable
set color 6
next
edit "Spoke1-LAN"
set uuid d7ef66fa-ce5e-51e9-81f4-3000592203be
set color 6
set subnet 10.2.2.0 255.255.255.0 
set allow-routing enable
set color 6
next 
edit "SEC-LAN-1"
set uuid 2fb047fa-0042-51ea-ccf2-beae0044ff87
set color 18
set subnet 10.221.1.0 255.255.255.0 
set allow-routing enable
set color 6
next 
edit "SEC-LAN-2"
set uuid 37103afa-0042-51ea-659c-2c2ceb431e0d
set color 18
set subnet 10.222.1.0 255.255.255.0 
set allow-routing enable
set color 6
next 
edit "ASA-LOCAL-LAN"
set uuid 8bd0e474-0153-51ea-89d6-d4877711f1bc
set color 6
set subnet 2.2.2.0 255.255.255.0 
set allow-routing enable
set color 6
next 
end

 

Hope this helps

 

Recent posts