MCPcopy Create free account
hub / github.com/aluzzardi/wssh / invoke_shell

Function invoke_shell

wssh/client.py:44–87  ·  view source on GitHub ↗
(endpoint)

Source from the content-addressed store, hash-verified

42
43
44def invoke_shell(endpoint):
45 ssh = websocket.create_connection(endpoint)
46 _resize(ssh)
47 oldtty = termios.tcgetattr(sys.stdin)
48 old_handler = signal.getsignal(signal.SIGWINCH)
49
50 def on_term_resize(signum, frame):
51 _resize(ssh)
52 signal.signal(signal.SIGWINCH, on_term_resize)
53
54 try:
55 tty.setraw(sys.stdin.fileno())
56 tty.setcbreak(sys.stdin.fileno())
57
58 rows, cols = _pty_size()
59 ssh.send(json.dumps({'resize': {'width': cols, 'height': rows}}))
60
61 while True:
62 try:
63 r, w, e = select.select([ssh.sock, sys.stdin], [], [])
64 if ssh.sock in r:
65 data = ssh.recv()
66 if not data:
67 break
68 message = json.loads(data)
69 if 'error' in message:
70 raise ConnectionError(message['error'])
71 sys.stdout.write(message['data'])
72 sys.stdout.flush()
73 if sys.stdin in r:
74 x = sys.stdin.read(1)
75 if len(x) == 0:
76 break
77 ssh.send(json.dumps({'data': x}))
78 except (select.error, IOError) as e:
79 if e.args and e.args[0] == errno.EINTR:
80 pass
81 else:
82 raise
83 except websocket.WebSocketException:
84 raise
85 finally:
86 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
87 signal.signal(signal.SIGWINCH, old_handler)

Callers

nothing calls this directly

Calls 3

_resizeFunction · 0.85
_pty_sizeFunction · 0.85
ConnectionErrorClass · 0.85

Tested by

no test coverage detected