本文共 3877 字,大约阅读时间需要 12 分钟。
最近准备和CoreSite - Any2 California接入商建立网络BGP邻居关系。从上找到了所有接入商的信息,但是转移信息到本地不是很方便,需要进行多次文本调整,耗时较长。
作为萌新,立马就想到近期学习的grep/sed/awk工具。于是就尝试处理数据。
1、下载页面内容
1 | > curl https: //www .peeringdb.com /ix/142 > peering |
将页面内容存入peering文件
2、删除无用信息
翻看网页,发现第一个行需要的信息是“2degrees”,最后一行是“Zscaler AS22616”
1 2 3 4 5 6 | > grep -n '2degrees' peering ##发现第一行是807 > sed -i '1,806' d peering ##删除1-806行 > head peering ##检查 > grep -nA8 '22616' peering ##发现最后一行是5161 > sed -i '5162,$' d peering ##删除5161后的行 > tail peering ##检查 |
▼还有一种方法:
1 2 3 4 | > egrep 'view_title right' peering <div class= "view_title right" data-edit-toggled= "edit" style= "display:none;" > > sed -i '1,/2degrees/' d peering > sed -i '/view_title right/,$' d peering |
这种方式基本能删除不需要的信息。
3、观察剩余内容,提取关键词
▼样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < div class = "row item" > < div class = "col-xs-5 col-sm-5 col-md-5" > < div class = "peer" data-sort-value = "4 less communications, inc." data-filter-value = "4 Less Communications, Inc." > < a href = "/net/5985" >4 Less Communications, Inc.</ a > </ div > < div class = "asn" data-sort-value = "16912" data-filter-value = "16912" >16912</ div > </ div > < div class = "col-xs-4 col-sm-4 col-md-4" > < div class = "ip4" data-filter-value = "206.72.210.229" >206.72.210.229</ div > < div class = "ip6" data-filter-value = "None" >None</ div > </ div > < div class = "col-xs-3 col-sm-3 col-md-3" > < div class = "speed" data-sort-value = "1000" data-filter-value = "1G" >1G</ div > < div class = "policy" data-filter-value = "Open" >Open</ div > </ div > </ div > |
关键词分别为“peer”,“asn”,“ip4”,“speed”和“policy“”,这些也是我想要提取的信息。
4、提取关键词信息
1 2 3 4 5 6 | > egrep '">.*</a>' peering > peer1 ##提取peer名称导入peer1文件 文本样例:<a href= "/net/5985" >4 Less Communications, Inc.< /a > > wc -l peer1 ##查看行数,用于确认 > sed -ir 's#</a>##g' peer1 ##删除最后的 "</a>" > awk -F '>' '{print $2}' peer1 >peer ##提取peer名称 > wc -l peer ##检查行数是否正确 |
▼还有一种方法:
1 2 | cat peering | grep -Po '(?<=>).*(?=</a>)' > peer ##"-P"表示采用Perl正则表达式模式 ##如果只需要匹配到的部分,还可以加上-o选项 |
用同样的方法提取asn,ip4,speed和policy信息。
5、组合文件内容
通过以上步骤,我得到了5个文本文件,分别装有我需要的信息,且经过行数对比,行数一致。
▎方法1——利用paste命令组合文件:
1 2 3 4 5 6 7 8 9 10 11 | [root@server01 test1] # paste -d '*' name asn ip speed policy 2degrees*23655*206.72.211.83*10G*Selective 4 Less Communications, Inc.*16912*206.72.210.229*1G*Open AARNet*7575*206.72.210.64*10G*Selective Ace Data Centers, Inc.*11798*206.72.210.128*10G*Open ACSData*18119*206.72.211.12*1G*Open Aerioconnect, Inc.*10993*206.72.210.21*10G*Selective ...... Zoom Video Communications, Inc.*30103*206.72.211.79*10G*Selective Zscaler AS22616*22616*206.72.211.39*10G*Open Zscaler AS22616*22616*206.72.211.40*10G*Open |
▎方法2——也可以利用python脚本提取并组合:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #/usr/bin/env python def newlist(arg): file = open (arg, 'r' ) arglist = [] for eachline in file : eachline = eachline.replace( "\n" ,"") arglist.append(eachline) return arglist file .close() if __name__ = = '__main__' : peer = newlist( 'peer' ) asn = newlist( 'asn' ) ip = newlist( 'ip' ) speed = newlist( 'speed' ) policy = newlist( 'policy' ) for i in range ( len (name)): print (peer[i] + '*' + asn[i] + '*' + ip[i] + '*' + speed[i] + '*' + policy[i]) |
▼处理结果:
1 2 3 4 5 6 7 8 9 10 11 | [root@server01 test1] # python3 python.py 2degrees*23655*206.72.211.83*10G*Selective 4 Less Communications, Inc.*16912*206.72.210.229*1G*Open AARNet*7575*206.72.210.64*10G*Selective Ace Data Centers, Inc.*11798*206.72.210.128*10G*Open ACSData*18119*206.72.211.12*1G*Open Aerioconnect, Inc.*10993*206.72.210.21*10G*Selective ...... Zoom Video Communications, Inc.*30103*206.72.211.79*10G*Selective Zscaler AS22616*22616*206.72.211.39*10G*Open Zscaler AS22616*22616*206.72.211.40*10G*Open |
因为部分peer名称带空格,故将分隔符改为其他特殊符号(如#,*),存入TXT文档内,在导入EXCEL时,也方便分隔。
通过以上步骤,可以采集到所需要的信息。
如果是熟悉python,那么可以将所有操作放在脚本里(详见:),最后使用openpyxl模块导入EXCEL文件里。
本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1945059,如需转载请自行联系原作者