import subprocess import json import sys import io def proc_l(l): return l.replace('\n','').strip() arealist=['ap-northeast-1','ap-northeast-2','ap-southeast-1','us-east-1','eu-west-1','us-east-1','us-west-2'] for area in arealist: #area='us-east-1' cmd='aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=RunInstances --max-results 20 --region '+area cmds=cmd.split(' ') cl=subprocess.Popen(cmds, stdout=subprocess.PIPE) lst=[] js="{" for l in io.TextIOWrapper(cl.stdout, encoding="utf-8"): if '"EventId":' in l: js+=proc_l(l) lst.append(proc_l(l)) if '"EventTime":' in l: l2=proc_l(l) list1 = list(l2) list1[12]='"' l2 = ''.join(list1) l2+='"' js+=l2 js+="}" print(js) js2=json.loads(js) js2['area']=area print(js2) js="{" if '"AccessKeyId":' in l: js+=proc_l(l) lst.append(proc_l(l)) if '"EventName":' in l: js+=proc_l(l) lst.append(proc_l(l)) #lines = cl.stdout.readlines() #clstr=str(cl) #lines=clstr.split('\n') #print(lst) # print(l) # sys.exit() #js=json.loads(str(cl)) #print(js)