使用python批量获取IP对应的交换机接口并生成excel表

以下代码是本人现网亲测脚本。

 

代码如下“

# -*- coding: gbk -*-
# 制作人:Tim

import os
import openpyxl
from openpyxl import Workbook

path = "D:\\设备日志\\7500-info.txt"
dir = "D:\\设备日志\\"


def action(pathx,hostname,content):
    f1 = open(pathx,'r')    # 打开文件
    for line1 in f1.readlines():    # 遍历文件内容
        if content[1] in line1:
            if "port-security" in line1:
                pass
            else:
                lst1 = line1.split(" ")    # 格式如下:['f84d-fc56-6c45', '', '', '22', '', 'Learned', '', 'GE1/0/25']
                if 'Security' in lst1:
                    output = open("D:\\设备日志\\output.txt",'a')     # 新建一个文件,把输出的内容保存到这里
                    if ('BAGG1' in lst1 or 'GE1/0/49' in lst1 or 'GE1/0/52' in lst1 or 'BAGG2' in lst1 or 'BAGG3' in lst1 or 'GE1/0/51' in lst1):
                        pass
                    else:
                        print("hostname:"+ hostname[1]+" IP:"+ content[0]+" MAC:" + content[1] + " 接口:" + lst1[21],file = output)
                        book = openpyxl.load_workbook(r'D:\python\program\test.xlsx')
                        sheet = book.active
                        l1 = []
                        l1.append(hostname[1]) 
                        l1.append(content[0])
                        l1.append(content[1])
                        l1.append(lst1[21])
                        sheet.append(l1)
                        book.save(r'D:\python\program\test.xlsx')
                    output.close()
                if "Learned" in lst1:
                    output = open("D:\\设备日志\\output.txt",'a')
                    if ('BAGG1' in lst1 or 'GE1/0/49' in lst1 or 'GE1/0/52' in lst1 or 'BAGG2' in lst1 or 'BAGG3' in lst1 or 'GE1/0/51' in lst1):
                        pass 
                    else:
                        print("hostname:"+ hostname[1]+" IP:"+ content[0]+" MAC:" + content[1] + " 接口:" + lst1[22],file = output)
                        book = openpyxl.load_workbook(r'D:\python\program\test.xlsx')
                        sheet = book.active
                        l1 = []
                        l1.append(hostname[1])
                        l1.append(content[0])
                        l1.append(content[1])
                        l1.append(lst1[22])
                        sheet.append(l1)
                        book.save(r'D:\python\program\test.xlsx')
                    output.close()
    f1.close()

def main():
    book = Workbook()
    sheet = book.active
    book.save('D:\\python\\program\\test.xlsx')
    content = []
    f = open(path,'r')
    for line in f.readlines():      # 遍历"D:\\设备日志\\7500-info.txt"文件
        lst = line.split(" ")
        for i in lst:
            if i !='':    # 去除列表中的' '空元素
                content.append(i)   
        for x in os.listdir(dir):   # 遍历dir目录下的内容,如D:\\设备日志\\
            pathx = dir +x     
            hostname = pathx.split(" ")
            for a in range(20,35):        
                if content[2] == str(a):      # content[2]匹配的是vlan
                    action(pathx,hostname,content)
        content=[]
    f.close ()

if __name__=='__main__':
    main()

 

效果如下:

 

 

PS:本人非专业的程序员,如写得不好请多担待,欢迎留言评论,谢谢!!

posted @ 2021-01-20 11:52  tim54252  阅读(1033)  评论(0编辑  收藏  举报