Skip to content

Instantly share code, notes, and snippets.

@TomCan
Last active October 9, 2020 10:57
Show Gist options
  • Save TomCan/9fda11a5f6ae7b7fb0c9979bf5c4d7c9 to your computer and use it in GitHub Desktop.
Save TomCan/9fda11a5f6ae7b7fb0c9979bf5c4d7c9 to your computer and use it in GitHub Desktop.
Check websocket connection (eg. from monitoring software)
#!/bin/bash
#
# Check a websocket connection with curl. Pass URL to check as parameter.
# In normal conditions, this should return a 101 (Switching protocol).
#
CODE=$(timeout 3 curl -s --http1.1 \
--include \
--no-buffer \
--header "Connection: Upgrade" \
--header "Upgrade: websocket" \
--header "Sec-WebSocket-Key: VGhhbmsgeW91IFRvbUNhbiE=" \
--header "Sec-WebSocket-Version: 13" \
$1 | grep -Eo "HTTP/1.1 [0-9]{3}" | grep -Eo "[0-9]{3}")
if [ "$CODE" != "" ]
then
# we got a HTTP status code
echo $CODE
else
# return return value of the command
echo $?
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment