簡體   English   中英

從輸入文件和csv格式輸出的Python腳本到Grep字符串

[英]Python script to Grep strings from input file and output in csv format

我正在寫一個python腳本到文件中的grep字符串並以以下格式在csv文件中顯示輸出

在此處輸入圖片說明

在此處輸入圖片說明

輸入文件(result_EPFT_config_device):

Hostname SIM-MPL-LTE-PE-RTR-134
loopback 22.13.7.34
lpts punt excessive-flow-trap
penalty-rate arp 10
penalty-rate icmp 50
penalty-rate igmp 50
penalty-rate ip 100
exclude interface Bundle-Ether6
exclude interface Bundle-Ether8
exclude interface Bundle-Ether15
exclude interface Bundle-Ether16
exclude interface Bundle-Ether53
exclude interface TenGigE0/0/1/1
exclude interface TenGigE0/1/1/0
exclude interface Bundle-Ether6.2
exclude interface Bundle-Ether6.4
exclude interface Bundle-Ether8.2
exclude interface Bundle-Ether8.4
exclude interface Bundle-Ether16.2
exclude interface Bundle-Ether16.4
exclude interface Bundle-Ether53.2
exclude interface TenGigE0/0/1/3.100
exclude interface TenGigE0/0/1/3.102
exclude interface TenGigE0/0/1/3.103
exclude interface TenGigE0/0/1/3.104
exclude interface TenGigE0/1/1/0.100
exclude interface GigabitEthernet0/0/0/1
exclude interface GigabitEthernet0/0/0/6
exclude interface GigabitEthernet0/0/0/9
dampening.
non-subscriber-interfaces
report-threshold 10

以下是我目前准備的python腳本。 僅能grep字符串並打印

import sys
import telnetlib
import os
import subprocess
import re
import csv

fh = open("result_EPFT_config_device", "r")
fh1 = open("testingAjay", "w+")
line = fh.readlines()
for lines in line:
        if re.search("(lpts punt excessive-flow-trap)", lines):
                m =  (lines.split(' '))
                print m[0], m[1], m[2]
        if re.search("(penalty-rate arp)", lines):
                n =  (lines.split(' '))
                print n[0], n[1], n[2]
        if re.search("(penalty-rate icmp)", lines):
                a =  (lines.split(' '))
                print a[0], a[1], a[2]
        if re.search("(penalty-rate igmp)", lines):
                b =  (lines.split(' '))
                print b[0], b[1], b[2]
        if re.search("(penalty-rate ip)", lines):
                c =  (lines.split(' '))
                print c[0], c[1], c[2]
        if re.search("(dampening)", lines):
                c =  (lines.split(' '))
                print c[0]
        if re.search("(non-subscriber-interfaces)", lines):
                c =  (lines.split('-'))
                print c[0], c[1], c[2]
        if re.search("(report-threshold 10)", lines):
                c =  (lines.split(' '))
                print c[0], c[1]

我的腳本輸出:

lpts punt excessive-flow-trap
penalty-rate arp 10
penalty-rate icmp 50
penalty-rate igmp 50
penalty-rate ip 100
dampening.
non subscriber interfaces

report-threshold 10

現在在這里我想把輸出放在csv文件中,如下所示

在此處輸入圖片說明

Hostname|loopback|lpts punt excessive-flow-trap|penalty-rate arp|penalty-rate icmp|penalty-rate igmp|penalty-rate ip|dampening|non-subscriber-interfaces|report-threshold
SIM-MPL-LTE-PE-RTR-134|1.1.1.1|yes|10|50|50|100|Yes|Yes|10
NDL-MPL-PE-RTR-195|2.2.2.2|No|No|No|20|50|NO|20Yes

如上面的屏幕快照所示,如果lpt spunt超出流量陷阱,則必須將其標記為“是”(如果輸入文件中存在),否則標記為“否”。 類似的邏輯需要應用於列阻尼非訂戶接口

您能幫我實現上述要求以csv格式輸出的要求嗎

開始了! 因此,正如上面的評論所述,您可以只使用“ startswith”,然后使用正則表達式來匹配行。

我在這里使用了python3而不是python2。

如果使用“ python3 main.py”在目錄中運行此文件,它將在“ inputs”子目錄中搜索所有要分析的文件。

然后,我們使用相關字段為每個文件構建一個字典,並加載它們的值。 我們將這些字典添加到列表中。 最后,我們只將標頭寫入csv,然后遍歷各行並寫入值。 您可能在讀取文件時寫了行,但是我發現從精神上分離了解析和輸出清理器。

當您要遍歷行中的每一行時,我將“行中的行”的順序更改為“行中的行”。

import os
import csv

def parseFile(fileName):

    # We are using a dictionary to store info for each file
    data = dict()

    # Set all Yes/Nos to NO by default
    data["lpts punt excessive-flow-trap"] = "NO"
    data["dampening"] = "NO"
    data["non-subscriber-interfaces"] = "NO"

    fh = open(fileName, "r")
    lines = fh.readlines()
    for line in lines:

        # We need this so we don't end up with newline characters in our CSV
        line = line.rstrip("\n")

        # We dont need regular expressions here as matching whole line
        # Do YES/NO first
        if line == "lpts punt excessive-flow-trap":
            data["lpts punt excessive-flow-trap"] = "YES"
            continue;

        if line == "dampening":
            data["dampening."] = "YES"
            continue;

        if line == "non-subscriber-interfaces":
            data["non-subscriber-interfaces"] = "YES"
            continue;

        # Now do the rest
        if line.startswith("Hostname"):
            splitted = line.split(' ')
            data["Hostname"] = splitted[1]
            continue;

        if line.startswith("loopback"):
            splitted = line.split(' ')
            data["loopback"] = splitted[1]
            continue;

        if line.startswith("penalty-rate arp"):
            print("ARP")
            splitted = line.split(' ')
            data["penalty-rate arp"] = splitted[2]
            continue;

        if line.startswith("penalty-rate icmp"):
            splitted = line.split(' ')
            data["penalty-rate icmp"] = splitted[2]
            continue;

        if line.startswith("penalty-rate igmp"):
            splitted = line.split(' ')
            data["penalty-rate igmp"] = splitted[2]
            continue;

        if line.startswith("penalty-rate ip"):
            splitted = line.split(' ')
            data["penalty-rate ip"] = splitted[2]
            continue;

        if line.startswith("report-threshold"):
            splitted = line.split(' ')
            data["report-threshold"] = splitted[1]
            continue;

    return data


if __name__ == "__main__":
    inputsDirectory = "inputs"
    path = os.path.abspath(inputsDirectory)
    fileList = ["{}/{}".format(path,x) for x in os.listdir(inputsDirectory)]
    print(fileList)

    # Load Each File and Build Dictionary
    csvRows = []
    for file in fileList:
        newRow = parseFile(file)
        csvRows.append(newRow)

    print(csvRows)

    # Output CSV using dictionaries for each file
    outputFile = "output.csv"
    with open(outputFile, 'w') as csvfile:
        fieldnames = ["Hostname",
                      "loopback",
                      "lpts punt excessive-flow-trap",
                      "penalty-rate arp",
                      "penalty-rate icmp",
                      "penalty-rate igmp",
                      "penalty-rate ip",
                      "dampening",
                      "non-subscriber-interfaces",
                      "report-threshold"]
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        writer.writeheader()
        for row in csvRows:
            writer.writerow(row)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM