srsudar / eg

Useful examples at the command line.
MIT License
1.82k stars 99 forks source link

Add netcat #21

Closed mcarton closed 9 years ago

srsudar commented 9 years ago

Out of curiosity, what made you choose 192.168.1.42 as your example IP address?

Also, my man netcat says it is an error to use the -l and -p flags together. I've removed them from your examples for that reason, and they work for me on OSX Yosemite. Is this different on your version? Do the examples I'm merging in work for you?

mcarton commented 9 years ago

@srsudar: Local IPs often are in the 192.168.1.0/8 range. As for 42, that's obvious.

My netcat starts with:

SYNOPSIS
       netcat [options] hostname port [port] ...

       netcat -l -p port [options] [hostname] [port] ...

       netcat -L hostname:port -p port [options] ...

but never says what -l stands for and all netcat calls in my histfile use -l -p. This must be a platform-specific problem. I just tried, without -p it does not work. Mine is GNU Netcat.

srsudar commented 9 years ago

Ah I see. Do the examples I added work for you?

On Sun, May 17, 2015, 4:12 AM Martin Carton notifications@github.com wrote:

@srsudar https://github.com/srsudar: Local IPs often are in the 192.168.1.0/8 http://192.168.1.0/8 range. As for 42, that's obvious https://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker's_Guide_to_the_Galaxy#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe.2C_and_Everything_.2842.29 .

My netcat starts with:

SYNOPSIS netcat [options] hostname port [port] ...

   netcat -l -p port [options] [hostname] [port] ...

   netcat -L hostname:port -p port [options] ...

but never says what -l stands for and all netcat calls in my histfile use -l -p. This must be a platform-specific problem.

— Reply to this email directly or view it on GitHub https://github.com/srsudar/eg/pull/21#issuecomment-102784570.

mcarton commented 9 years ago

No.

$ nc -vz 80
Warning: Inverse name lookup failed for `0.0.0.80'
Error: No ports specified for connection
zsh: exit 1     nc -vz 80
$ nc -vz -p 80
nc: missing hostname argument
Try `nc --help' for more information.
zsh: exit 1     nc -vz -p 80
$ nc -vz localhost 80
localhost [127.0.0.1] 80 (http): Connection refused
zsh: exit 1     nc -vz localhost 80
$ nc -vz localhost 1337
localhost [127.0.0.1] 1337 (menandmice-dns) open

Without -p it seems to interpret the port as an IP address. The last one was with another netcat listening to port 1337. This closed it, which might not be what was intended.

srsudar commented 9 years ago

Ha I really messed that one up. nc -vz 80 isn't the command I was actually testing, and doesn't work for me either. nc -vz 127.0.0.1 80 does. Thanks for checking.

I'll think about some way of showing that -p is required on some machines. This is the first command I've encountered where the base cases vary depending on the machine. I'm sure there will be more. nc -l 1337 works for me on Yosemite and Fedora.

Yet another question for you: the Yosemite and Fedora machines I have access to both have nc as the command, while netcat is the full name. It seems that changing netcat.md to nc.md and updating the aliases.json file to have netcat -> nc might be more in keeping with the man pages.

mcarton commented 9 years ago

I'll think about some way of showing that -p is required on some machines. This is the first command I've encountered where the base cases vary depending on the machine. I'm sure there will be more. nc -l 1337 works for me on Yosemite and Fedora.

That does not work on Archlinux, Debian 7 and Debian 8. Interestingly on Ubuntu I get:

% nc -l -p 1337 > /dev/null
This is nc from the netcat-openbsd package. An alternative nc is available
in the netcat-traditional package.
usage: nc [-46DdhklnrStUuvzC] [-i interval] [-P proxy_username] [-p
source_port]
      [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_protocol]
      [-x proxy_address[:port]] [hostname] [port[s]]
zsh: exit 1     nc -l -p 1337 > /dev/null

so I guess the difference is between BSD and GNU netcat.

Yet another question for you: the Yosemite and Fedora machines I have access to both have nc as the command, while netcat is the full name. It seems that changing netcat.md to nc.md and updating the aliases.json file to have netcat -> nc might be more in keeping with the man pages.

On Archlinux it's ncnetcat, there is no man nc and man netcat uses netcat (and only mentions nc once), that's why I did that. On Ubuntu and Debian it's netcatnc too, man nc exists and man netcat uses nc in examples.

srsudar commented 9 years ago

What do you think of my latest changes to try and resolve this? I added a Compatibility section at the end and added an example in the first section for specific implementations that require the -p.

I don't love this, but I'd like examples in the first section to be widely applicable, and the listen example seems like a key usage of netcat.

I also moved netcat.md to nc.md and updated the alias, since they seem interchangeable and nc is shorter for example purposes.

Where are you getting access to all the different linux distros? If it's an easy log on or VM situation it sounds very useful.

mcarton commented 9 years ago

On Tue, May 19, 2015 at 6:33 AM, Sam Sudar notifications@github.com wrote:

What do you think of my latest changes https://github.com/srsudar/eg/blob/master/eg/examples/nc.md to try and resolve this? I added a Compatibility section at the end and added an example in the first section for specific implementations that require the -p. Looks OK, but I just made small fixes. See #31.

Where are you getting access to all the different linux distros? If it's an easy log on or VM situation it sounds very useful.

I have my own computer (Arch Linux) and a server on Debian 8. And I am a student so I have access to lots of machines in my university (Ubuntus and Debian 7 and probably some BSDs flavors).