【问题标题】:Open/close port shell script打开/关闭端口外壳脚本
【发布时间】:2015-12-14 18:14:15
【问题描述】:

我找到了一个脚本,它允许您打开或关闭任何端口并将其保存到 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 $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 ]]

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 $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)"; }

        else

        echo "Please enter a valid port(0-65000)"

        fi

else

echo "Please enter only open or close..! Exiting script now";exit 1

fi

【问题讨论】:

    标签: bash shell scripting port iptables


    【解决方案1】:

    我没有在脚本中看到任何错误。你能粘贴你得到的错误吗?见下文

    [root@kali ~]# ./ip.sh

    #####################nnnPresent 在这台机器上打开的端口是

    n已完成列表...nnn#########################

    打开端口输入open,关闭etner close) close

    请输入您想要关闭的端口号:23

    iptables:错误规则(该链中是否存在匹配规则?)。

    iptables: 将防火墙规则保存到 /etc/sysconfig/iptables:[ OK ]

    重定向到 /bin/systemctl restart iptables.service

    通过 iptables 关闭的端口是 nREJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:23 reject-with icmp-port-unreachable

    【讨论】:

    • 我想通了,当我编写代码时,我忘记添加最后三行并且它突然结束了。代码按此处编写的方式工作。猜猜这就是我不睡觉的结果。
    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 2016-09-22
    • 2013-08-30
    • 2020-05-02
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多