英文:
Using python to parse a log file into csv with formatting
问题
我尝试使用PowerShell解决这个问题,但无法使其正常工作,所以我已经返回到Python来尝试解决它。目前,我已经将标题写入了一个csv文件,但我在尝试从日志文件中提取我想要的特定信息时遇到了困难。我只需要数据点,不再需要标题。
我目前的进展如下:
import csv
import numpy as np
header = np.asarray([["Connection","IKE PEER","TYPE","REKEY","ENCRYPT","AUTH","ROLE","STATE","HASH","LIFETIME","LIFETIME REMAINING"]])
with open('output.csv', 'w') as f:
mywriter = csv.writer(f, delimiter=',')
mywriter.writerows(header)
需要解析到CSV文件的示例:
1 IKE Peer: 184.188.106.30 (PHX2)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : 3des Hash : SHA
Auth : preshared Lifetime: 86400
Lifetime Remaining: 52140
2 IKE Peer: 96.39.70.182 (Worcester)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : aes-256 Hash : SHA
Auth : preshared Lifetime: 28800
Lifetime Remaining: 5929
(更多数据点的示例省略...)
我想要CSV文件看起来像这样,但下面有更多的数据点。
谢谢很多!
英文:
I attempted this problem using PowerShell but couldn't get it to work so I've returned to python to attempt it. I currently have my headers being written to a csv file however I'm having trouble trying to figure out how I would extract specific information from the logfile that I want. I only need the data points as I no longer need the headers from it.
What I have so far:
import csv
import numpy as np
header = np.asarray([["Connection","IKE PEER","TYPE","REKEY","ENCRYPT","AUTH","ROLE","STATE","HASH","LIFETIME","LIFETIME REMAINING"]])
with open('output.csv', 'w') as f:
mywriter = csv.writer(f, delimiter=',')
mywriter.writerows(header)
An example of what I need to parse to a csv file:
1 IKE Peer: 184.188.106.30 (PHX2)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : 3des Hash : SHA
Auth : preshared Lifetime: 86400
Lifetime Remaining: 52140
2 IKE Peer: 96.39.70.182 (Worcester)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : aes-256 Hash : SHA
Auth : preshared Lifetime: 28800
Lifetime Remaining: 5929
3 IKE Peer: 162.17.89.105 (Fresno)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : aes-256 Hash : SHA
Auth : preshared Lifetime: 28800
Lifetime Remaining: 6564
4 IKE Peer: 68.255.155.73 (San Leandro)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : 3des Hash : SHA
Auth : preshared Lifetime: 86400
Lifetime Remaining: 71608
5 IKE Peer: 96.69.172.213 (Denver WHSE)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : aes-256 Hash : SHA
Auth : preshared Lifetime: 86400
Lifetime Remaining: 35956
6 IKE Peer: 75.146.82.217 (Nashville)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : aes-256 Hash : SHA
Auth : preshared Lifetime: 86400
Lifetime Remaining: 72546
7 IKE Peer: 172.95.3.46 (Manteca)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : aes-256 Hash : SHA
Auth : preshared Lifetime: 28800
Lifetime Remaining: 15891
8 IKE Peer: 76.79.100.162 (Anaheim)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : aes-256 Hash : SHA
Auth : preshared Lifetime: 28800
Lifetime Remaining: 18567
9 IKE Peer: 69.75.17.212 (Murrieta)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : aes-256 Hash : SHA
Auth : preshared Lifetime: 86400
Lifetime Remaining: 78551
10 IKE Peer: 64.183.136.234 (Coeur D'Alene)
Type : L2L Role : initiator
Rekey : no State : MM_ACTIVE
Encrypt : aes-256 Hash : SHA
Auth : preshared Lifetime: 28800
Lifetime Remaining: 21253
What I want the CSV file to look like but with more data points underneath.
Thanks A lot!
答案1
得分: 1
以下是您要翻译的代码部分:
import csv
headers = ["Connection", "IKE PEER", "TYPE", "REKEY", "ENCRYPT", "AUTH", "ROLE", "STATE", "HASH", "LIFETIME", "LIFETIME REMAINING"]
with open("fileout.csv", "w", encoding="utf-8", newline="") as file_out:
writer = csv.DictWriter(file_out, fieldnames=headers)
writer.writeheader()
with open("infile.txt", "r", encoding="utf-8") as log_file:
while True:
try:
line = next(log_file)
except StopIteration:
break
new_row = {}
new_row["Connection"] = line[:4].strip()
new_row["IKE PEER"] = line[14:].strip()
line = next(log_file)
new_row["TYPE"] = line[14:30].strip()
new_row["ROLE"] = line[40:].strip()
line = next(log_file)
new_row["REKEY"] = line[14:30].strip()
new_row["STATE"] = line[40:].strip()
line = next(log_file)
new_row["ENCRYPT"] = line[14:30].strip()
new_row["HASH"] = line[40:].strip()
line = next(log_file)
new_row["AUTH"] = line[14:30].strip()
new_row["LIFETIME"] = line[40:].strip()
line = next(log_file)
new_row["LIFETIME REMAINING"] = line[24:].strip()
writer.writerow(new_row)
如果您需要进一步的帮助,请告诉我。
英文:
If you use a while loop and process lines "6 at a time" you might be able to leverage the fix nature of the rows to pick out your data:
import csv
headers = ["Connection","IKE PEER","TYPE","REKEY","ENCRYPT","AUTH","ROLE","STATE","HASH","LIFETIME","LIFETIME REMAINING"]
with open("fileout.csv", "w", encoding="utf-8", newline="") as file_out:
writer = csv.DictWriter(file_out, fieldnames=headers)
writer.writeheader()
with open("infile.txt", "r", encoding="utf-8") as log_file:
while True:
try:
line = next(log_file)
except StopIteration:
break
new_row = {}
new_row["Connection"] = line[:4].strip()
new_row["IKE PEER"] = line[14:].strip()
line = next(log_file)
new_row["TYPE"] = line[14:30].strip()
new_row["ROLE"] = line[40:].strip()
line = next(log_file)
new_row["REKEY"] = line[14:30].strip()
new_row["STATE"] = line[40:].strip()
line = next(log_file)
new_row["ENCRYPT"] = line[14:30].strip()
new_row["HASH"] = line[40:].strip()
line = next(log_file)
new_row["AUTH"] = line[14:30].strip()
new_row["LIFETIME"] = line[40:].strip()
line = next(log_file)
new_row["LIFETIME REMAINING"] = line[24:].strip()
writer.writerow(new_row)
答案2
得分: 1
这段代码看起来有点蛮力,但在示例中可以工作。
https://onlinegdb.com/cJoLkXtzi9
import csv
header = ["Connection","IKE PEER","TYPE","REKEY","ENCRYPT","AUTH","ROLE","STATE","HASH","LIFETIME","LIFETIME REMAINING"]
with (
open('output.csv', 'w') as f,
open('log.txt', 'r') as file
):
mywriter = csv.writer(f, delimiter=',')
mywriter.writerow(header)
lines = file.readlines()
for line in range(0, len(lines), 6):
conn, ike, peer, *ip = lines.split()
row = lines.split()
typ, role = row[2], row[5]
row = lines.split()
key, stat = row[2], row[5]
row = lines.split()
enc, has = row[2], row[5]
row = lines.split()
auth, life = row[2], row[4]
rem = lines.split()[2]
ip = ' '.join(ip)
row = [conn, ip, typ, key, enc, auth, role, stat, has, life, rem]
mywriter.writerow(row)
英文:
kind of brute force but worked with sample.
https://onlinegdb.com/cJoLkXtzi9
import csv
header = ["Connection","IKE PEER","TYPE","REKEY","ENCRYPT","AUTH","ROLE","STATE","HASH","LIFETIME","LIFETIME REMAINING"]
with (
open('output.csv', 'w') as f,
open('log.txt', 'r') as file
):
mywriter = csv.writer(f, delimiter=',')
mywriter.writerow(header)
lines = file.readlines()
for line in range(0, len(lines), 6):
conn, ike, peer, *ip = lines.split()
row = lines.split()
typ, role = row[2], row[5]
row = lines.split()
key, stat = row[2], row[5]
row = lines.split()
enc, has = row[2], row[5]
row = lines.split()
auth, life = row[2], row[4]
rem = lines.split()[2]
ip = ' '.join(ip)
row = [conn, ip, typ, key, enc, auth, role, stat, has, life, rem]
mywriter.writerow(row)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论