Hi community, I've been working on a script that check if an iptables file has erros, in case the script has errors, the script will tell you where the error is.
I know that you have iptables-save and iptables-restore, but if you are like me that you have many iptables files for differents customers, with comments and change request numbers in it. You may want to check that specific file, and not a auto generated file.
I would like to share this, and if anyone want to help me to improve this, very welcome to it.
Please be nice!
Code:
#! /bin/bash
## Autor : Rodrigo Tumaian - rodrigo.tumaian (AT_N0_SP4m) gmail
## Ano : Marzo 2015
## Version : 1.0
## GPLv3
##
####
## I had to give something back to the community from where I have learned so much.
####
##
############################################## Some considerations #################################
### This software comes with no guarantee
###
### This script was made using OpenSuse and Ubuntu, for all my iptables files.
### In all my test this script worked very well, maybe in other GNU/Linux distribution it doesn't.
###
### Please send me any kind of bug you find, or any collaboration to improve this script.
###
### This script does not support the use of “while” but can be easily adapted to make it work.
###
############################################## IMPORTAN #############################################
## Please run this script using nohup, you don’t wanna run iptables -F while you are using ssh :P ##
## Example: nohup ./test_filler > test_filter.out
## To check where the errors are in a easy way: grep Error test_filter.out
##
PATH_GRL=/etc
PATH_FILTER=$PATH_GRL/iptables-filters
LOOP_TMP=$PATH_GRL/.bucle
VARIABLES=$PATH_GRL/.variables
RUN_BLOCK=0
LINE_COUNTER=0;
CANT_FOR=0;
## I do this crazy thing, because I wanna make sure that the file is empty
## This is the easy solution
touch $LOOP_TMP
rm -f $LOOP_TMP
touch $LOOP_TMP
chmod +x $LOOP_TMP
touch $VARIABLES
rm -f $VARIABLES
touch $VARIABLES
chmod +x $VARIABLES
GET_IN_LOOP=0
##
## Here I find the vars in the filter file and try define them as global to make this work… I’m quite sure that must be a better solution
## Can we improve this?
for VAR in `grep \= $PATH_FILTER | grep -v ^# | grep -v LOG`; do
echo "export $VAR" >> $VARIABLES
done;
source $VARIABLES
while read LINEA; do
let LINE_COUNTER=$LINE_COUNTER+1;
# Avoid all the comments with this
if [ -z "`echo "$LINEA" | grep ^#`" ]; then
#find the “for “ and send them to a file
if [ -n "`echo "$LINEA" | grep "for "`" ]; then
GET_IN_LOOP=1
let CANT_FOR=$CANT_FOR+1;
echo "$LINEA" >> $LOOP_TMP
fi;
if (($GET_IN_LOOP)) && [ -n "`echo "$LINEA" | grep -v "for "`" ] && [ -n "`echo "$LINEA" | grep -v done`" ]; then
echo "$LINEA" >> $LOOP_TMP
fi;
if (($GET_IN_LOOP)) && [ -n "`echo "$LINEA" | grep done`" ]; then
echo "$LINEA" >> $LOOP_TMP
DONE=1;
let CANT_FOR=$CANT_FOR-1;
fi
if [ "$CANT_FOR" = "0" ] && (($DONE)); then
GET_IN_LOOP=0
RUN_BLOCK=1
fi;
## when the for is done, I run the code
if (($RUN_BLOCK)); then
echo
echo “We are going to run this code: "
cat $LOOP_TMP
$LOOP_TMP
if [ "$?" != "0" ]; then
echo "------------------------------------------------"
echo
echo "Error en Linea: $LINE_COUNTER"
echo "Linea: $LINEA"
echo
echo "------------------------------------------------"
fi;
GET_IN_LOOP=0
RUN_BLOCK=0;
rm -f $LOOP_TMP
touch $LOOP_TMP
chmod +x $LOOP_TMP
fi;
if ! (($RUN_BLOCK)) && ! (($GET_IN_LOOP)) && ! (($DONE)); then
eval $LINEA
if [ "$?" != "0" ]; then
echo "------------------------------------------------"
echo
echo "Error en Linea: $LINE_COUNTER"
echo "Linea: $LINEA"
echo
echo "------------------------------------------------"
fi;
fi;
# I don't remember why I added this :P
if [ "$?" != "0" ] && ! (($GET_IN_LOOP)) && ! (($DONE)); then
echo "------------------------------------------------"
echo
echo "Error en Linea: $LINE_COUNTER"
echo "Linea: $LINEA"
echo
echo "------------------------------------------------"
fi;
if (($DONE)); then
DONE=0;
fi;
fi;
done < $PATH_FILTER;
Hope this could be useful for somebody.
Kindly Regards,
Rodrigo
Bookmarks