Live data from Hacker News

Cool stuff you can do with netcat

en.wikipedia.org

31–40 of 44 posts

Re: Cool stuff you can do with netcat

#31
post #17

Poor man's skype: sender arecord -f cd -c 2 | lame -b128 - - | netcat -u your-ip 6881 | mpg123 - receiver arecord -f cd -c 2 | lame -b128 - - | netcat -u -l 6881 | mpg123 -

I used to do this with a friend in the days when modems tied up phone lines. You have to pass a much lower number to lame's -b option, though.

Re: Cool stuff you can do with netcat

#32
post #14

Anyone want to give a few examples of cool things they actually do with netcat?

This is pretty much the fastest way I've found to clone a disk over a network. To clone from one host's /dev/sda to another host's /dev/sda in Linux, try this. On the target host, run the following: nc -l 31337 | bzip2 -d | dd bs=16M of=/dev/sda On the host you're copying from , do this: dd bs=16M if=/dev/sda | bzip2 -c |nc dest.host.com 31337 Bear in mind that this isn't secure (everything goes over the wire in clea…

As a method of obscuring the data slightly (although really it doesn't add much), I sometimes like to add "conv=swab" to the dd. But for the CPU that's being spent on the bzip2, you could do it slightly more easily with a single command on one machine:

    dd bs=16M if=/dev/sda | ssh dest.host.com 'bzip2 -d >/dev/sda'
Of course, that presupposes a running sshd on dest.host.com. You could secure it from being observed (although not from being interfered with) under Linux using loopback devices:

    ## On the destination machine:
    LOD=`losetup -f`
    losetup -e aes $LOD /dev/sda
    # You'll be prompted for a password for the encryption.
    nc -l 31337 | bzip2 -d | dd bs=16M of=$LOD

    ## On the source machine:
    LOD=`losetup -f`
    losetup -e aes $LOD /dev/sda
    # You'll be prompted for a password; use the same one.
    dd bs=16M if=$LOD | bzip2 -c |nc dest.host.com 31337
Most people just use encrypted loopback devices for things like disk encryption, but losetup (like netcat) has so many other fun potential applications.

(Edited twice due to formatting sadness.)

Re: Cool stuff you can do with netcat

#33

Don't forget easy ssh host hopping. ssh -oProxyCommand="ssh host1 nc host2 22" host2

Interesting. Thanks for the tip! I'd never heard of ssh host hopping, but have been doing 2-3 sequential ssh logins for years. I always wondered if this was possible somehow! I found this nice tutorial that explains your tip in detail: http://sshmenu.sourceforge.net/articles/transparent-mulithop... This made all the time I've spent on HN in the past week worthwhile! :)

At $EMPLOYER, I have a script that takes a description of the network topology, converts it to a sequence of Host stanzas and writes them to ~/.ssh/config. The source file is simple enough that the sysad team can keep it up-to-date when they change the network configuration, and most people just have to run the 'update' script occasionally to have Super SSH Powers. It's quite popular.

Re: Cool stuff you can do with netcat

#34
post #14

Earlier quoted context omitted.

This is pretty much the fastest way I've found to clone a disk over a network. To clone from one host's /dev/sda to another host's /dev/sda in Linux, try this. On the target host, run the following: nc -l 31337 | bzip2 -d | dd bs=16M of=/dev/sda On the host you're copying from , do this: dd bs=16M if=/dev/sda | bzip2 -c |nc dest.host.com 31337 Bear in mind that this isn't secure (everything goes over the wire in clea…

As a method of obscuring the data slightly (although really it doesn't add much), I sometimes like to add "conv=swab" to the dd. But for the CPU that's being spent on the bzip2, you could do it slightly more easily with a single command on one machine: dd bs=16M if=/dev/sda | ssh dest.host.com 'bzip2 -d >/dev/sda' Of course, that presupposes a running sshd on dest.host.com. You could secure it from being observed (al…

That second one's really interesting. I've tried the SSH route and while it works (and works well) it really knocks up the time to send an image over the wire.

We've used this for forensic imaging in the past and have relied upon digital signatures as integrity checks. It's not great I know, but it's the best way to get disk images from a big server onto a NAS (when you can't plug the NAS into the server).

Re: Cool stuff you can do with netcat

#35
post #34

Earlier quoted context omitted.

As a method of obscuring the data slightly (although really it doesn't add much), I sometimes like to add "conv=swab" to the dd. But for the CPU that's being spent on the bzip2, you could do it slightly more easily with a single command on one machine: dd bs=16M if=/dev/sda | ssh dest.host.com 'bzip2 -d >/dev/sda' Of course, that presupposes a running sshd on dest.host.com. You could secure it from being observed (al…

That second one's really interesting. I've tried the SSH route and while it works (and works well) it really knocks up the time to send an image over the wire. We've used this for forensic imaging in the past and have relied upon digital signatures as integrity checks. It's not great I know, but it's the best way to get disk images from a big server onto a NAS (when you can't plug the NAS into the server).

The second one is way more fun, using the real device as the plaintext and the loopback as the cyphered version.

Have you tried using the -C (compression) option to ssh instead of the bzip2 pipe? I don't often have occasion to do this sort of thing, so if you've tried that already, it would be interesting to know relative timing. pbzip2 ( compression.ca/pbzip2/ ) might be helpful sometimes, but I don't suspect that it's carried on most live CDs (unless you guys are rolling your own) and the CPU probably isn't the bottleneck anyway.

9P2000 has recently made it into the Linux kernel and it gets way better throughput than NFS, but (and I'm not too familiar with NAS hardware or software, so I may be saying something foolish) I don't suspect it's a supported protocol on those things. Either way, I also don't suspect that adding the overhead of an FS protocol could be helpful in cases where netcat would do, but it's a nice tool to have in the box.

Re: Cool stuff you can do with netcat

#36

Anyone want to give a few examples of cool things they actually do with netcat?

1. Schlepping files between computers. computer A: nc -l $ARBITRARY_PORT | tar vzxf - computer B: tar vzcf - files_or_directories | nc computer_a $ARBITRARY_PORT 2. Testing socket servers: cat canned_request | nc server 80 There are probably other things, but I don't think, "Oh, this is a good use for netcat", I just do it. It's almost as handy as awk.

> Schlepping files between computers.

What about using rsync instead ?

Re: Cool stuff you can do with netcat

#37
post #34

Earlier quoted context omitted.

That second one's really interesting. I've tried the SSH route and while it works (and works well) it really knocks up the time to send an image over the wire. We've used this for forensic imaging in the past and have relied upon digital signatures as integrity checks. It's not great I know, but it's the best way to get disk images from a big server onto a NAS (when you can't plug the NAS into the server).

The second one is way more fun, using the real device as the plaintext and the loopback as the cyphered version. Have you tried using the -C (compression) option to ssh instead of the bzip2 pipe? I don't often have occasion to do this sort of thing, so if you've tried that already, it would be interesting to know relative timing. pbzip2 ( compression.ca/pbzip2/ ) might be helpful sometimes, but I don't suspect that i…

I haven't tried -C on SSH, but I'll make a note to try it next time I get the chance. We use QNAP NAS so we're more or less stuck to specific kernel versions, but the userland is fairly complete (as you have basically got a ton of disk space for userland apps!)

Re: Cool stuff you can do with netcat

#39

Don't forget easy ssh host hopping. ssh -oProxyCommand="ssh host1 nc host2 22" host2

Thanks for the tip, it inspired me to do the following:

user@host3# ssh-keygen -t rsa -f ~/.ssh/id_rsa_hopping

user@host3# echo command=\"nc host2 22\" `cat ~/.ssh/id_rsa_hopping.pub` | ssh user@host1 "cat >> ~/.ssh/authorized_keys"

user@host3# scp ~/.ssh/id_rsa_hopping* user@host4:~/.ssh/

The point of all of this being that host4 should be able to do anything on host2, but only be able to use host1 as a hop:

user@host4# ssh -oProxyCommand="ssh -i ~/.ssh/id_rsa_hopping host1" host2

Re: Cool stuff you can do with netcat

#40

Earlier quoted context omitted.

Interesting. Thanks for the tip! I'd never heard of ssh host hopping, but have been doing 2-3 sequential ssh logins for years. I always wondered if this was possible somehow! I found this nice tutorial that explains your tip in detail: http://sshmenu.sourceforge.net/articles/transparent-mulithop... This made all the time I've spent on HN in the past week worthwhile! :)

At $EMPLOYER, I have a script that takes a description of the network topology, converts it to a sequence of Host stanzas and writes them to ~/.ssh/config. The source file is simple enough that the sysad team can keep it up-to-date when they change the network configuration, and most people just have to run the 'update' script occasionally to have Super SSH Powers. It's quite popular.

Don't suppose you'd be willing to post this uber-script somewhere?
Post reply on HN