mirror of
https://github.com/ArchiveBox/ArchiveBox
synced 2024-11-22 20:23:12 +00:00
add ipc listener server script
This commit is contained in:
parent
64f68162de
commit
a61c9cfc14
1 changed files with 38 additions and 0 deletions
38
bin/docker_ipc_listener.py
Executable file
38
bin/docker_ipc_listener.py
Executable file
|
@ -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()
|
Loading…
Reference in a new issue