python_changetxt.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # lambda_code
  2. import json
  3. import boto3
  4. def lambda_handler(event,context):
  5. client = boto3.client('ssm')
  6. try:
  7. msg =json.loads(event['body'])
  8. sg = msg['secret_code']
  9. print("secret " + sg)
  10. client.put_parameter(Name="Secret",Value=sg,Type='String',Overwrite=True)
  11. except:
  12. print("NO")
  13. return {
  14. "statusCode": 200,
  15. "headers": {
  16. "Content-Type": "application/json",
  17. },
  18. "body": json.dumps(event, indent=4),
  19. }
  20. # -----------------------------
  21. import boto3
  22. client = boto3.client('ssm')
  23. respond = client.get_parameter(Name='Secret')
  24. code_ori = respond['Parameter']['Value']
  25. def alter(file,old_str,new_str):
  26. file_data = ""
  27. with open(file, "r", encoding="utf-8") as f:
  28. for line in f:
  29. if old_str in line:
  30. line = line.replace(line,new_str)
  31. file_data += line
  32. with open(file,"w",encoding="utf-8") as f:
  33. f.write(file_data)
  34. f.close()
  35. code_pro = code_ori.replace("'", '')
  36. code = f'secret_code = "{code_pro}"'
  37. alter("/conf.toml", "secret_code", cod
  38. #---------V2============
  39. import boto3
  40. import time
  41. client = boto3.client('ssm')
  42. def alter(file,old_str,new_str):
  43. file_data = ""
  44. with open(file, "r", encoding="utf-8") as f:
  45. for line in f:
  46. if old_str in line:
  47. line = line.replace(line,new_str)
  48. file_data += line
  49. with open(file,"w",encoding="utf-8") as f:
  50. f.write(f'[DEFAULT]\nlog_path = "/root/"\n{file_data}')
  51. # f.write(file_data)
  52. f.close()
  53. def old_read(file,old_str):
  54. with open(file, "r", encoding="utf-8") as f:
  55. for line in f:
  56. if old_str in line:
  57. return line
  58. while True:
  59. global old
  60. respond = client.get_parameter(Name='Secret')
  61. code_ori = respond['Parameter']['Value']
  62. code_pro = code_ori.replace("'", '')
  63. code = f'secret_code = "{code_pro}"'
  64. time.sleep(3)
  65. old = old_read("/conf.toml", "secret_code")
  66. if old == code:
  67. print('NO')
  68. else:
  69. print('OK')
  70. alter("/conf.toml", "secret_code", code)