python 利用IPY模块和字典实现将每个业务IP段转换为业务和ip一 一对应的文件。

IPY模块下载链接:

https://files.pythonhosted.org/packages/88/28/79162bfc351a3f1ab44d663ab3f03fb495806fdb592170990a1568ffbf63/IPy-0.83.tar.gz

输入(源文件)文件为:

苏州xxxx有限公司|36.155.118.0-36.155.118.255
xxxx软件技术有限公司|112.25.64.0-112.25.64.255,36.155.126.0-36.155.126.255,36.155.127.0-36.155.127.255
北京xxxx科技有限公司|112.25.86.0-112.25.86.255,112.25.87.0-112.25.87.255,112.25.89.0-112.25.89.255,112.25.90.0-112.25.90.255,112.25.91.0-112.25.91.255
广州xxxx科技有限公司|112.25.78.0-112.25.78.255,112.25.80.0-112.25.80.255,112.25.84.0-112.25.84.63,112.25.84.64-112.25.84.95,112.25.84.128-112.25.84.255,112.25.93.0-112.25.93.255,112.25.94.0-112.25.94.63,112.25.94.64-112.25.94.127,112.25.94.128-112.25.94.255,112.25.95.0-112.25.95.255,36.155.64.0-36.155.64.255,36.155.66.0-36.155.66.255,36.155.67.0-36.155.67.255,36.155.68.0-36.155.68.255,36.155.69.0-36.155.69.255,36.155.70.0-36.155.70.255,36.155.72.0-36.155.72.255,112.25.84.96-112.25.84.127
xxx技股份有限公司|112.25.81.0-112.25.81.127,112.25.82.0-112.25.82.255,112.25.83.0-112.25.83.255
xx云计算有限公司|112.25.88.128-112.25.88.255,112.25.92.0-112.25.92.255
xxxx信息安全管理系统|183.207.121.0/24

目的文件举例:

36.155.71.32|xxx科技股份有限公司(xx移动互联网电视)
36.155.71.33|xxx技股份有限公司(xx移动互联网电视)
36.155.71.34|xxx科技股份有限公司(xx移动互联网电视)
36.155.71.35|xxx科技股份有限公司(xx移动互联网电视)
36.155.71.36|xxx科技股份有限公司(xx移动互联网电视)
36.155.71.37|xxx科技股份有限公司(xx移动互联网电视)

脚本内容:

#!/usr/bin/python 
# coding=UTF8
##2018-6-7 15:56:21
###author:wendf
###将网段转化为单个ip
import sys
import IPy
###定义一个字典用来存业务名和ip段
web_ip_dict= {}
FILE1=sys.argv[1]
f1=open(FILE1,"r")
f2=open("./ip.txt",'w')
for line in f1.readlines():
    key = line.split('|')[0]
    value = (line.split('|')[1]).strip().split(',')

    ip =[]
    for ip_duan in value:
        re_ip =IPy.IP(ip_duan,make_net=True)
        ip = IPy.IP(re_ip)
    web_ip_dict[key] = ip
for key in web_ip_dict:
    for k in web_ip_dict[key]:
        f2.write('%s|%s\n' %(k,key)) 

你可能感兴趣的:(python)