Four Unorthodox Ways to Transfer Files
LINUX
Four Unorthodox Ways to Transfer Files
2013-01-29
By
picoFlamingo

Transferring files between computers has become a trivial task. Web, FTPs or SSH servers are common in nowadays computers. However, sometimes those standard services are not right there and you need to fallback to more basic transfer solutions which, anyway, are a lot cooler than an FTP or SSH server.
Let's introduce 4 unorthodox (and cool) ways to transfer files in case you are in a emergency. I had used all of them in some special situations, so I will give some examples on situations where they had been useful for me.

SimpleHTTPServer

Very often I have to share files with other colleagues in a corporate network. Despite of firewall, proxies and other beasts, in this wild environment you do not usually have administration permissions in your machine.

Installing a web server nowadays is quite simple. However, doing it as non-privileged user is less easy and a minimal configuration is usually required in order to let the web server know, at least, were your files are located.

SimpleHTTPServer is a Python module intended to develop webservers in a very easy way. The cool thing about this module is that, when executed directly, it becomes a working Web Server. Just create a directory, copy the files you want to share there, change to that directory and execute:

$ python -m SimpleHTTPServer

Voila!. You have got a web server running as a non-privileged user sharing the files you want. Now your colleagues just need to point their browsers to your machine (port 8000 is default). Something like: http://your_ip:8000

Netcat

Netcat is the best tool ever when you have to work with networks. You can do almost anything with this application and, of course, you can also transfer files. It is really easy. In the machine receiving the file execute:

$ nc -l 5000 > remote_file

And in the machine with the file you want to transfer just type:

$ cat remote_file | nc remote_server 5000

That's quite cool, uhm? I had used netcat a couple of times for this purpose when a SSH server was not available, or it couldn't be installed. Netcat is a bit like vim, is all around so it is highly probable you will have it in your system. Even on a bit outdated systems.

NetKitty

NetKitty is a shrink down version of Netcat that I wrote for fun several years ago. During these last years, NetKitty has become one of the tools I use most often.

It is 600 lines of plain C code that only depends in a C library function. That means that it can be compiled statically into a very small executable. It can even run on Android devices... actually in anything with a Linux kernel as the tool uses exclusively system calls (and one standard C function) to provide its functionality... that is pretty cool uhm? Anyway, NetKitty works the same that Netcat for transferring files. Just the syntax is a bit different: In the machine receiving the file execute:

$ nk -os -s T,5000 > remote_file

In the machine sending the file execute:

$ cat remote_file | nc -c T,remote_ip,5000

Two comments. First, the flag -os stands for One Shot. NetKitty, when executed as a server can accept several connections simultaneously, that means that it will not return when the last client disconnects (as Netcat does, at least in the basic versions). The One Shot flag tells Netkitty to actually do that, finish the application when the last client disconnects.

The second comment is that NetKitty does not resolve names so you always need to use an IP address. The reason for that was to maintain the library dependencies at minimum (name resolution added some extra Kb on a static executable). So, you are probably wondering... why are you mentioning this if it is the same that netcat?. Well, it is not exactly the same. It's time for a example where I couldn't use netcat but netkitty saved my day.

Several years ago I had to transfer some files to my beloved OpenMoko. For some reason, I cannot remember, I couldn't use the cable connection (maybe I didn't have a cable at that time or the USB over Ethernet was broken because of some testing). So I just enabled Bluetooth and used NetKitty to transfer the files with the following commands: In the OpenMoko I ran:

$ nk -os -s B,1 > the_file

In my laptop I ran:

$ cat the_file | nk -c B,bluetooth_addr,1

The B flag allows to use a RFComm Bluetooth connection which was very handy at that time.

SHELL

OK, in all the methods described above you need some software installed, so, somehow you already have a way to transfer files in first instance. But what happens if you do not have any other way to get files into your target box?. Don't panic, /dev/tcp is your friend. In the machine with the file you want to transfer (the one in which you can install things), just launch netcat (or NetKitty) as a server.

cat file | nc -l 5000

In the machine receiving the file type

exec 5<> /dev/tcp/localhost/5000; cat 0<&5 > file.txt

Sure, I know what you are thinking.... this is really Cool uhm? To be honest I had never used this method to transfer files but I had used it for quickly prototyping network controlled applications (reading a GPIO and sending a network command)... I will write about that soon. Hope these file transfer tricks may be useful for you as they were for me.

Note: the /dev/tcp/ device is provided by bash and some other shells. It may be disabld, specailly in embedded systems where each Kb saved counts