参考文档:iptables 禁止某些 IP 访问
1.测试环境
centos 6.x
2.脚本代码
#!/bin/bash
#Set PATH
main(){
while :; do echo
echo "*******************************"
echo "1.禁封IP"
echo "2.解封IP"
echo "3.查看禁止列表"
echo "0.退出"
echo "*******************************"
read -p "请选择: " choice
if [[ ! $choice =~ ^[0-6]$ ]]; then
echo "输入错误! 请输入正确的数字!"
else
che $choice
fi
done
}
function Banned(){
iptables -I INPUT -s $1 -j DROP
/etc/rc.d/init.d/iptables save
service iptables restart
}
function Unblock(){
iptables -D INPUT -s $1 -j DROP
/etc/rc.d/init.d/iptables save
service iptables restart
}
function che(){
clear
if [[ $1 == 0 ]];then
exit
fi
if [[ $1 == 1 ]];then
read -p "请输入要禁止的IP或IP段:" choice
Banned $choice
fi
if [[ $1 == 2 ]];then
read -p "请输入要解封的IP或IP段:" choice
Unblock $choice
fi
if [[ $1 == 3 ]];then
iptables -L
fi
}
main
3.注意:保存成sh文件时候,注意转换成UNIX格式