notes/netcat webserver notes-ACjnggcz.sh
#webservers using netcat, busybox nc, or ncat
#this will work with the full version of ncat, but busybox nc doesn't have '-c' builtin

ncat -l -p 8000 -c 'echo -e "HTTP/1.1 200 OK\r\n$(date)\r\n\r\n";echo "<p>How are you today?</p>"'

#this is more universal
{ echo -e 'HTTP/1.1 200 OK\r\n'; echo "<p>How are you today?</p>"; } | netcat -l -p 8000

#keep it up and running after serving files

while [ 1 ];do { echo -e 'HTTP/1.1 200 OK\r\n'; echo "<p>How are you today?</p>"; } | busybox -l -p 8000;done

#or you can pass it a file

{ echo -e 'HTTP/1.1 200 OK\r\n'; cat index.html; } | busybox nc -l -p 8000

#or the output of a command
{ echo -e 'HTTP/1.1 200 OK\r\n'; ls /; } |busybox nc -l -p 8000


#or a command with html tags

#note the use of '<!DOCTYPE html>' and use single quotes due to special characters 
{ echo -e 'HTTP/1.1 200 OK\r\n'; echo '<!DOCTYPE html>';echo "<ul>";for i in /*;do echo "<li>$i</li>";done;echo "</ul>"; } | busybox nc -l -p 8000
{ echo -e 'HTTP/1.1 200 OK\r\n'; echo '<!DOCTYPE html>';
#you can embed images into your document with base64

<img src='data:image/png;base64,[base64 code]'/>
{ echo -e 'HTTP/1.1 200 OK\r\n'; echo '<!DOCTYPE html>';cat img.html; } | busybox nc -l -p 8000

#display all png images in current folder
{ echo -e 'HTTP/1.1 200 OK\r\n'; echo '<!DOCTYPE html>';for i in *.png;do echo "<img src='data:image/png;base64,$(base64 $i)'/>";done; } | busybox nc -l -p 8000

{ echo -e 'HTTP/1.1 200 OK\r\n'; echo '<!DOCTYPE html>';for i in *.ogg;do echo "<audio controls src='data:audio/ogg;base64,$(base64 $i)'></audio>";done; } | busybox nc -l -p 8000

syntax highlighted by Code2HTML, v. 0.9.1