PDA

View Full Version : Linux bash script for easy port connections banning in IPTables firewall



Fli
10-03-2013, 11:10 AM
If you need to disallow people to connect to some port, one option is to do it via firewall iptables.

Here is the commands to do: http://internetlifeforum.com/networking/967-how-block-allow-iptables-port/#post1482

If you dont remember these commands and may need to deny/allow connections repeatedly, you may use my script (use on your own risk, it works properly for me).



echo "IPTables firewall - Deny/Allow connections to certain port, select operation
--------------------------------------------------
i = insert rule / deny port connections
d = delete rule / allow port connections
e = exit"
read choice


if [ $choice = "i" ];then
echo "List of open ports and services on them:"
nmap localhost
echo "
You selected to insert rule into IPtables, blocking connections to your port. Example: If you add ssh or http, then your server will stop responding to ssh or http requests, You/Anyone wont be able to connect it!!!
Please type port name or number (Examples: http,80,ftp,21 ) and hit enter to add it into iptables/block it."
read port
/sbin/iptables -I INPUT -p tcp --dport $port -j DROP
echo "Command to add port to iptables (block it), was executed."
service iptables save;


elif [ $choice = "d" ];then


echo "You selected to delete rule from IPtables, allowing connections to your port. Example: If you remove ssh or http, then your server will start responding to ssh or http requests.
Please type port name or number (Examples: http,80,ftp,21 ) and hit enter to delete it from iptables/allow it."
read port
/sbin/iptables -D INPUT -p tcp --dport $port -j DROP
echo "- Command to delete port from iptables (unblock it), was executed."
service iptables save;
else
echo "Complete. No changes made"
fi