Shell脚本 过滤掉错误写法的ip地址

来源:互联网 发布:什么是虚拟专用网络 编辑:程序博客网 时间:2024/05/21 07:02

遇到给的IP地址列表的文件中出现 错误写法的IP,通过下面的脚本,过滤掉写法错误的IP地址

脚本文件:

[root@ops-ip-statistic bin]# cat judge.sh #!/bin/bash#auth:hehailiang#USAGE: sh judge.sh ip_list_fileSRC=$1grep -v "^0" $SRC >tmpfilerm -rf $SRCget_true_ip(){src_file=$1dst_file=$2cat $src_file|while read linedo   ipcalc -c $line >/dev/null 2>&1   a=$(echo $?)   if [ $a -eq 0 ];then      echo $line >>$2    fi done}get_true_ip tmpfile $SRCrm -rf tmpfile

实例:

--- 样例文件 ---[root@ops-ip-statistic bin]# cat a.txt            1.2.3.40.3.4.5                                          #没有0开头的IP地址265.31.0.15                                      #265大于2555.2.3.490                                        #490大于490111.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.4111.9.3.4
0 0
原创粉丝点击