Open/close端口shell脚本
Open/close port shell script
我找到了一个脚本,可以让您打开或关闭任何端口并将其保存到 iptables。我遇到的问题是在我告诉它关闭之后但在我给它一个端口号之前它会抛出一个语法错误。这个问题就在第一个 else 之前。
#!/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
clear
echo -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) " OPT1
if [[ "$OPT1" == open ]]
then
read -p "Please enter your desired port number to open: " PORT1
if [[ "$PORT1" =~ [0-9]{1,6} ]]
then
iptables -D INPUT $(iptables -nL INPUT --line-numbers | grep "$PORT1" | grep REJECT | awk '{print }')
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 ]]
then
read -p "Please enter your desired port number to close: " PORT1
if [[ "$PORT1" =~ [0-9]{1,6} ]]
then
iptables -D INPUT $(iptables -nL INPUT --line-numbers | grep "$PORT1" | grep ACCEPT | awk '{print }')
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)"; }
else
echo "Please enter a valid port(0-65000)"
fi
else
echo "Please enter only open or close..! Exiting script now";exit 1
fi
我没有看到脚本中有任何错误。你能粘贴你得到的错误吗?见下文
[root@kali ~]# ./ip.sh
######################nnn本机打开的端口是
n已完成上市...nnn#########################
打开端口输入open,关闭etner close) close
请输入您要关闭的端口号:23
iptables:错误规则(该链中是否存在匹配规则?)。
iptables:将防火墙规则保存到 /etc/sysconfig/iptables:[确定]
重定向到 /bin/systemctl 重新启动 iptables.service
通过 iptables 关闭的端口是 nREJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:23 reject-with icmp-port-unreachable
我找到了一个脚本,可以让您打开或关闭任何端口并将其保存到 iptables。我遇到的问题是在我告诉它关闭之后但在我给它一个端口号之前它会抛出一个语法错误。这个问题就在第一个 else 之前。
#!/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
clear
echo -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) " OPT1
if [[ "$OPT1" == open ]]
then
read -p "Please enter your desired port number to open: " PORT1
if [[ "$PORT1" =~ [0-9]{1,6} ]]
then
iptables -D INPUT $(iptables -nL INPUT --line-numbers | grep "$PORT1" | grep REJECT | awk '{print }')
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 ]]
then
read -p "Please enter your desired port number to close: " PORT1
if [[ "$PORT1" =~ [0-9]{1,6} ]]
then
iptables -D INPUT $(iptables -nL INPUT --line-numbers | grep "$PORT1" | grep ACCEPT | awk '{print }')
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)"; }
else
echo "Please enter a valid port(0-65000)"
fi
else
echo "Please enter only open or close..! Exiting script now";exit 1
fi
我没有看到脚本中有任何错误。你能粘贴你得到的错误吗?见下文
[root@kali ~]# ./ip.sh
######################nnn本机打开的端口是n已完成上市...nnn#########################
打开端口输入open,关闭etner close) close
请输入您要关闭的端口号:23
iptables:错误规则(该链中是否存在匹配规则?)。
iptables:将防火墙规则保存到 /etc/sysconfig/iptables:[确定]
重定向到 /bin/systemctl 重新启动 iptables.service
通过 iptables 关闭的端口是 nREJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:23 reject-with icmp-port-unreachable