This is a small script which will takecare of blocking and unblocking ports by asking user about his desire. Just copy this code to your system and change permissions and start executing it.
#!/bin/bash#Author: Surendra Anne(surendra@linuxnix.com)PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbinclearecho -e "############################nnnPresent ports opened on this machine are$(iptables -nL INPUT | grep ACCEPT | grep dpt)nCompleted listing...nnn#########################"read -p "To open port enter open, to close etner close) " OPT1if [[ "$OPT1" == open ]]thenread -p "Please enter your desired port number to open: " PORT1if [[ "$PORT1" =~ [0-9]{1,6} ]]
then
iptables -D INPUT $(iptables -nL INPUT --line-numbers | grep "$PORT1" | grep REJECT | awk '{print $1}')iptables -A INPUT -m tcp -p tcp --dport "$PORT1" -j ACCEPT && { service iptables save;service iptables restart; echo -e "Ports opend through iptables are n$(iptables -nL INPUT | grep ACCEPT | grep dpt)"; }
else
echo "Please enter a valid port(0-65000)"
fi
elif [[ "$OPT1" == close ]]thenread -p "Please enter your desired port number to close: " PORT1if [[ "$PORT1" =~ [0-9]{1,6} ]]theniptables -D INPUT $(iptables -nL INPUT --line-numbers | grep "$PORT1" | grep ACCEPT | awk '{print $1}')
iptables -A INPUT -m tcp -p tcp --dport "$PORT1" -j REJECT && { service iptables save;service iptables restart; echo -e "Ports closed through iptables are n$(iptables -nL INPUT | grep REJECT | grep dpt)"; }elseecho "Please enter a valid port(0-65000)"fielseecho "Please enter only open or close..! Exiting script now";exit 1fiOutput: For closing a port[root@localhost ~]# bash block-unblock-ports.sh############################Present ports opened on this machine areACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80Completed listing...#########################To open port enter open, to close etner close) closePlease enter your desired port number to close: 80iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]iptables: Flushing firewall rules: [ OK ]iptables: Setting chains to policy ACCEPT: filter [ OK ]iptables: Unloading modules: [ OK ]iptables: Applying firewall rules: [ OK ]Ports closed through iptables areREJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 reject-with icmp-port-unreachableFor opening port:[root@localhost ~]# bash block-unblock-ports.sh############################Present ports opened on this machine areACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22Completed listing...#########################To open port enter open, to close etner close) openPlease enter your desired port number to open: 81Bad argument `7'Try `iptables -h' or 'iptables --help' for more information.iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]iptables: Flushing firewall rules: [ OK ]iptables: Setting chains to policy ACCEPT: filter [ OK ]iptables: Unloading modules: [ OK ]iptables: Applying firewall rules: [ OK ]Ports opend through iptables areACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:81The following two tabs change content below.Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020- FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018