shell脚本–筛选出合法ip地址
作者:蓝眼泪
#!/bin/bash
grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" /root/ip.txt >/root/ip_test1.txt
line=$(wc -l /root/ip_test1.txt |awk '{print $1}')
echo "" >/root/ip_test.txt
for (( i=1;i<=$line;i=i+1 ))
do
cat /root/ip_test1.txt |awk ' NR=="$i" {print} ' >/root/ip_test2.txt
a=$( cat /root/ip_test2.txt |cut -d "." -f 1 )
b=$( cat /root/ip_test2.txt |cut -d "." -f 2 )
c=$( cat /root/ip_test2.txt |cut -d "." -f 3 )
d=$( cat /root/ip_test2.txt |cut -d "." -f 4 )
if [ "$a" -lt 1 -o "$a" -gt 255 ]
then
continue
fi
if [ "$b" -lt 0 -o "$b" -gt 255 ]
then
continue
fi
if [ "$c" -lt 0 -o "$c" -gt 255 ]
then
continue
fi
if [ "$d" -lt 0 -o "$d" -gt 255 ]
then
continue
fi
cat /root/ip_test2.txt >> /root/ip_test.txt
done
# rm -rf /root/ip_test1.txt
# rm -rf /root/ip_test2.txt