Live data from Hacker News

A Unix Utility You Should Know About: Netcat (2009)

catonmat.net

31–39 of 39 posts

Re: A Unix Utility You Should Know About: Netcat (2009)

#31
post #5

You should also know about the so-called "netcat mode" in OpenSSH: -W host:port Requests that standard input and output on the client be forwarded to host on port over the secure channel. Implies -N, -T, ExitOnForwardFailure and ClearAllForwardings. Works with Protocol version 2 only.

I only discovered this recently - I was using a literal "nc %h %p" rather than this shortcut. Also worth noting this is also extremely useful when used in your ~/.ssh/config file.

Can you give an example of how you'd use it in your config?

Re: A Unix Utility You Should Know About: Netcat (2009)

#32

Stupid LAN trick: you can combine nc with pbcopy/pbpaste for distributed copy/paste on macs.

That sounds like a useful LAN trick, not a stupid one. I can't think of a situation recently where I've needed it but I'm sure it'll crop up some point.

Re: A Unix Utility You Should Know About: Netcat (2009)

#33
post #13

On a related note it's always annoyed me when people use "telnet" when they mean "make a TCP connection" as in "can you telnet to the port?"

One thing I like about the original and the gnu netcat is the z option: `nc -z host port; echo $?` for quick testing.

Re: A Unix Utility You Should Know About: Netcat (2009)

#34
post #16

Be aware that GNU netcat and BSD netcat are incompatible. Many a time have I attempted to cat a file from a Mac to a PC running Linux, only to have found that Nothing Happens (tm). I just recently realized that installing BSD netcat on Linux solves the problem. Not sure what the actual incompatibility is, just a heads up.

For my own purposes[1], I wrote these wrappers that abstract the differences between the two versions of nc that I had to deal with away:

https://github.com/pflanze/chj-bin/blob/master/netcat-get https://github.com/pflanze/chj-bin/blob/master/netcat-push https://github.com/pflanze/chj-bin/blob/master/netcat-receiv... https://github.com/pflanze/chj-bin/blob/master/netcat-serve-...

(They use 'have' from here: https://github.com/pflanze/chj-bin/blob/master/have )

[1] for example my way to transfer files between servers: https://github.com/pflanze/chj-bin/blob/master/netoffer (which depends on netfetch from the same place)

Re: A Unix Utility You Should Know About: Netcat (2009)

#36

Stupid LAN trick: you can combine nc with pbcopy/pbpaste for distributed copy/paste on macs.

I do something similar with local port forwarding in ssh - I can also use this to open files on remote servers in local gui editors, and to send things to notification centre - useful if you leave a long running task in the background.

Re: A Unix Utility You Should Know About: Netcat (2009)

#37

Earlier quoted context omitted.

I only discovered this recently - I was using a literal "nc %h %p" rather than this shortcut. Also worth noting this is also extremely useful when used in your ~/.ssh/config file.

Can you give an example of how you'd use it in your config?

Sure. I have something like:

  Host 
    Hostname 
    User gateway
    ForwardAgent yes

  Host 10.0.*
    User 
    ProxyCommand ssh -W %h:%p 
Where bastion is the NAT/Bastion SSH host for my infrastructure. 10.0.* are internal IP addresses, which don't work on my network, but get passed through to the bastion (where they do work). Pretty handy as I can just "ssh "

Re: A Unix Utility You Should Know About: Netcat (2009)

#38
post #19
post #16

Be aware that GNU netcat and BSD netcat are incompatible. Many a time have I attempted to cat a file from a Mac to a PC running Linux, only to have found that Nothing Happens (tm). I just recently realized that installing BSD netcat on Linux solves the problem. Not sure what the actual incompatibility is, just a heads up.

I just confirmed this is not the case. Could you share what you were doing?

Seems like bartbes pointed out the problem. I suppose not specifying the port correctly makes it use some default port or something like that.

Re: A Unix Utility You Should Know About: Netcat (2009)

#39

Earlier quoted context omitted.

Can you give an example of how you'd use it in your config?

Sure. I have something like: Host Hostname User gateway ForwardAgent yes Host 10.0.* User ProxyCommand ssh -W %h:%p Where bastion is the NAT/Bastion SSH host for my infrastructure. 10.0.* are internal IP addresses, which don't work on my network, but get passed through to the bastion (where they do work). Pretty handy as I can just "ssh "

Thanks!
Post reply on HN