|
|
@ -7,26 +7,39 @@ from base64 import b64encode, b64decode |
|
|
|
from contextlib import closing |
|
|
|
from json import loads |
|
|
|
|
|
|
|
def read_socket(sock, chunk_size=1024): |
|
|
|
buf = [] |
|
|
|
|
|
|
|
while True: |
|
|
|
chunk = sock.recv(1024) |
|
|
|
buf.append(chunk) |
|
|
|
if b"\n" in chunk: |
|
|
|
break |
|
|
|
|
|
|
|
return b"".join(buf).decode("utf-8") |
|
|
|
|
|
|
|
def read_log(pid): |
|
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: |
|
|
|
s.connect("/tmp/shelltalk.sock") |
|
|
|
s.send(("read {0}\n".format(pid)).encode("utf-8")) |
|
|
|
|
|
|
|
buf = [] |
|
|
|
buf = read_socket(s) |
|
|
|
|
|
|
|
while True: |
|
|
|
chunk = s.recv(1024) |
|
|
|
buf.append(chunk) |
|
|
|
if b"\n" in chunk: |
|
|
|
break |
|
|
|
|
|
|
|
return [b64decode(l) for l in loads(b"".join(buf).decode("utf-8"))] |
|
|
|
return [b64decode(l) for l in loads(buf)] |
|
|
|
|
|
|
|
def spawn(pid): |
|
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: |
|
|
|
s.connect("/tmp/shelltalk.sock") |
|
|
|
s.send(("spawn {0}\n".format(pid)).encode("utf-8")) |
|
|
|
|
|
|
|
|
|
|
|
def list_pids(): |
|
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: |
|
|
|
s.connect("/tmp/shelltalk.sock") |
|
|
|
s.send("list\n".encode("utf-8")) |
|
|
|
|
|
|
|
return loads(read_socket(s)) |
|
|
|
|
|
|
|
def write(pid, msg): |
|
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: |
|
|
|
s.connect("/tmp/shelltalk.sock") |
|
|
@ -50,6 +63,11 @@ parser.add_argument("-R", |
|
|
|
nargs=1, |
|
|
|
help="Would you like to read some logs?") |
|
|
|
|
|
|
|
parser.add_argument("-L", |
|
|
|
"--list", |
|
|
|
action="store_true", |
|
|
|
help="List all of our shells") |
|
|
|
|
|
|
|
args = parser.parse_args() |
|
|
|
|
|
|
|
if args.spawn: |
|
|
@ -58,6 +76,9 @@ if args.spawn: |
|
|
|
if args.write: |
|
|
|
write(*args.write) |
|
|
|
|
|
|
|
if args.list: |
|
|
|
print(list_pids()) |
|
|
|
|
|
|
|
if args.read: |
|
|
|
for line in read_log(*args.read): |
|
|
|
print(line) |
|
|
|
print(line.decode("utf-8")) |
|
|
|