Mar 2, 2011

Clients and Servers on the Terminal: Simple File Transfers

I just stumbled across an interesting shell command: nc

It's used for simple client/server interactions from the shell. With it, you can do interesting things like take output from one machine, send it over the network to another machine that pipes this input into other commands. Here's an example:
# On the destination machine
nc -l -p 2345 | tar xv
# On the source machine
tar c PATH_TO_FILES > /dev/tcp/IP.OF.DESTINATION/2345
Which will tar PATH_TO_FILES, on the source machine and transmit it over the network on port 2345. The destination will receive the data and pipe it into a command that will extract the tar.

This is so useful, I had to jot it down!

1 comment:

  1. This command is incredibly useful. Just found another common use on my Mac:

    nc -lk 17723

    listens, indefinitely (allowing multiple connections), on port 17723

    If you type into the "server" window, it will send commands through to the "client"

    ReplyDelete