使用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:本人非專業的程序員,如寫得不好請多擔待,歡迎留言評論,謝謝!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM