python_changetxt.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. import time
  23. import os
  24. client = boto3.client('ssm')
  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(f'[DEFAULT]\nlog_path = "/root/"\n{file_data}')
  34. # f.write(file_data)
  35. f.close()
  36. def old_read(file,old_str):
  37. with open(file, "r", encoding="utf-8") as f:
  38. for line in f:
  39. if old_str in line:
  40. return line
  41. while True:
  42. global old
  43. respond = client.get_parameter(Name='Secret')
  44. code_ori = respond['Parameter']['Value']
  45. code_pro = code_ori.replace("'", '')
  46. code = f'secret_code = "{code_pro}"'
  47. time.sleep(3)
  48. old = old_read("/conf.toml", "secret_code")
  49. if old == code:
  50. print('NO')
  51. else:
  52. # os.popen('killall server_k12')
  53. os.popen("kill -9 `lsof -i:7777|awk '{print $2}'|awk 'END {print}'`")
  54. alter("/conf.toml", "secret_code", code)
  55. os.system('nohup /server /conf.toml &')
  56. print('OK===========')
  57. #===
  58. #!/bin/bash
  59. cat >>/etc/yum.repos.drhel7.repo<<OK
  60. [rhel7]
  61. name=this
  62. baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
  63. enabled=1
  64. gpgcheck=0
  65. OK
  66. yum clean all
  67. yum makecache
  68. yum install -y lsof
  69. nohup python3 /secret.py &
  70. chmod +x /server
  71. /server /conf.toml