shell 脚本判断ip

1.编写txt里面放入ip

[ ~]# vim ip.txt
192.168.177.1
192.168.177.2
192.168.177.3
192.168.177.4
192.168.177.5
192.168.177.6172.168.177.8127.0.0.1

2.编写脚本

[ ~]# vim if.sh

#!/bin/bash
ip=(`cat /root/ip.txt`)
for i in ${ip[*]}
do
        if [[ $i == "192"* ]]; then
                echo "$i is ok"
        else
                iptables -A INPUT -s $i -j DROP &> /dev/null
                echo "$i drop"
        fi
done

3.测试

[ ~]# sh if.sh

相关推荐