瀏覽代碼

add ipc listener server script

Nick Sweeting 1 年之前
父節點
當前提交
a61c9cfc14
共有 1 個文件被更改,包括 38 次插入0 次删除
  1. 38 0
      bin/docker_ipc_listener.py

+ 38 - 0
bin/docker_ipc_listener.py

@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+
+# Allow another docker container to run commands on this container
+# This is the script to run on the server container.
+# The client can connect and run a command like so:
+#    $ echo whoami | nc servercontainername 2222
+#    root
+
+import socket
+import subprocess as sp
+from datetime import datetime
+
+LISTEN_PORT = 2222
+
+s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+s1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+s1.bind(("0.0.0.0", LISTEN_PORT))
+s1.listen(1)
+print("Listening for shell commands on 0.0.0.0:2222", flush=True)
+
+conn, addr = s1.accept()
+while True:
+    cmd = conn.recv(1024).decode()
+    if not cmd:
+        conn, addr = s1.accept()
+        continue
+
+    timestamp = datetime.now().isoformat()
+    client_ip, client_port = conn.getsockname()
+    print(f'\n[{timestamp}][{client_ip}:{client_port}] $', cmd)
+
+    with sp.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE, bufsize=1, universal_newlines=True) as p:
+        for line in p.stdout:
+            print(line.strip(), flush=True)
+            conn.sendall(line.encode("utf-8"))
+
+    conn.close()
+    conn, addr = s1.accept()