#!/usr/bin/env bash
# porcmd <command...> : send one line to the running session driver.
#
# The port is $POR_CMD_PORT, and 6600 -- the human's -- when that is unset.
# A pooled session prints its own on the line beginning `slot N:`; slot 1's
# is 6561.  Without this every agent driving a pooled session wrote its own
# copy of the six lines below.
python3 - "$@" <<'PY'
import os,socket,sys
port=int(os.environ.get("POR_CMD_PORT") or 6600)
s=socket.create_connection(("127.0.0.1",port),timeout=300); s.settimeout(300)
s.sendall((" ".join(sys.argv[1:])+"\n").encode())
out=b""
while True:
    d=s.recv(65536)
    if not d: break
    out+=d
sys.stdout.write(out.decode("latin1"))
PY
