Netcat - how to transfer files without scp or ftp

One of my recent customers provided me hardware with custom Linux system. Distribution used on this hardware was very limited there was no developers tools, file transfer applications (like scp, ftp or even tftp) or communication clients like ssh. I had to deploy some firmware files to the system without modifying it. This was i386 machine. Of course I could compile something and add this software using usb stick or other stoarge, but what if I would not have direct access to hardware ? Also for development and testing purposes it would be much easier to use network transfer, then running with usb stick.

When looking for answer I found this. I heard before about netcat, but more in context of debugging then using it as file transfer application. Luckily nc as very small tool is in almost all distributions and it was also available in my small custom distro.

File transfer with netcat

nc by man page is described as TCP/IP swiss army knife , but can be used to transfer files.

What have to be done is setting receiving side ie.:

nc -l -p 2020 > my_file.bin

What tell nc to listen on inbound connection (-l) on port 2020 (-p 2020) and redirect content of incoming packages to my_file.bin.

On sender side we pipe my_file.bin to nc like that:

cat my_file.bin | nc <dest_ip_addr> 2020

Which cause nc to create TCP connection to <dest_ip_addr> on port 2020 and send everything it gets on standard input.

Known flaws

From what I saw sometimes nc doesn’t end at EOF and just hang waiting for next data, which never come. In that case I just break with Ctrl-C on both ends. Then check if all stuff was transfered correctly by verifying MD5 sum on sender and receiver side. In most cases files pass this integrity test.

Piotr Król
Founder and Embedded Systems Consultant at 3mdeb as well as freelance CTO of Vitro Technology and CEO of LPN Plant. Passionate about building firmware that enables advanced hardware features in modern products. Dedicated to customers that treat embedded software security and upgradeability as forethought. Open source firmware evangelist interested in platform security and trusted computing. In favor of fixed price projects with a clear definition of success.