I cannot help but do a disservice to netcat here, no matter what I write today.
That’s because netcat is so simple, so flexible and so powerful that I could hardly encompass it all here. And like some other tools I’ve run across, there’s more out there across the vast wasteland of the Internet than I could list here.
All the same, there are a few nifty tricks I’ll repeat, just because they strike me as very cool. And it helps me remember them. 😉
For example, a simple file transfer between machines, no networking for file system tools needed: On one machine:
cat test.txt | nc -l -p 1228
And on the other:
nc (host) 1228 > test.txt
It’s almost as easy as ncp.
Here’s another, I remember using a long time ago: an impromptu remote shell, if ssh isn’t handy. On the controlling end:
nc -l -p 1228 -e /bin/bash
nc (host) 1228
Security-wise I don’t think that compares to ssh, but it might let you get the job done.
Those are the only two that I know of, and I learned them years ago, probably from this ancient how-to.
There are lots more out there though. netcat does some amazing things, and even when there are tools dedicated to a certain task, netcat can sometimes double for them. Check it out.
Edit, 2014-02-03, 06:30 a.m.: As luck would have it, I found this only a short while after posting. See, I told you there was a lot more you could do with it! 😉
netcat is great! You can also use nc to get “poor-man” audio and video streaming:
You should also study socat: it is “netcat++”
One thing worth mentioning is that there are several different programs called netcat that all have similar, but differing functionality — and similar, but frustratingly different (for scripting) usage flags (for instance, not all variants support the -e flag from your example, and they differ in how to specify listen and the port number to listen on).
There’s the traditional netcat by *Hobbit*, then there’s GNU netcat, OpenBSD netcat and a stripped down netcat in busybox who’s functionality will vary depending on which config options were selected when busybox was built.
OpenBSD netcat has the most features and is the one I’d recommend using if you need to do anything slightly unusual (and if you’re invoking nc, you’re already doing something slightly unusual) – it supports IPv6, proxies (HTTP CONNECT, SOCKS4 & SOCKS5 – useful in conjunction with SSH’s ProxyCommand option), Unix sockets and can continue listening for more connections after the first connection has been closed (whereas other netcats will simply exit, breaking any pipelines they may be a part of).
Pingback: netstat: Power concedes nothing | Inconsolation
Pingback: nmap: The grand meister network tool | Inconsolation
Pingback: socat: You want it? socat’s got it | Inconsolation
Pingback: tcpdump: Raw data at will | Inconsolation
Pingback: ngincat: Four lines of bash, plus netcat | Inconsolation
Pingback: Bonus: A dozen more remainders | Inconsolation