shell-脚本_IP判断

需求

生成一个ip.txt文件

判断:当ip为192开头的,显示这个ip是ok的,当ip为172开头的,就调用iptables drop掉

生成IP.txt文件并键入ip

vim ip.txt

键入:

192.168.1.1

192.168.1.2

172.129.1.1

172.129.1.2

shell脚本源码

#!/bin/bash
# 用数组的方式输出ip.txt文件
ip=(`cat /root/ip.txt`)
# 循环遍历数组
for i in ${ip[@]}
do
	# 开始判断IP是否为192开头的IP
	if [[ $i == "192."* ]];then
		echo "$i is OK"
	else
		# 使用iptables dorp掉IP
		iptables -A INPUT -s $i -j OROP &>/dev/null
		echo "$i 此IP以drop"
	fi
done

猜你喜欢

转载自www.cnblogs.com/sunjianlin/p/13169925.html
今日推荐