cl-test.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import subprocess
  2. import json
  3. import sys
  4. import io
  5. def proc_l(l):
  6. return l.replace('\n','').strip()
  7. arealist=['ap-northeast-1','ap-northeast-2','ap-southeast-1','us-east-1','eu-west-1','us-east-1','us-west-2']
  8. for area in arealist:
  9. #area='us-east-1'
  10. cmd='aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=RunInstances --max-results 20 --region '+area
  11. cmds=cmd.split(' ')
  12. cl=subprocess.Popen(cmds, stdout=subprocess.PIPE)
  13. lst=[]
  14. js="{"
  15. for l in io.TextIOWrapper(cl.stdout, encoding="utf-8"):
  16. if '"EventId":' in l:
  17. js+=proc_l(l)
  18. lst.append(proc_l(l))
  19. if '"EventTime":' in l:
  20. l2=proc_l(l)
  21. list1 = list(l2)
  22. list1[12]='"'
  23. l2 = ''.join(list1)
  24. l2=l2.replace(',','')
  25. l2+='"'
  26. js+=l2
  27. js+="}"
  28. print(js)
  29. js2=json.loads(js)
  30. js2['area']=area
  31. print(js2)
  32. js="{"
  33. if '"AccessKeyId":' in l:
  34. js+=proc_l(l)
  35. lst.append(proc_l(l))
  36. if '"EventName":' in l:
  37. js+=proc_l(l)
  38. lst.append(proc_l(l))
  39. #lines = cl.stdout.readlines()
  40. #clstr=str(cl)
  41. #lines=clstr.split('\n')
  42. #print(lst)
  43. # print(l)
  44. # sys.exit()
  45. #js=json.loads(str(cl))
  46. #print(js)