遇到给的IP地址列表的文件中出现 错误写法的IP,通过下面的脚本,过滤掉写法错误的IP地址
脚本文件:
[root@ops-ip-statistic bin]# cat judge.sh
#!/bin/bash
#auth:hehailiang
#USAGE: sh judge.sh ip_list_file
SRC=$1
grep -v "^0" $SRC >tmpfile
rm -rf $SRC
get_true_ip(){
src_file=$1
dst_file=$2
cat $src_file|while read line
do
ipcalc -c $line >/dev/null 2>&1
a=$(echo $?)
if [ $a -eq 0 ];then
echo $line >>$2
fi
done
}
get_true_ip tmpfile $SRC
rm -rf tmpfile
实例:
--- 样例文件 ---
[root@ops-ip-statistic bin]# cat a.txt
1.2.3.4
0.3.4.5 #没有0开头的IP地址
265.31.0.15 #265大于255
5.2.3.490 #490大于490
111.9.3.4
有注释的三行是错误的IP
--- 执行脚本过滤 ---
[root@ops-ip-statistic bin]# sh judge.sh a.txt
[root@ops-ip-statistic bin]# cat a.txt
1.2.3.4
111.9.3.4