Skip to content

Instantly share code, notes, and snippets.

@baude
Created May 22, 2018 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baude/f60b7759f81f82cbf42f0649351e0fad to your computer and use it in GitHub Desktop.
Save baude/f60b7759f81f82cbf42f0649351e0fad to your computer and use it in GitHub Desktop.
import socket
import select, sys, os, tty, atexit, termios
sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
# stdin 1
# stdout 2
# stderr 3
def reset_fd(my_fd, old_settings):
termios.tcsetattr(my_fd, termios.TCSADRAIN, old_settings)
os.close(my_fd)
if len(sys.argv) != 2:
print("pass the path to the container's attach socket")
sys.exit(1)
sock.connect(sys.argv[1])
stdin = os.open("/dev/stdin", os.O_RDWR)
old_settings = termios.tcgetattr(stdin)
atexit.register(reset_fd, stdin, old_settings)
tty.setraw(stdin)
inputs = [sock, stdin]
outputs = []
while inputs:
readable, writable, exceptional = select.select(inputs, outputs, inputs)
for r in readable:
if isinstance(r, socket.socket):
data = r.recv(1024)
os.write(2, data[1:])
if isinstance(r, int):
data = os.read(stdin, 1024)
if 29 in data: # Bail on ctrl-[
sys.exit(0)
sock.send(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment