Last active
August 29, 2015 14:25
-
-
Save tropicloud-2k/f9468174b121cd8276e4 to your computer and use it in GitHub Desktop.
Netcat HTTP server (/bin/bash http.sh 9000)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# https://gist.github.com/marcellodesales/9e4288f35ac2cc3e1b83 | |
PORT="$1" | |
OUTPUT="/tmp/stdout" | |
rm -f $OUTPUT | |
mkfifo $OUTPUT | |
trap "rm -f $OUTPUT" EXIT | |
echo "Starting HTTP server" | |
echo "Listening on port $PORT" | |
while true | |
do cat $OUTPUT | nc -l $PORT > >( | |
export REQUEST= | |
while read LINE | |
do LINE=$(echo "$LINE" | tr -d '[\r\n]') | |
if echo "$LINE" | grep -qE '^GET /' # HTTP GET request | |
then REQUEST=$(echo "$LINE" | cut -d ' ' -f2) # extract the request | |
elif [ "x$LINE" = x ] # empty line / end of request | |
then HTTP_LOCATION="Location:" | |
HTTP_200="HTTP/1.1 200 OK" | |
HTTP_404="HTTP/1.1 404 Not Found" | |
# location ^/echo/$ | |
if echo $REQUEST | grep -qE '^/echo/' | |
then printf "%s\n%s %s\n\n%s\n" "$HTTP_200" "$HTTP_LOCATION" $REQUEST ${REQUEST#"/echo/"} > $OUTPUT | |
# location ^/date | |
elif echo $REQUEST | grep -qE '^/date' | |
then date > $OUTPUT | |
# location ^/stats | |
elif echo $REQUEST | grep -qE '^/stats' | |
then vmstat -S M > $OUTPUT | |
# location ^/net | |
elif echo $REQUEST | grep -qE '^/net' | |
then ifconfig > $OUTPUT | |
# location * | |
else printf "%s\n%s %s\n\n%s\n" "$HTTP_404" "$HTTP_LOCATION" $REQUEST "Resource $REQUEST NOT FOUND!" > $OUTPUT | |
fi | |
fi | |
done) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment