def invoke_shell(endpoint): ssh = websocket.create_connection(endpoint) _resize(ssh) oldtty = termios.tcgetattr(sys.stdin) old_handler = signal.getsignal(signal.SIGWINCH) def on_term_resize(signum, frame): _resize(ssh) signal.signal(signal.SIGWINCH, on_term_resize) try: tty.setraw(sys.stdin.fileno()) tty.setcbreak(sys.stdin.fileno()) rows, cols = _pty_size() ssh.send(json.dumps({'resize': {'width': cols, 'height': rows}})) while True: try: r, w, e = select.select([ssh.sock, sys.stdin], [], []) if ssh.sock in r: data = ssh.recv() if not data: break message = json.loads(data) if 'error' in message: raise ConnectionError(message['error']) sys.stdout.buffer.write(decode(message['data'])) sys.stdout.flush() if sys.stdin in r: x = sys.stdin.read(1) if len(x) == 0: break ssh.send(json.dumps({'data': encode(x)})) except (select.error, IOError) as e: if e.args and e.args[0] == errno.EINTR: pass else: raise except websocket.WebSocketException: raise finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty) signal.signal(signal.SIGWINCH, old_handler)
def _forward_inbound(self, channel): """ Forward inbound traffic (websockets -> ssh) """ try: while True: data = self._websocket.receive() if not data: return if six.PY3: data = str(data, 'utf-8') else: data = str(data) data = json.loads(data) if 'resize' in data: channel.resize_pty(data['resize'].get('width', 80), data['resize'].get('height', 24)) if 'data' in data: channel.send(decode(data['data'])) finally: self.close()
def _forward_inbound(self, channel): """ Forward inbound traffic (websockets -> ssh) """ try: while True: data = self._websocket.receive() if not data: return if six.PY3: data = str(data, 'utf-8') else: data = str(data) data = json.loads(data) if 'resize' in data: channel.resize_pty( data['resize'].get('width', 80), data['resize'].get('height', 24)) if 'data' in data: channel.send(decode(data['data'])) finally: self.close()