Skip to content

Instantly share code, notes, and snippets.

@markstos
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markstos/93022b61bd49a0fbb045 to your computer and use it in GitHub Desktop.
Save markstos/93022b61bd49a0fbb045 to your computer and use it in GitHub Desktop.
oneshot - a single-use, single-file web server to serve a file and then shutdown. Share a file directly with a network neighbor-- no third-party needed.
#!/bin/bash
USAGE="Usage: $0 file.txt. Start a webserver on port $PORT, serve this file and close the connection.";
# exit if there's an error
set -e
HOSTNAME=`hostname -I | cut -d' ' -f1`;
PORT=8000
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
FILE=$1;
echo "Serving $FILE one time at http://$HOSTNAME:$PORT";
{ echo -ne "HTTP/1.0 200 OK\r\nContent-Disposition: attachment;filename="`basename $FILE`"\r\nContent-Length: $(wc -c <$FILE)\r\n\r\n"; cat $FILE; } | nc -l $PORT
# Potential features:
# * Random open port selection
# * SSL support (with "sslnetcat", "scnc", or ncat )
@markstos
Copy link
Author

For personal use it only. Note it calls the shell with unsanitized input. Only use with your filenames that you provide and trust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment