[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: what binds to port



On Fri, Jul 30, 2021 at 02:27:19PM +0300, IL Ka wrote:
> >
> > sudo netstat -ltp|grep sane-port
> >
> 
> or
> 
> $ sudo ss -l4pon | grep [port]

That grep command is wrong in a couple ways.

First, the unquoted [port] is a live glob pattern for the shell.  The
shell will look for files in the current working directory named "p"
or "o" or "r" or "t".  If it finds one or more of those, it will replace
the [port] glob with the list of the matching filenames, sorted by
your locale's collating order.

Second, even if [port] is quoted or doesn't match any files in the
current directory, grep is going to treat is as "match any line that
contains the letter p or the letter o or the letter r or the letter t".
That's going to generate way more matches than you probably want.

I believe what you were aiming for is this hack:

grep [p]ort

This is one of the cargo cult tricks that people use when they're
looking for a process in the output of ps.  Most people don't even
understand it; they just tried it once and it "worked" so they keep
doing it.

You apparently only half-remembered the trick, so you got it wrong.

Even worse, you applied the trick in a context where it isn't even
necessary.  You're not looking for processes in the output of ps, so
you didn't need to stop grep from finding itself by mangling the pattern.

Finally, you added the -n option to ss, which means it no longer prints
service names; it prints port numbers instead.  So the output is no
longer going to contain the word "sane-port", and therefore your grep
for "port" won't work.

(And now that I've written all of this, it occurs to me that maybe
you were using [port] as a syntactic symbol meaning "put the numeric
port number here".  Normally we'd use <port> or _port_ for that.  Or
else you'd actually type out the numeric equivalent of sane-port, so
the reader doesn't have to look it up themselves.  Square brackets
are reserved for "this is an optional argument", or else they're literal.)


Reply to: