python处理网段

# coding:utf-8
import xlrd
import xlwt
import sys
from IPy import IP
import  re

reload(sys)
sys.setdefaultencoding("utf-8")

class IpTable():
    def getIp(self,xls,txt):
        aworkbook = xlrd.open_workbook(xls)
        sheet1 = aworkbook.sheet_by_index(0)
        # col3 = sheet1.col_values(7)
        col3 = sheet1.col_values(7)
        len1 = len(col3)
        s = ','.join(col3)
        s.decode('utf-8')
        list1 = []
        list2 = []
        list3 = []
        for i in range(1, len1):
            emp = sheet1.row_values(i)
            name = emp[7]
            list1.append(name)
        for mm in list1:
            num = re.sub(r':', "", mm)
            num = re.sub(r'[a-z]', "", num)
            num = re.sub(r'[A-Z]', "", num)
            num = re.findall(r'\d+.\d+.\d+.\d+/+\d+', num)
            list2.append(num)
        print list2
        for j in list2:
            if j:
                list3.append(j)
        # print list3
                # # print j
                # for kk in j:
                #     print kk

        # print list1
        # list2 = []
        # list3 = []
        # for t in list1:
        #     if t.strip() != '':
        #         k = t.split(':')
        #         list2.append(k[-1:])
        # for w in list2:
        #     l = ''.join(w)
        #     list3.append(l)
        # for m in range(len(list3)):
        #     list3[m] = list3[m].replace(' ', '')
            # list3[m] = list3[m].replace('\n', '')
        # print list3
        # with open('d.txt', 'w') as f:
        #     for j in list2:
        #         f.write(j + '\n')
        src = open(txt, 'r+')
        ips = src.read().split('\n')
        print (ips)

        # def get_ipduan(file):
        #     list4 = []
        #     list5 = []
        #     connects=[]
        #     with open(file, 'r') as f:
        #         connects = f.readlines()
        #         tmp = []
        #         # print connects
        #         for g in range(len(connects)):
        #             if connects[g] == '\n':
        #                 list4.append(tmp)
        #                 tmp = []
        #             else:
        #                 tmp.append(connects[g].split('\n')[0])
        #     for g in list4:
        #         if g != []:
        #             list5.append(g)
        #     return list5

        list_tmp = []
        for ip in ips:
            for qq in list3:
                for aa in qq:
                    if int(''.join(aa.split('/')[-1:])) >= 20:
                        try:
                            tmp = IP(aa)#172.30.144.0/20
                        except:
                            tmp = ''
                        if ip in tmp:
                            # list_tmp.setdefault(ip, []).append(qq)
                            # print tmp
                            if qq in list_tmp:
                                continue
                            list_tmp.append(qq)
                            # list_tmp1=list(set(list_tmp))
        global str,str1
        str1=[]
        print list_tmp
        for yo in list_tmp:
            pp= ';'.join(yo)
            str1.append(pp)
            # if str =="":
            #     print "none"
            str=';'.join(str1)
        return str

if __name__=="__main__":
    a = IpTable()
    result = a.getIp("","")
    print 'yo ',result
# coding:utf-8
from Main import Main
from tkFileDialog import askopenfilename
import tkMessageBox as tkMB
from iptables import IpTable
import sys

reload(sys)
sys.setdefaultencoding("utf-8")


class FirewallIptable(Main):
    def __init__(self):
        Main.__init__(self)
        self.func = IpTable()

    def OpenIptablesFile(self):
        file_path = askopenfilename(filetypes=(("xls files", ("*.xls", "*.xlsx")),
                                               ("All files", "*.*")))
        print type(file_path)
        if not file_path:
            self.return_msg["text"] = '没有导入文件'
        elif file_path[len(file_path) - 3:] != 'xls' and file_path[len(file_path) - 4:] != 'xlsx':
            print file_path[len(file_path) - 3:]

            self.return_msg["text"] = '不是xls或者xlsx文件'
        else:
            # flag, msg = self.func.comple(file_path)
            print file_path
            self.LB1["text"] = str(file_path)
    def OpenIPtableFile(self):
        file_path = askopenfilename(filetypes=(("txt files", ("*.txt")),
                                               ("All files", "*.*")))
        if not file_path:
            self.return_msg["text"] = '没有导入文件'
        else:
            # flag, msg = self.func.comple(file_path)
            self.LB2["text"] = str(file_path)

    def getResult(self):
        Lb2 = self.LB2["text"]
        Lb1 = self.LB1["text"]
        if Lb1 != '' and (Lb1[len(Lb1) - 3:] == 'xls' or Lb1[len(Lb1) - 4] == 'xlsx'):
            print "|", Lb2[len(Lb2) - 4], "|"
            result = self.func.getIp(Lb1, Lb2)
            self.ScrolledText1.delete(0.0,'end')
            self.ScrolledText1.insert(0.0,result)

        else:
            tkMB.askokcancel("温馨提示", "IP地址数据不是xls或者xlsx文件")


if __name__ == "__main__":
    dingding = FirewallIptable()
    dingding.mainloop()

 

 

posted @ 2017-03-24 18:29  runningzz  阅读(722)  评论(0编辑  收藏  举报