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 “192.168.1.101”, 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 192.168.1.101 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!
Follow Me:
Top Best Sellers!!