vi / websocat

Command-line client for WebSockets, like netcat (or curl) for ws:// with advanced socat-like functions
MIT License
7.17k stars 278 forks source link

Connecting to shell and tty raw mode #60

Open paride opened 5 years ago

paride commented 5 years ago

I'm using websocat to interactively connect to the remote serial console of an OpenStack instance. It kind of works: I can get to the shell and use it, however this is made awkward by the local buffering (I think). When I type a command, e.g. ls -l, this is locally echoed, and sent to the remote side only when I press Enter. Then the remote side echoes is, resulting in it being printed twice. This is annoying and breaks a few other things (editing files in vim, arrow keys).

I normally run websocat like this:

websocat --protocol binary --binary 'wss://...'

I tried to add --buffer-size=1 to the command line option, hoping to get any single keypress to be send immediately to the remote side, but this doesn't seem to work. I get a response from the remote side only after pressing Enter. Is --buffer-size broken in binary mode?

If I get this to work, I'll need to disable the local echo of typed text. Is there a way to do this with the current version of websocat? Thanks!

vi commented 5 years ago

Currently there is no websocat option to set terminal parameters (e.g. disabling local echo or turning off Ctrl+C handler locally). You need to use stty for this.

Maybe I'll provide a demo of running shell over websocat in a while. Maybe websocat can gain options for this, especially if readline mode would be implemented.

vi commented 5 years ago

Shell server: websocat -b -E ws-l:127.0.0.1:1234 sh-c:'reptyr -L bash'

Shell client: stty raw -echo; websocat -E -b ws://127.0.0.1:1234/; stty sane cooked

Results in a proper shell with job control, interrupting and ability to use curses apps.

vi commented 5 years ago

Is --buffer-size broken in binary mode?

I don't expect it to be it. It is just default mode of operation of psedo-terminals: apps only see input when you press Enter. Even with buffer size 1, program won't get a byte per pressed key unless terminal is set to raw mode, which stty raw does.

You don't need --buffer-size=1 for interactive shell. Websocat supports short reads and turns input it receives into a message even if it is doesn't fill the buffer completely.

paride commented 5 years ago

Thanks @vi, this is super useful! I don't have control on the remote side, however running:

tty raw; stty -echo; websocat -E --protocol binary -b  'wss://...'

did it. I think it would be of great help to document this. Feel free to close this issue, or keep it open as a documentation issue if you prefer.

EfraimChu commented 3 years ago

Shell server: websocat -b -E ws-l:127.0.0.1:1234 sh-c:'reptyr -L bash'

Shell client: stty raw -echo; websocat -E -b ws://127.0.0.1:1234/; stty sane cooked

Results in a proper shell with job control, interrupting and ability to use curses apps.

i use the command on iterm2; it works;

But when I type the arrow keys, terminal show up=> ^[[A down=> ^[[B left=> ^[[D right=> ^[[C

root@wmsv2-api-86444c4865-fftkv:/home# echo 11

echo 11 11 root@wmsv2-api-86444c4865-fftkv:/home# root@wmsv2-api-86444c4865-fftkv:/home# ^[[A echo 11 11

Is there any way to solve this problem? thanks

vi commented 3 years ago

Have you tried socat-based helper as I suggested in #108?