Skip to content

Instantly share code, notes, and snippets.

@joho1968
Created September 16, 2021 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joho1968/9991ecefcc2473ecb25c40161619c4e4 to your computer and use it in GitHub Desktop.
Save joho1968/9991ecefcc2473ecb25c40161619c4e4 to your computer and use it in GitHub Desktop.
Simple (bash) script to check the number of iptables rules for Linux
#!/bin/bash
#
# Simple (bash) script to check the number of iptables rules for Linux
#
# You could run this at system start (possibly after a few second delay) or
# as a cron job every minute or so. On any given system, it's probably not
# a normal situation that there exist no (zero) iptables rules.
#
# Released to the Public Domain, Joaquim Homrighausen, 2021-09-16
# (If you break it, you own all the pieces.)
#
rules=$(iptables -n --list --line-numbers | sed '/^num\|^$\|^Chain/d' | wc -l)
if [[ $rules == "0" ]]; then
echo "No rules found"
#insert your command to restore iptables rules here
#e.g. iptables-restore < /etc/iptables.up.rules
exit 1
else
#this could of course be commented out if you don't
#want a verbal message when all is good
echo "$rules rule(s) found"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment