Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A web socket server as a bash script.
# WebSocket shell, start & browse to http://<Host>:6655/
# Requires bash 4.x, openssl.
# Author: (which isn't me, apk)
coproc d { nc -l -p 6656 -q 0; }
nc -l -p 6655 -q 1 > /dev/null <<-ENDOFPAGE
HTTP/1.1 200 OK
<html><head><script language="javascript">
var url = location.hostname + ':' + (parseInt(location.port) + 1);
var ws = new WebSocket('ws://' + url + '/test');
ws.onmessage = function(msg) {
document.f.out.value += + '\n';
document.f.out.scrollTop = document.f.out.scrollHeight;
ws.onclose = function() { alert('Connection closed.'); }
function send() {
ws.send('' +;'';
</script></head><body><form name="f"><textarea name="out" cols="100"rows="40"></textarea>
<input value="" type="text" size="120" id="in" onkeypress="if(event.keyCode==13){send();return false;}" />
until read line; line=`tr -d '\r\n'<<<$line`; test -z "$line"; do
test "${line:0:18}" = "Sec-WebSocket-Key:" && key=${line:19}
test "${line:0:22}" = "Sec-WebSocket-Version:" && ver=$line
done <&${d[0]}
rkey=`echo -n ${key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11|openssl dgst -sha1 -binary|base64`
echo -ne "HTTP/1.1 101 Switching Protocols\r\n" >&${d[1]}
echo -ne "Upgrade: websocket\r\nConnection: Upgrade\r\n" >&${d[1]}
echo -ne "Sec-WebSocket-Accept: $rkey\r\n$ver\r\n\r\n" >&${d[1]}
doasync() {
bash -c "$1" | while read line; do
while [ "${#line}" -gt 0 ]; do
len=`echo -n "$l2" | wc -c | tr -d ' '`
echo -ne "\x81\x`printf '%02x' $len`$l2"
done &
while true; do
reclen=$((`od -j 1 -N 1 -t dI -A n <&${d[0]}` - 128))
for i in `seq 0 3`; do
mk[$i]=`od -N 1 -t dI -A n <&${d[0]}`
for i in `seq 0 $(($reclen - 1))`; do
bt=`od -N 1 -t dI -A n <&${d[0]}`
bt=$(($bt ^ ${mk[$(($i % 4))]}))
msg="$msg$(echo -e "\x`printf '%02x' $bt`")"
test "$msg" = "exit" && break
doasync "$msg" >&${d[1]}
echo >&${d[1]}-
Copy link

haisamido commented Aug 17, 2013

The coproc d { nc -l -p 6656 -q 0; } line yields this error with respect to nc

nc: invalid option -- 'q'

I'm using nc version 1.84-22.7. and bash, version 4.1.2(1)-release.

Which version of nc are you using?

Copy link

scheuref commented Aug 27, 2013

simply the most amazing bash script that a saw in 12 years !!!

tried it and it worked perfectly! congrats!!

Copy link

scheuref commented Aug 27, 2013

I just linked your page from our blog article

who is the author "rootshell" ?

francois scheurer

Copy link

manjana commented Jul 3, 2014


Copy link

maz-1 commented Sep 25, 2016

Have no luck with archlinux
Error message: line 49: ${d[0]}: ambiguous redirect line 58: ${d[1]}: ambiguous redirect line 47: ${d[0]}: ambiguous redirect

Copy link

apk commented Feb 7, 2017

I should mention that a) I don't get notification for comments here, and b) that wasn't my code. A colleague passed to to me; I only have the email address of the (apparent) original author.

And linux and macos are very different in their understanding of nc.

Copy link

infracritical commented Aug 6, 2017

Ditto re: error. I am using CentOS v6.

Copy link

positively commented Nov 9, 2018

simply the most amazing bash script that a saw in 12 years !!!

+1 !

Copy link

andriygnyp commented Mar 27, 2020

Say "thank you for the masterpiece" to the original author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment