有没有办法比较 json 中两个列表的元素?
Is there a way to compare the elements of two lists residing in a json?
我目前正在分析日志中的消息并将其打印出来。在我解释我的问题之前,请先看下面的程序。
count = 0
error_list = ["*SSH connection as gentan (using password) successful.*", "*Deployment State Updated to ATTEMPTING_CONNECTION*"]
for i in data['deploymentStatus']['page']:
count = count + 1
regex = re.compile(error_list)
if re.findall(regex, i['history']) is True:
string = """
========================
TARGET # %s
========================
[+] IP => %s
[+] HISTORY => %s""" % (count, i['address'], i['history'])
print(string)
这是 i['history'] 等于的代码。
i['history] = ["13/04/2021 05:42:59:589Z: Attempting connection via ssh as gentan.", "13/04/2021 05:42:59:589Z: Deployment State Updated to ATTEMPTING_CONNECTION"]
现在最后我想要的是,我希望 if 语句只打印 error_list 和 中相互匹配的日志我['history']
在我的例子中它不匹配的原因是因为它以日期和时间开头,python 中有没有办法解析它并只比较字符串?
如有任何帮助,我将不胜感激。我也可以 post 完整的 data['deploymentStatus']['page'] json 文件但是它太长 post 这里 12k+ 行.
您不需要使用正则表达式。只需使用 in
运算符来测试 error_list
中的字符串之一是否在历史字符串中。
error_list = ["SSH connection as gentan (using password) successful", "Deployment State Updated to ATTEMPTING_CONNECTION"]
for i in data['deploymentStatus']['page']:
if any(error in i['history'] for error in error_list):
print("""
========================
TARGET # %s
========================
[+] IP => %s
[+] HISTORY => %s""" % (count, i['address'], i['history']))
count = len(data['deploymentStatus']['page']) # no need to calculate this in the loop
我目前正在分析日志中的消息并将其打印出来。在我解释我的问题之前,请先看下面的程序。
count = 0
error_list = ["*SSH connection as gentan (using password) successful.*", "*Deployment State Updated to ATTEMPTING_CONNECTION*"]
for i in data['deploymentStatus']['page']:
count = count + 1
regex = re.compile(error_list)
if re.findall(regex, i['history']) is True:
string = """
========================
TARGET # %s
========================
[+] IP => %s
[+] HISTORY => %s""" % (count, i['address'], i['history'])
print(string)
这是 i['history'] 等于的代码。
i['history] = ["13/04/2021 05:42:59:589Z: Attempting connection via ssh as gentan.", "13/04/2021 05:42:59:589Z: Deployment State Updated to ATTEMPTING_CONNECTION"]
现在最后我想要的是,我希望 if 语句只打印 error_list 和 中相互匹配的日志我['history']
在我的例子中它不匹配的原因是因为它以日期和时间开头,python 中有没有办法解析它并只比较字符串?
如有任何帮助,我将不胜感激。我也可以 post 完整的 data['deploymentStatus']['page'] json 文件但是它太长 post 这里 12k+ 行.
您不需要使用正则表达式。只需使用 in
运算符来测试 error_list
中的字符串之一是否在历史字符串中。
error_list = ["SSH connection as gentan (using password) successful", "Deployment State Updated to ATTEMPTING_CONNECTION"]
for i in data['deploymentStatus']['page']:
if any(error in i['history'] for error in error_list):
print("""
========================
TARGET # %s
========================
[+] IP => %s
[+] HISTORY => %s""" % (count, i['address'], i['history']))
count = len(data['deploymentStatus']['page']) # no need to calculate this in the loop