Send Data Across Networked Computers with Netcat Using the Command Line

Mar 27, 2014 - 14 Comments

Send data over a network with netcat

Netcat is a powerful command line tool that can read and write data across a network connection using TCP/IP, it’s commonly used for relays, file transfer, port scanning, amongst other things. Though netcat origins are from the unix and linux worlds, netcat is also built into Mac OS X, and we’re going to use the nc utility as an easy way to send data and other text across two networked computers.

By using netcat to send data with a fairly simple client and server relationship, it offers a potentially quicker alternative to traditional file sharing when users would prefer to stay in the command line while sending and receiving data, and where connecting through SSH or SFTP isn’t practical.

Note that netcat requires no logins or authentication, the only requirement is that the client is aware of the servers IP address and the listening port number. That obviously raises some potential for security misuse, thus using netcat to send data and text is generally best reserved for advanced users, or for use exclusively behind a protected local network where there is little risk. Situations where security is important to emphasize would be better off using SSH.

For ease, we’ll refer to computer 1 that is listening with netcat as “Server”, and we’ll refer to computer 2 sending the data to computer 1 as “client”.

Set Netcat to Listen on Server (Computer 1) & Port

We’ll launch netcat and have it listen on port 2999, and then redirect any of the data received to a file named “received.txt” with the following command string:

nc -l 2999 > received.txt

Before calling it done an moving over to the client, you’ll want to have the Macs IP address on the local network. You can get that from the Network control panel, or since you’re already at the Terminal, via the command line with the following syntax:

ipconfig getifaddr en0

Modern Macs with Wi-Fi only will use en0, Macs with ethernet & wifi may use en1. If one turns up nothing, try the other interface to get the LAN IP. Let’s assume this Macs IP is reported as “”, of course yours is likely to vary. You’ll need this on the client computer to send data over, which we’ll cover next.

Pipe Data from Client (Computer 2) to the Listening Server

Now on the client where you wish to send data from, you can use a command like the following. We’ll use cat to dump a text file over the network to the listening netcat server, but you can pipe over just about anything you want:

cat sendthisdataover.txt | nc 2999

For this to work properly, be sure to fill in your own IP address from the server, and to cat the appropriate file or text you want to send over.

Assuming the local network is even marginally quick, the data should arrive quite fast if not instantly. Once the data transfer is complete, both sides of the connection will cease and the server will stop listening, then close the port. This makes it effective for one-off sending of data like log files or a big text document, but it’s not necessarily practical for moving over a lot of files.

As already mentioned, you can pipe over just about any data, so whether it’s the output of another app, tail, cat, or even dumped directly from the clipboard with pbcopy and pbpaste, it will transfer through netcat.

It should be noted there are more convenient ways of sharing a clipboard between Macs or between a Mac and Linux or Windows machines, and in those situations you’re better off using free tools like Teleport for Mac-to-Mac input sharing, or the free Synergy utility if you’re going cross platform between Macs and PC’s. Both allow a user to share clipboard data as well as input devices like a mouse and keyboard.

For the record, while this demonstration is shown on two Macs with OS X, there’s no reason you can’t use netcat to send data between a Mac and Linux machine, or vice versa.

There are tons of other great uses for netcat out there, if you have any favorites, let us know in the comments!


Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks


» Comments RSS Feed

  1. mrunali says:

    Netcat is awesome! Here are two tricks I like to use with it:

    For file transfers like, an archived tgz or zip file or something, on the server:

    nc -l 1337 > expected-file.tgz

    Then sending file from the client:

    nc 1337 < sendthisfile.tgz

    Check md5sum when finished to make sure everthing is fine.

    -k flag also keeps nc open for more than one connection BTW.

    Client side, send as usual.

    Kapow! Anyone got other nc hackery to share? I love this stuff.

  2. Css says:

    Rub a touch of lemon on the space bar works every time

  3. anastasia lee says:

    I find too many security holes on most mac apps. Calendar, chat, itunes, Imovie, iphoto, on and on. Modems, bluetooth file xfer, comsat, and on and on. I can’t even use my mac book anymore. If a hacker wants in, and you’ve locked one door, pick any app, it’ll get in with another. I can’t say how frustrating it is to spend 2 yrs trying to get rid of this hack. I don’t even have anything on it anymore, but it keeps transferring my files.

    The hack persists, even after 30x erase and clean installs.

    This may not be the forum for this comment, but I’m so angry. Apple has zero security. Anyone can break in and decrypt your data, then, as you pointed out, can easily be transferred to anyone.

    I’d like to see one (1) article on how to secure built in apps. I’d like to see one (1) step by step article on how to remove persistent malware, and lock the computer down.

    Of course, I get no help from Apple.

  4. William Scott says:

    I compiled the gnu version of netcat (fink, macports, or easy enough to do on its own) which permits me to use bash as a command interpreter, eg:

    /sw/bin/nc -l -p 4321 -e /bin/bash

    Then you can send any unix command that suits you. Obviously this can also be a major security hole (which is probably why the Apple version doesn’t have the option).

    I wrote a simple launchd xml file to keep the process alive and listening. This is useful on my mac mini which I use as a home theatre computer.

  5. Andrew says:

    receiving end:
    nc -l [-p] port | tar -xpvf –

    sending end:
    tar -cpvf – multiple/ paths/ or.txt files.jpg | nc port [-q 1]

    (On some OSes, the connection may remain open, so -q 1 will tell netcat to quit 1 second after EOF is detected)

  6. Kurt Pfeifle says:

    I’ve used netcat for this purpose too, in the past…

    However, what I didn’t like about this method:

    * The file transfer is not “secure” in various meanings: one of them is that any random port scanning activity which knocks on my listening port will corrupt my received data.
    * I need to execute *two* different commands: one on the netcat “client”, one on the “server”.
    * So if the server is remote, how to access it? SSH of course…
    * But if the remote server is accessible via SSH — couldn’t I just execute some SSH command that transfers the data?

    Usually, if you can access a host via SSH, you also can use SFTP (Secure FTP) to transfer files. However, there are some cases where SSH will be enabled, but SFTP is disabled.

    So here is a command that pipes a data stream into SSH, logs into the remote server via SSH and uses a remotely executed command to save the data stream as a file on the remote host:

    cat local-data-name | ssh myname@remote “cat – > remote-data-name”

    You’ll get asked the remote login password, and after typing it in, the data transfer will start. After data transfer completion, you’ll be back in your local shell.

    This trick exploits the fact that SSH gives you a remote shell, and that you can use SSH to execute a “single” command remotely once, automatically logging out again after that command completed. Of course, this way you can do much more fancy stuff as well:

    gtar cvjf – some-subdir \
    | ssh myname@remote “cd Downloads; mkdir subdir; cat – > subdir/”

    This one packs my local subdirectory “some-subdir” into a BZIP-compressed GNU tar archive streaming it to local stdout (“-“), redirecting that stream by piping it into the SSH command, which in turn logs into remotehost under the named account where it executes 3 different sub-commands within the same “single” command:

    cd Downloads;
    mkdir subdir;
    cat – > subdir/remote-data-name.tar.bz2

    If you want, you could even add and execute a fourth command: unpacking the GNU tar archive remotely.

    • Paul says:

      Great tricks and good advice, thanks Kurt!

    • Bill says:

      I think SCP would work in situations where SSH is available, but not SFTP (assuming scp is installed of course) for simply copying a file, whether text or binary:

      $> scp local_file user@host:/path/to/remote_file

      Hope that helps!

  7. Theo Vosse says:

    I’ve got almost 30 years of Unix experience, yet this one is new for me. Nice.

  8. baby says:

    send a tailed log to elsewhere:

    tail -f /var/log/system.log | nc IP port

  9. Mathieu says:

    Netcat is awesome! Here are two tricks I like to use with it:

    For file transfers like, an archived tgz or zip file or something, on the server:

    nc -l 1337 > expected-file.tgz

    Then sending file from the client:

    nc 1337 < sendthisfile.tgz

    Check md5sum when finished to make sure everthing is fine.

    -k flag also keeps nc open for more than one connection BTW.

    Client side, send as usual.

    Kapow! Anyone got other nc hackery to share? I love this stuff.

Leave a Reply


Shop on and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks


iPhone / iPad



Shop on Amazon to help support this site