【发布时间】:2018-11-04 01:51:54
【问题描述】:
我在 AWS Lambda 上编写了一些代码,并试图从 GuardDuty 的调查结果中提取 IP。我的 FindingId 没问题,但是当我尝试提取 IP 地址时,我收到以下错误:
{ "errorMessage": "列表索引必须是整数或切片,不能 str”,“errorType”:“TypeError”,“stackTrace”:[ [ "/var/task/lambda_function.py", 38, "lambda_handler", “打印(loadFindings['Findings']['Resource']['NetworkInterfaces']['PublicIp'])” ] ] }
到目前为止,我的完整代码如下:
import json
import boto3
from pprint import pprint # Pretty-print for displaying the JSON nicely.
#pprint(listOfFindings)
def lambda_handler(event, context):
client = boto3.client('guardduty') # Creating the client.
Det_ID = '5ab1b6808e98faaabd947a01af9ed970' # Setting the Detect ID for GD.
response = client.list_findings(DetectorId=Det_ID) # Gathering all findings... Need to filter.
findings = json.dumps(response) # Dumping the JSON findings
listOfFindings = json.loads(findings) # Making them into a readable format for Python.
# print("Here's the IDs!",listOfFindings['FindingIds'],"\n\n\n") # Printing all Finding IDs.
idPosition=0
idList = []
for id in listOfFindings['FindingIds']: # Looping through all the Finding IDs.
#print("\n\n\nNumber", x, listOfFindings['FindingIds'][x]) # Prints all the Finding Ids separated.
idList.append(listOfFindings['FindingIds'][idPosition])
idPosition+=1
# print("TEST") - Debugging.
# print(idList) - Debugging.
findingsList = []
position = 0
for ids in idList:
# print(idList[position])
stringFindingId = str(idList[position])
#stringFindingId = idList[position]
allFindings = client.get_findings(
DetectorId=Det_ID,
FindingIds=[
stringFindingId,])
dumpFindings = json.dumps(allFindings)
loadFindings = json.loads(dumpFindings)
# findingsList.append(loadFindings)
print(loadFindings['Findings']['Resource']['NetworkInterfaces']['PublicIp']) # BROKEN HERE
position += 1
非常感谢任何帮助!
【问题讨论】:
-
你能发一个
loadFindings的样本吗?
标签: python json amazon-web-services aws-lambda