例53
需求:如果不小心用iptables规则把22端口封掉了,不能远程登陆,要想解决问题,需要去机房,写一个监控脚本,监控iptables规则是否封22,如果封掉了,打开,定时任务每分钟执行一次
#!/bin/bash
#这个脚本用来解封22端口
#作者:xzm
#日期:2019-12-11
iptables -nvL INPUT --line-numbers | grep -w 'dpt:22'| awk '$4~/REJECT|DROP/ {print $1}' >/tmp/iptables.log
n=`wc -l /tmp/iptables.log`
if [ $n -gt 0 ]
then
for n in `tac /tmp/iptables.log`
do
iptables -D INPUT $n
done
fi
知识点:iptables -nvL INPUT --line-numbers打印出规则id 倒序解封,否则规则id号会改变