shell-linux禁止IP访问服务器

参考文档: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格式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值