Skip to content

Instantly share code, notes, and snippets.

@Shardj
Last active May 30, 2022 10:44
Show Gist options
  • Save Shardj/f9c17056704e61279d51fb4f937bd5fc to your computer and use it in GitHub Desktop.
Save Shardj/f9c17056704e61279d51fb4f937bd5fc to your computer and use it in GitHub Desktop.
Host a website from a bash script using toilet, fortune, cowsay, lolcat, aha and nc
#!/bin/bash
ColorOff='\033[0m' # Text Reset
Yellow='\033[0;33m' # Yellow for important info
Red='\033[0;31m' # Red for errors
function infoMessage() {
echo -e ${Yellow}
echo $1
echo -e ${ColorOff}
}
function errMessage() {
echo -e ${Red}
echo $1
echo -e ${ColorOff}
}
errHandler() {
# Any steps that must be taken prior to exiting due to error
errMessage "Something went wrong. Exiting now."
exit 1
}
set -eE # -e throw ERR for any non-zero exit codes, -E as well as in any child functions
trap 'errHandler' ERR INT # Call errHandler function on ERR (non-zero exit code) or INT (ctrl-c interupt execution)
PORT="8080"
while nc -vz 127.0.0.1 $PORT &>/dev/null; do infoMessage "Port $PORT already bound"; PORT=$((PORT+1)); done
infoMessage "Port set as $PORT"
out () {
toilet="/usr/bin/toilet"
header=$($toilet "Welcome to My Website")
body=$(/usr/games/fortune | /usr/games/cowsay -n -f $(ls -1 /usr/share/cowsay/cows | shuf | head -n 1))
info=$(echo "\n Contact: your@email.address")
footer=$($toilet "THE END")
total="$header\n$body\n$info\n$footer"
echo -e "$total" | /usr/games/lolcat -f | aha --title LMR --black
}
infoMessage "Starting now!"
while true; do echo -e "HTTP/1.1 200 OK\n\n$(out)" \ | nc -l -k -p "$PORT" -q 1; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment