If you've spent any time configuring user authentication on... Full Story
By Manny Fernandez
October 27, 2019
SED and AWK for scripting configs
Let me start by saying that I did not write this code, but I have used it for many years. Essentially, you can get a CSV file loaded with information and then iterate through the CSV and replace variables with content from CSV. To give you an example, I provided the networking and security for the 2012 Presidential debate. At the time, I was a Cisco junky. We needed to configure hundreds of access switches. Instead of connecting to each switch and adding the configs, I created a template file containing the basics and used variables for: VLAN IDs, 3rd Octet for IP Addresses, Network statements on BGP, hostnames, etc. I will break down the scrip here and will post on my github for you to use.
Although this script show a basic Fortinet Fortigate address object template, I have personally used this same script, modified obviously, for very, very complex configs.
You can also check out my buddies blog site that also has a powershell version of a similar script.
Source File
The source file is a CSV format that will contain different information used to create the config file (e.g. hostnames, VLAN IDs, etc.). Although I tend to NOT use extensions for the file names, you may want to add an extension (e.g. .csv, .txt, etc). Below is the contents of my example.
Host-92.241.165.127,92.241.165.127 Host-219.94.198.78,219.94.198.78 Host-69.16.180.34,69.16.180.34 Host-74.92.135.109,74.92.135.109 Host-80.177.183.2,80.177.183.2 Host-60.234.251.122,60.234.251.122
Template File
This is what the config output SHOULD look like but with VAR labels that will come into play when you see the script. Below is the contents of my example.
edit "VAR_HOSTNAME" set subnet VAR_IPADDR 255.255.255.255 next
Output
This is the ready-to-use output once the script has successfully run. (Shown later in the post).
The Script
This is a bash script and it used sed and awk to do the heavy lifting.
#!/bin/bash
IMPORT="/Users/mannyfernandez/Desktop/Script/source"
TEMPLATE="/Users/mannyfernandez/Desktop/Script/template"
for i in `cat ${IMPORT}`
do
VAR_HOSTNAME=`echo $i | awk -F, '{print $1}'`
VAR_IPADDR=`echo $i | awk -F, '{print $2}'`
cat $TEMPLATE | sed -e s/VAR_HOSTNAME/$VAR_HOSTNAME/g \
-e s/VAR_IPADDR/$VAR_IPADDR/g \
| tee -a ///Users/mannyfernandez/Desktop/Script/output 1>/dev/null
done
In the script, you will see the IMPORT line that shows the source file to use. The second line or the TEMPLATE line shows the file that will be used as a template.
The other important piece to remember is the section in the curly brackets or {} . This print $1 statement tell the script what column to use to populate the corresponding VAR_ variable.
NOTE: To have it create a separate file for each iteration, remove the -a from the
| tee -a ///Users/mannyfernandez/Desktop/Script/output 1>/dev/null done
command above.
A little bit about SED and AWK. Sed
SED
sed refers to Stream Editor. It can perform text transformations on a given file or an input stream.
AWK
awk is a text pattern scanning and processing language, which is created by Aho, Weinberger & Kernighan. awk is mostly used for data extraction and reporting (dealing with .csv files).
I got a great PDF eBook from a great resource www.thegeekstuff.com . He has a great ebook on SED and AWK.

Making Script Executable
By default, when you create a script file, it will not be executable, meaning you cannot run the command from the command line with the ./ pre-seeding command.

Notice the rights associated with the script file.
We will now make the file executable from the cli. To do this, we are going to go back to the CLI and enter the following command:
chmod +x import-script.sh
Here we can see that we ran the chmod command and when we ran the ls -all we can now see that there is a x in the output.

Running the Script
To run the script, simply shell out to the CLI again and type:
./import-script.sh

When you run the script, you should not see any errors. If you do, you should make sure that you are escaping any . or other special characters.
The Output
Here we see the output
edit "Host-92.241.165.127" set subnet 92.241.165.127 255.255.255.255 next edit "Host-219.94.198.78" set subnet 219.94.198.78 255.255.255.255 next edit "Host-69.16.180.34" set subnet 69.16.180.34 255.255.255.255 next edit "Host-74.92.135.109" set subnet 74.92.135.109 255.255.255.255 next edit "Host-80.177.183.2" set subnet 80.177.183.2 255.255.255.255 next edit "Host-60.234.251.122" set subnet 60.234.251.122 255.255.255.255 next
Recent posts
-
-
DNS is one of those technologies that quietly underpins... Full Story
-
BGP issues on FortiGate firewalls usually trace back to... Full Story
-
Every time your laptop talks to your router, a... Full Story
-
If you've spent any time configuring NAT on a... Full Story
-
If you have spent any time configuring firewall policies... Full Story
-
High availability on FortiGate is one of those features... Full Story
-
If you've configured SD-WAN on a FortiGate, you've almost... Full Story
-
FortiLink is the management protocol that turns a FortiSwitch... Full Story
-
FortiSwitches are pretty rock solid from Mean Time Between... Full Story
-
This is a quicky tip. Have you ever gone... Full Story
-
DNS is one of those quiet pieces of internet... Full Story
-
This article is an updated version of the previous... Full Story
-
You will add ns2 as a secondary (slave) BIND9... Full Story
-
In the process of deploying my lab, I needed... Full Story
-
RFC 8805, used to be known as Self-Correcting IP... Full Story
-
Years back, I wrote an article about certificate pinning. ... Full Story
-
FortiGates have the ability to send alerts to Microsoft... Full Story
-
In this post, I am going to walk through... Full Story
-
Troubleshooting VoIP on a FortiGate can feel like trying... Full Story
-
Prior to FortiOS 7.0, there were three commands to... Full Story
-
In this post, I am going to go over... Full Story
-
What we are going to do: We are going... Full Story
-
Choosing between FGCP (FortiGate Clustering Protocol) and FGSP (FortiGate... Full Story
-
Creating a VLAN on macOS (The "Pro" Move) A... Full Story
-
This blog post explores the logic behind how macOS... Full Story
-
Pretty Fly for a Wi-Fi Tell My Wi-Fi Love... Full Story
-
Part of my daily gig is creating BoMs (Bill-of-Materials)... Full Story
-
ICMP introduces several security risks, but careful filtering, rate... Full Story
-
The command diag debug application dhcps -1 enables full... Full Story
-
In the world of FortiOS, execute tac report is... Full Story
-
LLDP; What is it The Link Layer Discovery Protocol... Full Story
-
What it actually does When you run diagnose fdsm... Full Story
-
Monkey Bites are bite-sized, high-impact security insights designed for... Full Story
-
I have run macOS in macOS with Parallels but... Full Story
-
Don't be confused with my other FortiNAC posts where... Full Story
-
This is the third session in a multi-part article... Full Story
-
Today I was configuring key-based authentication on a FortiGate... Full Story
-
Netcat, often called the "Swiss Army knife" of networking,... Full Story
-
At its core, IEEE 802.1X is a network layer... Full Story
-
In case you did not see the previous FortiNAC... Full Story
-
This is our 5th session where we are going... Full Story
-
Now that we have Wireshark installed and somewhat configured,... Full Story
-
The Philosophy of Packet Analysis Troubleshooting isn't about looking... Full Story
-
Overview FortiOS 8.0 introduces custom tags as a first-class... Full Story
-
These are two distinct mechanisms on FortiOS, and conflating... Full Story
-
Replacement messages are the pages and text blocks that... Full Story