浏览代码

add py/k12-ssm-osPopen.py.

xdc 5 年之前
父节点
当前提交
605d9a0c0a
共有 1 个文件被更改,包括 71 次插入0 次删除
  1. 71 0
      py/k12-ssm-osPopen.py

+ 71 - 0
py/k12-ssm-osPopen.py

@@ -0,0 +1,71 @@
+# lambda_code
+
+import json
+import boto3
+
+def lambda_handler(event,context):
+    client = boto3.client('ssm')
+    try:
+        msg =json.loads(event['body'])
+        sg = msg['secret_code']
+        print("secret " + sg)
+        client.put_parameter(Name="Secret",Value=sg,Type='String',Overwrite=True)
+    except:
+        print("NO")
+    return {
+        "statusCode": 200,
+        "headers": {
+            "Content-Type": "application/json",
+        },
+        "body": json.dumps(event, indent=4),
+    }
+
+
+
+
+# -----------------------------
+
+
+import boto3
+import time
+import os
+client = boto3.client('ssm')
+
+def alter(file,old_str,new_str):
+	file_data = ""
+
+	with open(file, "r", encoding="utf-8") as f:
+		for line in f:
+			if old_str in line:
+				line = line.replace(line,new_str)
+				file_data += line
+	with open(file,"w",encoding="utf-8") as f:
+		f.write(f'[DEFAULT]\nlog_path = "/root/"\n{file_data}')
+		# f.write(file_data)
+		f.close()
+def old_read(file,old_str):
+	with open(file, "r", encoding="utf-8") as f:
+		for line in f:
+			if old_str in line:
+				return line		
+		
+while True:
+	global old
+	respond = client.get_parameter(Name='Secret')
+	code_ori = respond['Parameter']['Value']		
+	code_pro = code_ori.replace("'", '')
+	code = f'secret_code = "{code_pro}"'
+	time.sleep(3)
+	old = old_read("/conf.toml", "secret_code")
+	if old == code:
+		print('NO')
+	else:
+		os.popen('killall server_k12')
+		alter("/conf.toml", "secret_code", code)
+		os.system('nohup /root/server_k12 /conf.toml &')
+		print('OK===========')
+
+
+
+
+