Home > Basic Linux-related things, Linux > Simple ad-hoc file sharing

Simple ad-hoc file sharing

January 1st, 2010

There is a distinct lack of simple, ad-hoc file sharing mechanisms for one-off file transfers between platforms. Maintaining an ftp or http server securely and grant users access to files is cumbersome. An ssh guest account opens more than you’d like, and still requires you to somehow grant access to a certain file to a user and then close it. IRC requires that the file is on the box you run the client on (which is often not your local box), and MSN requires that you add people to your contact list, assuming you don’t use it through bitlbee anyways.

Here is a little script I have lying around, I call it wwwshare:

#!/bin/bash

die() { echo "$*" >&2; exit 1; }

[[ $# != 0 ]] || die "Usage: $0 filename"
[[ -f $1 ]]   || die "No such file: $1"

file="$1"
ip=$(curl -s 'http://checkip.dyndns.com/' | sed 's/.* \([0-9.]*\).*/\1/')
port=$((8000 + RANDOM%1000))

echo "http://$ip:$port/$file"

cat - "$file" << EOF | nc -l -p $port -q 0
HTTP/1.1 200 Ok
Content-Type: application/octet-stream
Content-Length: $(stat -c %s "$file")

EOF

Just run wwwshare filename, and it’ll print an URL and start a wannabe http server on a random port (8000-9000) for a single session. When the file is downloaded, it exits. No setup or cleanup required.

Basic Linux-related things, Linux

  1. January 1st, 2010 at 13:10 | #1

    Markup engine screwed up ” and ‘

  2. Vidar
    January 1st, 2010 at 13:44 | #2

    @ Voker57
    Eek! Should be fixed now, thanks.

  3. FCKGW
    February 2nd, 2010 at 22:52 | #3

    Great function. I adapted it in my .bash_profile so I can use it both on linux and my osx (bsd) but for LAN sharing. Can post the code if interested.

  4. pgas
    February 4th, 2010 at 08:12 | #4

    Great Idea!
    I made a gnu awk port (for the lol, you know):
    http://pgas.freeshell.org/shell/wwwshare

  5. Flupp
    January 2nd, 2011 at 22:01 | #5

    Great script!
    I just added a line using xclip to copy the url to the clipboard, and I replaced
    Content-Type: application/octet-stream
    by
    Content-Type: $(file –brief –mime “$file”)
    which seems to help the browser.

  6. Vidar
    January 3rd, 2011 at 07:34 | #6

    @Flupp

    Very convenient additions!

  1. April 12th, 2010 at 10:34 | #1
  2. April 21st, 2010 at 03:51 | #2

Ignore the following captcha and enter 'bunnies' in the field instead.