Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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