Created
October 30, 2017 16:48
-
-
Save elico/f21dae7a34e1736f56a1995977852460 to your computer and use it in GitHub Desktop.
An iptables script that uses an ipset to dynamically allow hosts that are defined with domain names at a firewall.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!/usr/bin/env bash | |
IPTABLES=/sbin/iptables | |
IPSET=/sbin/ipset | |
TMPFILE1="/tmp/`uuidgen`.lookupres" | |
touch $TMPFILE1 | |
$IPSET create allowedhosts hash:net | |
while read item; do | |
# echo $item | |
LOOKUPRES=`host -4 $item|grep "has address"` | |
echo $LOOKUPRES > $TMPFILE1 | |
while read resolvedhost; do | |
ALLOWEDHOST=`echo $resolvedhost|gawk '{print $4}'` | |
echo "Adding host => $ALLOWEDHOST" | |
$IPSET add allowedhosts $ALLOWEDHOST | |
done < $TMPFILE1 | |
done < /opt/etc/doms-to-allow.txt | |
$IPTABLES -L INPUT |grep "allowedhosts" >/dev/null | |
if [ "$?" -ne "0" ];then | |
$IPTABLES -I INPUT -m set --match-set allowedhosts dst,src -j ACCEPT | |
fi | |
rm $TMPFILE1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment