vi / websocat

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

websocat script #68

Closed giruzgz closed 4 years ago

giruzgz commented 4 years ago

Hi! Good job with the tool! I need a help to use it in bash script. I would like to open a websocket connection from Script, write to the socket at any moment and read and save in file.

I achieved send information to the socket dinamically using a webscoket server and redirection it like this: websocat -t ws-l:127.0.0.1:1234 mirror:WebsiteWithSocket echo 'Hello world' | nc -q 1 127.0.0.1 1234

How could save the response to file from my bash script? Thanks. Good job again!

vi commented 4 years ago

websocat -t ws-l:127.0.0.1:1234 mirror:WebsiteWithSocket

websocat: MirrorClass-specifer requires no parameters. `WebsiteWithSocket` is not needed

Why mirror: at all? What do you mean by WebsiteWithSocket?

echo 'Hello world' | nc -q 1 127.0.0.1 1234

You are sending a plaintext request to a socket that is supposedly opened by Websocat in websocket (not plaintext) mode (ws-l: part).


You you want a persistent client connection to a WebSocket server that can be reused multiple times in a Bash script, you can use something like this:

$ websocat -t -E tcp-l:127.0.0.1:1234  reuse-raw:ws://echo.websocket.org --max-messages-rev 1&
[1] 864
$ echo 'Hello 1' | nc 127.0.0.1 1234
Hello 1
$ echo 'World 2' | nc 127.0.0.1 1234
World 2
$ kill %1
$
[1]+  Terminated              websocat -t -E tcp-l:127.0.0.1:1234 reuse-raw:ws://echo.websocket.org --max-messages-rev 1

If your Websocat has no --max-messages-rev option, you can omit it and use -q1 in netcats.

giruzgz commented 4 years ago

Sorry for my english.

What do you mean by WebsiteWithSocket?

Sockets

Yes, I need a code like this but, how can capture the response of socket to file or to the script?

vi commented 4 years ago

I don't understand what do you want to attain.

a code like this

Like what? Like the reuse-raw: variant?

What do you mean by WebsiteWithSocket? Sockets

This reply does not clarify anything.

capture the response of socket to file or to the script

Do you mean "how do I convert interactive session that demonstrates concept into a script which does it automatically"?


Try to write the script yourself, with extended comments describing what do you expect to happen at what stage. Maybe I can help then.

giruzgz commented 4 years ago

sorry, I will try to explain myself better.

I have a bash script in which I want to constantly receive information and, on occasion, transmit it.

To transmit it, with the code you have given me it works for me. This Code:

$ websocat -t -E tcp-l:127.0.0.1:1234  reuse-raw:ws://echo.websocket.org --max-messages-rev 1&
[1] 864
$ echo 'Hello 1' | nc 127.0.0.1 1234
Hello 1
$ echo 'World 2' | nc 127.0.0.1 1234
World 2

The problem is how to receive information through the same channel and enter it in a bash variable or save it to a file. The code I would use would be like the following, although it doesn't work. websocat -t -E tcp-l:127.0.0.1:1234 reuse-raw:ws://echo.websocket.org --max-messages-rev 1& > output.txt Or websocat -t -E tcp-l:127.0.0.1:1234 reuse-raw:ws://echo.websocket.org --max-messages-rev writefile:output.txt 1& Or output=websocat -t -E tcp-l:127.0.0.1:1234 reuse-raw:ws://echo.websocket.org --max-messages-rev 1& echo $output

vi commented 4 years ago
$ websocat -t -u  tcp-l:127.0.0.1:1234 reuse-raw:- | websocat ws://echo.websocket.org > /tmp/output.txt&
[1] 15073
$ echo qwer | nc 127.0.0.1 1234
$ echo aaaassss | nc 127.0.0.1 1234
$ echo 43434 | nc 127.0.0.1 1234
$ cat /tmp/output.txt
qwer
aaaassss
43434
$ kill %1
$
[1]+  Terminated              websocat -t -u tcp-l:127.0.0.1:1234 reuse-raw:- | websocat ws://echo.websocket.org > /tmp/output.txt

?

giruzgz commented 4 years ago

Wow! Great! It's working! Thanks!! :D

Foster024 commented 4 years ago

Hello! Help me please. Your example works well with ws: //echo.websocket.org but bad with wss: //irc-ws.chat.twitch.tv After starting websocat

`websocat -vv -t -u tcp-l:127.0.0.1:1234 reuse-raw:- | websocat --ping-interval 3 -vv wss://irc-ws.chat.twitch.tv [INFO websocat::lints] Auto-inserting the line mode [DEBUG websocat] Done third phase of interpreting options. [DEBUG websocat] Done fourth phase of interpreting options. [DEBUG websocat] Preparation done. Now actually starting. [INFO websocat::sessionserve] Serving Line2Message(Stdio) to Message2Line(WsClient("wss://irc-ws.chat.twitch.tv/")) with Options { websocket_text_mode: true, websocket_protocol: None, websocket_reply_protocol: None, udp_oneshot_mode: false, unidirectional: false, unidirectional_reverse: false, exit_on_eof: false, oneshot: false, unlink_unix_socket: false, exec_args: [], ws_c_uri: "ws://0.0.0.0/", linemode_strip_newlines: false, linemode_strict: false, origin: None, custom_headers: [], custom_reply_headers: [], websocket_version: None, websocket_dont_close: false, one_message: false, no_auto_linemode: false, buffer_size: 65536, broadcast_queue_len: 16, read_debt_handling: Warn, linemode_zero_terminated: false, restrict_uri: None, serve_static_files: [], exec_set_env: false, reuser_send_zero_msg_on_disconnect: false, process_zero_sighup: false, process_exit_sighup: false, socks_destination: None, auto_socks5: None, socks5_bind_script: None, tls_domain: None, tls_insecure: false, headers_to_env: [], max_parallel_conns: None, ws_ping_interval: Some(3), ws_ping_timeout: None } [INFO websocat::stdio_peer] get_stdio_peer (async) [INFO websocat::stdio_peer] Setting stdin to nonblocking mode [INFO websocat::stdio_peer] Setting stdout to nonblocking mode [INFO websocat::stdio_peer] Installing signal handler [DEBUG websocat::sessionserve] Underlying connection established [INFO websocat::ws_client_peer] get_ws_client_peer [DEBUG websocat] Done third phase of interpreting options. [DEBUG websocat] Done fourth phase of interpreting options. [DEBUG websocat] Preparation done. Now actually starting. [INFO websocat::sessionserve] Serving TcpListen(V4(127.0.0.1:1234)) to Reuser(Stdio) with Options { websocket_text_mode: true, websocket_protocol: None, websocket_reply_protocol: None, udp_oneshot_mode: false, unidirectional: true, unidirectional_reverse: false, exit_on_eof: false, oneshot: false, unlink_unix_socket: false, exec_args: [], ws_c_uri: "ws://0.0.0.0/", linemode_strip_newlines: false, linemode_strict: false, origin: None, custom_headers: [], custom_reply_headers: [], websocket_version: None, websocket_dont_close: false, one_message: false, no_auto_linemode: false, buffer_size: 65536, broadcast_queue_len: 16, read_debt_handling: Warn, linemode_zero_terminated: false, restrict_uri: None, serve_static_files: [], exec_set_env: false, reuser_send_zero_msg_on_disconnect: false, process_zero_sighup: false, process_exit_sighup: false, socks_destination: None, auto_socks5: None, socks5_bind_script: None, tls_domain: None, tls_insecure: false, headers_to_env: [], max_parallel_conns: None, ws_ping_interval: None, ws_ping_timeout: None } [INFO websocat::ws_client_peer] Connected to ws [DEBUG websocat::ws_client_peer] Starting pinger [INFO websocat::ws_peer] Sending WebSocket ping [INFO websocat::ws_peer] Received a pong from websocket [INFO websocat::ws_peer] Sending WebSocket ping [INFO websocat::ws_peer] Received a pong from websocket [INFO websocat::ws_peer] Sending WebSocket ping [INFO websocat::ws_peer] Received a pong from websocket [INFO websocat::ws_peer] Sending WebSocket ping [INFO websocat::ws_peer] Received a pong from websocket [INFO websocat::net_peer] Incoming TCP connection from Some(V4(127.0.0.1:47292)) [INFO websocat::sessionserve] Serving 1 ongoing connections [INFO websocat::primitive_reuse_peer] Initializing [INFO websocat::stdio_peer] get_stdio_peer (async) [INFO websocat::stdio_peer] Setting stdout to nonblocking mode [INFO websocat::stdio_peer] Installing signal handler [DEBUG websocat::my_copy] zero len [DEBUG websocat::my_copy] read_done [DEBUG websocat::my_copy] done [INFO websocat::sessionserve] Forward finished [INFO websocat::sessionserve] Forward shutdown finished [DEBUG websocat::ws_peer] incoming text PONG :tmi.twitch.tv [INFO websocat::ws_peer] Sending WebSocket ping [INFO websocat::ws_peer] Received a pong from websocket [INFO websocat::ws_peer] Sending WebSocket ping [INFO websocat::ws_peer] Received a pong from websocket [INFO websocat::ws_peer] Sending WebSocket ping [INFO websocat::ws_peer] Received a pong from websocket [INFO websocat::ws_peer] Sending WebSocket ping [INFO websocat::ws_peer] Received a pong from websocket [INFO websocat::ws_peer] Sending WebSocket ping [INFO websocat::ws_peer] Received a pong from websocket [INFO websocat::net_peer] Incoming TCP connection from Some(V4(127.0.0.1:47412)) [INFO websocat::sessionserve] Serving 1 ongoing connections [INFO websocat::primitive_reuse_peer] Reusing [DEBUG websocat::my_copy] zero len [DEBUG websocat::my_copy] read_done [DEBUG websocat::my_copy] done [INFO websocat::sessionserve] Forward finished [INFO websocat::sessionserve] Forward shutdown finished [DEBUG websocat::ws_peer] incoming text PONG :tmi.twitch.tv [DEBUG websocat::ws_peer] incoming None [DEBUG websocat::my_copy] BrokenPipe: read_done [DEBUG websocat::my_copy] done [INFO websocat::sessionserve] Reverse finished [INFO websocat::sessionserve] Reverse shutdown finished [DEBUG websocat::ws_peer] Pinger aborted [INFO websocat::net_peer] Incoming TCP connection from Some(V4(127.0.0.1:47446)) [INFO websocat::sessionserve] Serving 1 ongoing connections [INFO websocat::primitive_reuse_peer] Reusing [DEBUG websocat::my_copy] zero len [DEBUG websocat::my_copy] read_done [DEBUG websocat::my_copy] done [INFO websocat::sessionserve] Forward finished [INFO websocat::sessionserve] Forward shutdown finished [INFO websocat::net_peer] Incoming TCP connection from Some(V4(127.0.0.1:47494)) [INFO websocat::sessionserve] Serving 1 ongoing connections [INFO websocat::primitive_reuse_peer] Reusing [DEBUG websocat::my_copy] zero len [DEBUG websocat::my_copy] read_done [DEBUG websocat::my_copy] done [INFO websocat::sessionserve] Forward finished [INFO websocat::sessionserve] Forward shutdown finished [INFO websocat::net_peer] Incoming TCP connection from Some(V4(127.0.0.1:47500)) [INFO websocat::sessionserve] Serving 1 ongoing connections [INFO websocat::primitive_reuse_peer] Reusing [DEBUG websocat::my_copy] zero len [DEBUG websocat::my_copy] read_done [DEBUG websocat::my_copy] done [INFO websocat::sessionserve] Forward finished [INFO websocat::sessionserve] Forward shutdown finished [DEBUG websocat::ws_peer] drop WsWriteWrapper [DEBUG websocat::stdio_peer] restore_blocking_status [INFO websocat::stdio_peer] Restoring blocking status for stdin [INFO websocat::stdio_peer] Restoring blocking status for stdout websocat: WebSocketError: I/O failure [DEBUG websocat::stdio_peer] restore_blocking_status [INFO websocat::stdio_peer] Restoring blocking status for stdin [INFO websocat::stdio_peer] Restoring blocking status for stdout websocat: error running [INFO websocat::net_peer] Incoming TCP connection from Some(V4(127.0.0.1:47528)) [INFO websocat::sessionserve] Serving 1 ongoing connections [INFO websocat::primitive_reuse_peer] Reusing websocat: Broken pipe (os error 32) [INFO websocat::net_peer] Incoming TCP connection from Some(V4(127.0.0.1:47542)) [INFO websocat::sessionserve] Serving 1 ongoing connections [INFO websocat::primitive_reuse_peer] Reusing websocat: Broken pipe (os error 32) [INFO websocat::net_peer] Incoming TCP connection from Some(V4(127.0.0.1:47556)) [INFO websocat::sessionserve] Serving 1 ongoing connections [INFO websocat::primitive_reuse_peer] Reusing websocat: Broken pipe (os error 32) [INFO websocat::net_peer] Incoming TCP connection from Some(V4(127.0.0.1:47562)) [INFO websocat::sessionserve] Serving 1 ongoing connections [INFO websocat::primitive_reuse_peer] Reusing websocat: Broken pipe (os error 32)

In secundary terminal i just type: echo PING | nc 127.0.0.1 1234`

vi commented 4 years ago

For me it seems to be working:

In one window:
$ websocat -u  -t tcp-l:127.0.0.1:1234 reuse-raw:- | websocat --ping-interval 3  wss://irc-ws.chat.twitch.tv
PONG :tmi.twitch.tv
PONG :tmi.twitch.tv

In other window:
$ echo PING | nc -q 1 127.0.0.1 1234
$ echo PING | nc -q 1 127.0.0.1 1234

I also see some PONG :tmi.twitch.tv lines in the log you included as well. Maybe you want to receive PONGs in netcat itself, not directly from Websocat? In this case it should be websocat -E --ping-interval 3 -t tcp-l:127.0.0.1:1234 reuse-raw:wss://irc-ws.chat.twitch.tv.

vi commented 4 years ago

Note: there is shiny new feature of Websocat you may be interested in: server-sent-events + POST requests combination:

In the first window:
$ target/debug/websocat -E -t http-post-sse:tcp-l:127.0.0.1:8080 reuse:wss://irc-ws.chat.twitch.tv

In the second window:
$ curl -d$'PING :qwe' http://127.0.0.1:8080/
$ curl -d$'NICK :qwe' http://127.0.0.1:8080/

In the third window:
$ curl -N http://127.0.0.1:8080/
data: :tmi.twitch.tv PONG tmi.twitch.tv :qwe
data:

data: :tmi.twitch.tv NOTICE * :Improperly formatted auth
data:

This feature is not yet tested properly and requires building Websocat yourself.