CONTENTS

Home
Updates
Software
Electronics
Music
Resume
Contact


YouTube
Twitter
GitHub
LinkedIn


Shell Script Web Server

Introduction

He's a dumb proof of concept. This is a web server written as a shell script using netcat. WARNING: This is NOT a secure web server and if you run this in the open, you will get hacked.

The Script

#!/bin/bash

file=""
host="localhost"

while [ true ]
do
  read a

  a=`echo ${a} | sed 's/\r//'`

  if [ "${a}" == "" ]
  then
    break
  fi

  command=`echo ${a} | grep GET`
  if [ "${command}" != "" ]
  then
    file=`echo ${a} | awk '{ print $2 }'`
  fi

  command=`echo ${a} | grep Host:`
  if [ "${command}" != "" ]
  then
    host=`echo ${a} | awk '{ print $2 }'`
  fi

done

if [ ${file} == "/" ]
then
  file="index.html"
else
  file=`echo ${file} | sed 's/^\///'`
fi

if [ ! -e ${file} ]
then
  echo "HTTP/1.1 404 NOT FOUND"
  echo ""
  echo "Document not found"
else
  len=`ls -la ${file} | awk '{ print $5 }'`
  echo "HTTP/1.1 200 OK"
  echo "Host: ${host}"
  echo "Content-Length: ${len}"
  echo ""
  cat ${file}
fi

To Run It

while [ true ]; do nc -e 'shellweb.sh' -l -p 80; done

Copyright 1997-2024 - Michael Kohn