check_ethernet: improve find_ssh_client for IPv6
This function had a number of problems:
(a) doesn't satisfy its description: it could print more than 1 IP,
which its callers wouldn't really like
(b) it has this minor shellcheck warning:
"SC1004: This backslash+linefeed is literal. Break outside single
quotes if you just want to break the line."
(c) it only handles IPv4 properly (IPv6 has colons in the address, so we
were only dumping the first hextet)
Fixes applied here:
* netstat -l (listening) -- contradicts our search for "established"
* netstat -p (program name) -- we ignore this
* netstat -a (listening and non-listening) -- again, we're only looking
* netstat -t -- add this, to limit the output netstat has to generate
* netstat -W -- need wide printing, to fit IPv6 addresses
* convert awk pattern to match columns more precisely, instead of a
regex across the entire line
* only look for the last colon when stripping the port (i.e.,
dead:beef:...:22 -> dead:beef:...)
* exit after the first match
Also, while netstat sort of gives us a time-ordered listing of
connections, it doesn't sort across different protocols (e.g., v4 vs.
v6). So just drop the "first" comment -- it's not really important to
the logic, as long as this finds some valid address.
Tested-by: Brian Norris <firstname.lastname@example.org>
Commit-Ready: Brian Norris <email@example.com>
Legacy-Commit-Queue: Commit Bot <firstname.lastname@example.org>
Reviewed-by: Grant Grundler <email@example.com>
Reviewed-by: Alex Khouderchah <firstname.lastname@example.org>
1 file changed