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,
- click 'Network' tab
- use the filters at the bottom to show only WebSocket connections),
- select the desired websocket connection,
- note that there are 'Headers', 'Preview', 'Response', etc. sub-tabs
to the right, - once data starts flowing a 'WebSocket Frames' subtab will appear.
All data going in either direction is logged. Very informative.
Related Topics
Using Bash Script to Feed Input to Command Line
Calculate and Print the Average Value of Strings in a Column
How to Increase the Scrollback Buffer in a Running Screen Session
How to Check If There Are Symbolic Links Pointing to a Directory
What Is a Way to Read Man Pages in Vim Without Using Temporary Files
Using 'Find -Perm' to Find When a Permission Is Not Set
Rm: Cannot Remove: Permission Denied
What Is The Equivalent of "Aptitude" in MAC Osx
Grep Command to Add End Line After Every Match
How to Rename Multiple Files Beginning with a Unix Timestamp - Imapsync Issue
Why Does Find -Exec Mv {} ./Target/ + Not Work
Where Are All My Inodes Being Used
What Would Be the Equivalent of Win32 API in Linux
How to Find Files Modified in Last X Minutes (Find -Mmin Does Not Work as Expected)
Searching for a String in Multiple Files on Linux
Apache Webserver - How to Write to Dir/Files with Permissions Set at 755 Instead of 777