Home  >  Q&A  >  body text

python - 检测sshd端口误封

1,检查sshd端口误封,如果误封就重新添加。
2,代码如下:

#!/bin/bash
##written by lin
##check port 22
iptables -P INPUT ACCEPT;
iptables -P OUTPUT ACCEPT;
a=`iptables -nvL |grep dpt:22|awk  '{print $3}'`
#if [ $a == "REJECT" ]||[ $a == "DROP" ]
if [ $a != "ACCEPT" ]
then iptables -I INPUT -p tcp --dport 22 -j ACCEPT
fi

3.提示错误

4,不是可以指定变量等于某个字符串吗?

5,如果有更好的建议,请附上您的脚本,感谢。

`

高洛峰高洛峰2741 days ago2400

reply all(2)I'll reply

  • 阿神

    阿神2017-04-17 17:54:49

    It seems that the comparison of unequal is using -ne而不是!=~

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:54:49

    1. Your error is because the avariable is empty

    2. You can use the following script to check

    #!/usr/bin/env bash
    
    block_22=`iptables -vnL | grep 'tcp dpt:22' | egrep 'DROP|REJECT' | wc -l`
    [ $block_22 -gt 0 ] && iptables -I INPUT -p tcp --dport 22 -j ACCEPT

    reply
    0
  • Cancelreply