Skip to content

Instantly share code, notes, and snippets.

@tristandostaler
Created July 8, 2016 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tristandostaler/0c3f4021a7887cd8d3ba1ae3f5fce573 to your computer and use it in GitHub Desktop.
Save tristandostaler/0c3f4021a7887cd8d3ba1ae3f5fce573 to your computer and use it in GitHub Desktop.
Script to block ip in IP tables.
#!/bin/sh
if [ "$#" -ne 1 ]; then
echo "Usage: $0 IP (ex: $0 192.168.0.1)" >&2
exit 1
fi
sudo iptables -I INPUT 2 -s $1 -j DROP
sudo iptables -I FORWARD 2 -s $1 -j DROP
#http://stackoverflow.com/questions/3231804/in-bash-how-to-add-are-you-sure-y-n-to-any-command-or-alias
read -p "Permanent Ban? (Add to /etc/rc.local) <y/N> " choice
if [ "$choice" = "y" ] || [ "$choice" = "Y" ] || [ "$choice" = "yes" ] || [ "$choice" = "YES" ]; then
sed -i "$ i/sbin/iptables -I INPUT 2 -i eth1 -s ${1} -j DROP\n/sbin/iptables -I FORWARD 2 -i eth1 -s ${1} -j DROP\n" /etc/rc.local
echo "The address has been added to the end of the file /etc/rc.local"
else
echo "Don't forget to add to /etc/rc.local for permanent ban"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment