How to Read Websocket Response in Linux Shell

How to read websocket response in linux shell

Well, you can try to mimic the required headers to get some response using curl:

  • https://gist.github.com/htp/fbce19069187ec1cc486b594104f01d0 or
  • Linux Bash: How to open a websocket connection as client

Also, there are other ways to communicate with a WebSocket server, e.g.

  • https://github.com/websockets/wscat
  • https://github.com/bwasti/webpipe
  • https://github.com/progrium/wssh

Linux Bash: How to open a websocket connection as client

My tool websocat is specifically designed for this.

websocat ws://your_server/url

You can connect and exchange data with your server. By default each line becomes a WebSocket text message and vice versa.

On Linux it is more comfortable to play with it using readline:

rlwrap websocat ws://your_server/url.

It is not the only CLI websocket client. There are also "ws" and "wscat" projects.

Opening a socket to see output with bash

If you version of bash supports networking

#!/bin/bash
set -u
host="$1"
port="$2"
path="$3"
exec 3<>/dev/tcp/$host/$port
printf '%s\r\n' "$path" >&3
cat <&3

if you are hitting a HTTP server the probably you have to pass the GET requests as path, something like

script example.com 80 $'GET / HTTP/1.1\r\nConnection: close\r\n'

How do I connect to a websocket manually, with netcat/socat/telnet?

I think you want to modify the socket stream to translate \n (line feed) to CRLF (Carriage return & line feed). Doing info socat produces detailed information which includes this modifier:

crnl   Converts the default line termination character NL ('\n',  0x0a)
to/from CRNL ("\r\n", 0x0d0a) when writing/reading on this chan-
nel (example). Note: socat simply strips all CR characters.

So I think you should be able to do this:

socat - TCP:echo.websocket.org:80,crnl

Websocket client on linux cuts off response after 8192 bytes

So it turns out the problem came from the provided websocket module from google cloud sdk. It has a bug where after 8192 bytes it will not continue to read from the socket. This can be fixed by supplying the websocket library maintained by Hiroki Ohtani earlier on your PYTHONPATH than the google cloud sdk.

How do I send a message to my socket.io websocket from the command line in linux?

You can write a simple client like this (let's name it client with no extension):

#!/usr/bin/env node
const socket = require('socket.io-client')('http://localhost:3000');
const someDelay = 10;
socket.on('connect', function () {
console.log('connected...');
if (process.argv[2] && process.argv[3]) {
console.log('sending ' + process.argv[2] + ': ' + process.argv[3]);
socket.emit(process.argv[2], process.argv[3]);
setTimeout(() => {
process.exit(0);
}, someDelay);
} else {
console.log('usage: ./client.js <event> <data>');
process.exit(1);
}
});

with a very basic package.json

{
"name": "client",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "client"
},
"dependencies": {
"socket.io-client": "^1.4.6"
}
}

Then run npm install, give client permissions to be executed and you can run it with (for example) ./client message testControl

What do you think? ;)

How to view WS/WSS Websocket request content using Firebug or other?

Try Chrome's developer tools,

  1. click 'Network' tab
  2. use the filters at the bottom to show only WebSocket connections),
  3. select the desired websocket connection,
  4. note that there are 'Headers', 'Preview', 'Response', etc. sub-tabs
    to the right,
  5. once data starts flowing a 'WebSocket Frames' subtab will appear.
    All data going in either direction is logged. Very informative.


Related Topics



Leave a reply



Submit