Hi,
With nc(1) you can do:
nc -zv example.com 80
or
nc -zv example.com http
which does the same. This works well unless the service name has a dash:
$ nc -zv example.com syslog-tls
nc: service "tls" unknown
This is because nc(1) is able to do some port scanning and the
delimiter used for the range is the dash. When it sees a dash, it
thinks it's a port range.
nc(1) is not the only software that takes an input that can be a port,
a range or a service name: pf is in this case too. In pf the delimiter
used is ":" so this works fine.
Here's a diff that change the delimiter to ":". This breaks existing
scripts but it would make the syntax like pf.conf instead of using
another symbol for a port range.
If you have a better idea how to solve this problem, please share!
Cheers,
Daniel
Index: nc.1
===================================================================
RCS file: /cvs/src/usr.bin/nc/nc.1,v
retrieving revision 1.91
diff -u -p -r1.91 nc.1
--- nc.1 25 Sep 2018 20:05:07 -0000 1.91
+++ nc.1 22 Dec 2018 21:21:40 -0000
@@ -393,7 +393,7 @@ option is given).
.Ar port
can be a specified as a numeric port number, or as a service name.
Ports may be specified in a range of the form
-.Ar nn Ns - Ns Ar mm .
+.Ar nn : Ns Ar mm .
In general,
a destination port must be specified,
unless the
@@ -491,12 +491,12 @@ to report open ports,
rather than initiate a connection.
For example:
.Bd -literal -offset indent
-$ nc -z host.example.com 20-30
+$ nc -z host.example.com 20:30
Connection to host.example.com 22 port [tcp/ssh] succeeded!
Connection to host.example.com 25 port [tcp/smtp] succeeded!
.Ed
.Pp
-The port range was specified to limit the search to ports 20 \- 30.
+The port range was specified to limit the search to ports 20 : 30.
.Pp
Alternatively, it might be useful to know which server software
is running, and which versions.
@@ -509,7 +509,7 @@ flag, or perhaps by issuing a
.Qq Dv QUIT
command to the server:
.Bd -literal -offset indent
-$ echo "QUIT" | nc host.example.com 20-30
+$ echo "QUIT" | nc host.example.com 20:30
SSH-1.99-OpenSSH_3.6.1p2
Protocol mismatch.
220 host.example.com IMS SMTP Receiver Version 0.84 Ready
Index: netcat.c
===================================================================
RCS file: /cvs/src/usr.bin/nc/netcat.c,v
retrieving revision 1.199
diff -u -p -r1.199 netcat.c
--- netcat.c 29 Nov 2018 14:25:06 -0000 1.199
+++ netcat.c 22 Dec 2018 21:21:40 -0000
@@ -1427,7 +1427,7 @@ build_ports(char *p)
int hi, lo, cp;
int x = 0;
- if ((n = strchr(p, '-')) != NULL) {
+ if ((n = strchr(p, ':')) != NULL) {
*n = '\0';
n++;