我正在尝试将某些SSL发现扫描的结果输出为用于报告目的的表格,并且我在以我想要的方式解析数据时遇到了一些问题。
我希望输出看起来像这样:
IP Address Common Name Valid To
------------ ---------------------- ----------
10.0.255.250 ex.example.com 2017/02/09
10.0.255.251 localhost 2009/07/04
10.0.255.252 ex.example2.com 2016/05/24
相反,它看起来像这样。
IP Address Common Name Valid To
------------ ---------------------- ----------
10.0.255.250 ex.example.com 2017/02/09
IP Address Common Name Valid To
------------ ------------- ----------
10.0.255.251 localhost 2009/07/04
None
IP Address Common Name Valid To
------------ ------------- ----------
10.0.255.252 ex.example2.com 2016/05/2
我一直在尝试使用while循环来解决这个问题并且没有运气。它似乎不是一个"而#34;循环和更多的"为每个"循环。
def tabulateText():
loop = True
while loop == True:
with open("testinput.txt", "r") as text_file:
for line in text_file:
if "end" in line:
loop = False
elif "IP Address =" in line:
start = line.find('IP Address = ')
endline = line.find('\n', start)
ip = line[start+13:endline]
cert = SSLmon(ip)
Col1 = ip
FCS2 = cert.find('commonName')
FCE2 = cert.find('/', FCS2)
FCS2b = cert.find('commonName')
FCE2b = cert.find('\n', FCS2b)
Colopt2 = cert[FCS2+11:FCE2]
Colopt2b = cert[FCS2b+11:FCE2b]
Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b]
FCS3 = cert.find('Not valid after:')
FCE3 = cert.find('T', FCS3)
Col3 = cert[FCS3+18:FCE3].replace('-', '/')
column = Col1[n], Col2[n], Col3[n]
print(tabulate([column], headers=['IP Address', 'Common Name', 'Valid To']))
else:
pass
print(tabulateText())
print(tabulateText())
print(tabulateText())
答案 0 :(得分:2)
每次读取“IP地址”行后都会执行print(tabulate([column],headers ..)命令,这就是为什么你会看到标题和只有一行数据的原因。
您可以做的是将每行结果(您称之为“列”)附加到数组中。这将创建一个包含所有结果的表格。在阅读完文件的所有行后,执行print(tabulate())命令。。
首先,在读取文本文件之前创建一个空数组 - 我将其称为表:
table = []
with open("testinput.txt", "r") as text_file:
将列数组追加到表数组(table.append [column])并删除当前具有的print(tabulate())命令。
column = Col1[n], Col2[n], Col3[n]
table.append[column]
在读完文件并引用新变量“table”之后,在最后移动print(tabulate())命令。
print (tabulate(table), headers=['...'])
您不需要循环 - “for line”命令循环遍历文本文件。此外,完成后关闭文件(text_file.closed)
该功能将如下所示:
table = []
with open("testinput.txt", "r") as text_file:
for line in text_file:
if "IP Address =" in line:
start = line.find('IP Address = ')
endline = line.find('\n', start)
ip = line[start+13:endline]
cert = SSLmon(ip)
Col1 = ip
FCS2 = cert.find('commonName')
FCE2 = cert.find('/', FCS2)
FCS2b = cert.find('commonName')
FCE2b = cert.find('\n', FCS2b)
Colopt2 = cert[FCS2+11:FCE2]
Colopt2b = cert[FCS2b+11:FCE2b]
Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b]
FCS3 = cert.find('Not valid after:')
FCE3 = cert.find('T', FCS3)
Col3 = cert[FCS3+18:FCE3].replace('-', '/')
column = Col1[n], Col2[n], Col3[n]
table.append[column]
text_file.closed
print(tabulate([table], headers=['IP Address', 'Common Name', 'Valid To']))
表数组本身看起来像这样你是做print(table)命令:
[['10.0.255.250','ex.example.com','2017/02/09'],['10.0.255.251','localhost','2009/07/04'],[' 10.0.255.252' , 'ex.example2.com', '2016年5月24日']]
答案 1 :(得分:0)
我最终采用了不同的方法来获取我想要的数据。我终于打印出每个列,并使用由.ljust()
控制的填充。
with open("testinput.txt", "r") as text_file:
for line in text_file:
if "IP Address =" in line:
start = line.find('IP Address = ')
endline = line.find('\n', start)
ip = line[start+13:endline]
cert = SSLmon(ip)
Col1 = ip
FCS2 = cert.find('commonName')
FCE2 = cert.find('/', FCS2)
FCS2b = cert.find('commonName')
FCE2b = cert.find('\n', FCS2b)
Colopt2 = cert[FCS2+11:FCE2]
Colopt2b = cert[FCS2b+11:FCE2b]
Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b]
FCS3 = cert.find('Not valid after:')
FCE3 = cert.find('T', FCS3)
Col3 = cert[FCS3+18:FCE3].replace('-', '/')
with open(filename1, 'a') as f:
line = '%s %s %s %s' % (Col1.ljust(20), Col2.ljust(35), Col3.ljust(15), '\n')
f.write(line)