Class NetStat
java.lang.Object
org.terracotta.utilities.test.net.NetStat
Produces a network status collection similar to that obtained using
netstat.
This class is intended for diagnostic purposes; elevated privileges may be required
for full status collection.
Implementation Notes
-
For a Windows platform,
powershellmust be available viaPATH. -
For a Mac OS X platform,
nettopandpsmust be available viaPATH. -
For a Linux platform,
lsofandpsmust be available viaPATH. For a complete port list, the effective user must be permitted to usesudoto execute thelsofcommand without a password. -
Not all connections observed using
netstatare shown bylsof-- seelsofFAQ item 3.29.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDescribes an active TCP port and associates it with the controlling process.static final classThrown to indicate the failure of a host command used in construction of TCP connection status information.private static classprivate static enumSpecifies the platform-dependent support for obtainingnetstat-like information. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final org.slf4j.Loggerprivate static final String[]private static final AtomicBooleanprivate static final String[]private static final String[]private static final String[]private static final PatternPattern for parsing IPv4 endpoints expressed bynettop.private static final PatternPattern for parsing IPv6 endpoints expressed bynettop. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateNetStat()Private niladic constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic List<NetStat.BusyPort>info()Gets the list of busy ports on the current host.static List<NetStat.BusyPort>info(int port) Gets the list of busy ports associated with a single local port.static voidWrites the list returned byinfo()toSystem.out.private static List<NetStat.BusyPort>Parses the output ofLSOF_COMMAND.private static InetSocketAddressparseLsofEndpoint(NetStat.BusyPort.IPVersion ipVersion, String endpoint) private static NetStat.InetEndpointPairparseLsofEndpointPair(NetStat.BusyPort.IPVersion ipVersion, String endpointPair) Converts the TCP endpoint address pair obtained from the "name" field output fromlsofinto anInetSocketAddress.private static List<NetStat.BusyPort>parseNetTop(Stream<String> lines) Parses the output ofNETTOP_COMMAND.private static NetStat.BusyPortparseWindowsCsv(String line) Parses a response line fromPOWERSHELL_NETSTAT_COMMAND.private static voidprocessNettopEndpoints(NetStat.BusyPort.IPVersion ipVersion, NetStat.BusyPort.Builder builder, String line, Pattern endpointPattern, Function<String, String> endpointHostMapper)
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGER -
LSOF_WARNING_EMITTED
-
POWERSHELL_NETSTAT_COMMAND
-
SUDO_PREFIX
-
LSOF_COMMAND
-
NETTOP_COMMAND
-
TCP4_ENDPOINT_PATTERN
Pattern for parsing IPv4 endpoints expressed bynettop.- Group 1
- IP Address
- Group 2
- Port
-
TCP6_ENDPOINT_PATTERN
Pattern for parsing IPv6 endpoints expressed bynettop.- Group 1
- IP Address
- Group 2
- Port
nettopseparates the IP address and port with a period ('.'). The port can be a number or an asterisk ('*') when used as the undesignated target for a listening port.
-
-
Constructor Details
-
NetStat
private NetStat()Private niladic constructor to prevent instantiation.
-
-
Method Details
-
info
Gets the list of busy ports on the current host.Note that individual ports may appear more than once in the returned list. Depending on the source, a port may be listed for both IPv4 and IPv6 uses; for some applications (e.g.
sshd), a port may be shared among multiple processes.- Returns:
- the list of
NetStat.BusyPortinstances representing the busy TCP ports - Throws:
RuntimeException- if the busy port information cannot be obtained
-
info
Gets the list of busy ports associated with a single local port.Note that individual ports may appear more than once in the returned list. Depending on the source, a port may be listed for both IPv4 and IPv6 uses; for some applications (e.g.
sshd), a port may be shared among multiple processes.- Parameters:
port- the target port- Returns:
- the list of
NetStat.BusyPortinstances representing the busy TCP ports - Throws:
IllegalArgumentException- ifportis not a valid port numberRuntimeException- if the busy port information cannot be obtained
-
main
Writes the list returned byinfo()toSystem.out.- Parameters:
args- zero or more port numbers to check
-
parseWindowsCsv
Parses a response line fromPOWERSHELL_NETSTAT_COMMAND.- Parameters:
line- a comma-separated-value line containing:- OwningProcess
- LocalAddress
- LocalPort
- RemoteAddress
- RemotePort
- State
- ProcessCaption (mapping to
BusyPort.shortCommand) - ProcessCommandLine (mapping to
BusyPort.commandLine)
- Returns:
- a
BusyPortinstance formed fromline
-
parseLsof
Parses the output ofLSOF_COMMAND.The
Foptions are:p- process id
g- process group id
R- parent process id
c- command
L- process login name
f- file descriptor (fd)
t- file's type (IPv6 | IPv4)
P- protocol name (TCP)
n- internet address pair
T- TCP information
- ST
- connection state
- QR
- receive queue size
- QS
- send queue size
lsofcommand are structured for program consumption:- Fields within each line are separated by a NUL (
\0) - Lines are grouped by process
- a line beginning with a
pbegins each process group - each TCP ports used by the process is represented by a line beginning with an
f
- a line beginning with a
- The value of the
c(command) field is not the full command line; this value is applied as theBusyPort.shortCommand. This value is generally not sufficient to establish, diagnostically, what application is actually using a port -- all Java processes are listed simply asjava.
lsof:p1\0g1\0R0\0claunchd\0Lroot f21\0tIPv6\0PTCP\0n*:445\0TST=LISTEN\0TQR=0\0TQS=0 f22\0tIPv4\0PTCP\0n*:445\0TST=LISTEN\0TQR=0\0TQS=0 f24\0tIPv6\0PTCP\0n*:445\0TST=LISTEN\0TQR=0\0TQS=0 f25\0tIPv4\0PTCP\0n*:445\0TST=LISTEN\0TQR=0\0TQS=0 f35\0tIPv6\0PTCP\0n*:548\0TST=LISTEN\0TQR=0\0TQS=0 f36\0tIPv4\0PTCP\0n*:548\0TST=LISTEN\0TQR=0\0TQS=0 f37\0tIPv6\0PTCP\0n*:548\0TST=LISTEN\0TQR=0\0TQS=0 f38\0tIPv4\0PTCP\0n*:548\0TST=LISTEN\0TQR=0\0TQS=0 p121\0g121\0R1\0cUserEventAgent\0Lroot f71\0tIPv6\0PTCP\0n[fe80:5::aede:48ff:fe00:1122]:49177->[fe80:5::aede:48ff:fe33:4455]:59602\0TST=ESTABLISHED\0TQR=0\0TQS=0 f91\0tIPv6\0PTCP\0n[fe80:5::aede:48ff:fe00:1122]:49178\0TST=LISTEN\0TQR=0\0TQS=0 f94\0tIPv6\0PTCP\0n[fe80:5::aede:48ff:fe00:1122]:49179\0TST=LISTEN\0TQR=0\0TQS=0 f102\0tIPv6\0PTCP\0n[fe80:5::aede:48ff:fe00:1122]:49180\0TST=LISTEN\0TQR=0\0TQS=0 f103\0tIPv6\0PTCP\0n[fe80:5::aede:48ff:fe00:1122]:49181\0TST=LISTEN\0TQR=0\0TQS=0 f104\0tIPv6\0PTCP\0n[fe80:5::aede:48ff:fe00:1122]:49182\0TST=LISTEN\0TQR=0\0TQS=0 p22899\0g22706\0R22706\0cjava\0Lclifford f66\0tIPv6\0PTCP\0n127.0.0.1:14542\0TST=LISTENT\0QR=0T\0QS=0 f67\0tIPv6\0PTCP\0n127.0.0.1:51880\0TST=LISTENT\0QR=0T\0QS=0 f68\0tIPv6\0PTCP\0n127.0.0.1:54696->127.0.0.1:51828\0TST=ESTABLISHED\0TQR=0\0TQS=0- Parameters:
lines- aStreamdelivering lines from thelsofcommand- Returns:
- the list of busy ports
-
parseLsofEndpointPair
private static NetStat.InetEndpointPair parseLsofEndpointPair(NetStat.BusyPort.IPVersion ipVersion, String endpointPair) Converts the TCP endpoint address pair obtained from the "name" field output fromlsofinto anInetSocketAddress. The endpoint pair is of the following form:
If the port is a listener port, the remote endpoint portion is omitted.<localHost>':'<localPort>['->'<remoteHost>':'<remotePort>]From the man page for
lsof:...
the local host name or IP number is followed by a colon (':'), the port, ``->'', and the two-part remote address; IP addresses may be reported as numbers or names, depending on the +|-M, -n, and -P options; colon-separated IPv6 numbers are enclosed in square brackets; IPv4 INADDR_ANY and IPv6 IN6_IS_ADDR_UNSPECIFIED addresses, and zero port numbers are represented by an asterisk ('*'); a UDP destination address may be followed by the amount of time elapsed since the last packet was sent to the destination; TCP, UDP and UDPLITE remote addresses may be followed by TCP/TPI information in parentheses - state (e.g., ``(ESTABLISHED)'', ``(Unbound)''), queue sizes, and window sizes (not all dialects) - in a fashion similar to what netstat(1) reports; see the -T option description or the description of the TCP/TPI field in OUTPUT FOR OTHER PROGRAMS for more information on state, queue size, and window size;- Parameters:
ipVersion- theIPVersionconstant identifying the IP address typeendpointPair- tbe value of the "name" field from a file descriptor describing a TCP port/connection- Returns:
- an
InetEndpointPairdescribing the connection/port
-
parseLsofEndpoint
private static InetSocketAddress parseLsofEndpoint(NetStat.BusyPort.IPVersion ipVersion, String endpoint) -
parseNetTop
Parses the output ofNETTOP_COMMAND.The output begins with a header line which is dropped by this method. The remaining output is grouped:
- each group begins with process line
<processName>'.'<processId>',,' - following the process line, are one or more lines each of which describes a port used by
that process; each of these lines is of the form:
['tcp6'|'tcp4'] <localEndpoint>'<->'<remoteEndpoint>','<tcpState>',' - 'tcp4' endpoints are in the form
[<ipAddress>|'*']':'[<port>|'*'] - 'tcp6' endpoints are in the form
[<ipv6Address>'%'<interface>'.'<port>|'*'.[<port>|'*']]note that the<ipv6Address>is not wrapped in square brackets
nettop:launchd.1,, tcp6 *.445<->*.*,Listen, tcp4 *:445<->*:*,Listen, tcp6 *.548<->*.*,Listen, tcp4 *:548<->*:*,Listen, UserEventAgent.121,, tcp6 fe80::aede:48ff:fe00:1122%en11.49177<->fe80::aede:48ff:fe33:4455%en11.59602,Established, tcp6 fe80::aede:48ff:fe00:1122%en11.49178<->*.*,Listen, tcp6 fe80::aede:48ff:fe00:1122%en11.49179<->*.*,Listen, tcp6 fe80::aede:48ff:fe00:1122%en11.49180<->*.*,Listen, tcp6 fe80::aede:48ff:fe00:1122%en11.49181<->*.*,Listen, tcp6 fe80::aede:48ff:fe00:1122%en11.49182<->*.*,Listen, ... idea.22706,, tcp4 127.0.0.1:51828<->localhost:51173,Established, tcp4 127.0.0.1:6942<->*:*,Listen, tcp4 127.0.0.1:63342<->*:*,Listen, tcp4 127.0.0.1:51828<->*:*,Listen, tcp4 127.0.0.1:52002<->*:*,Listen, tcp4 127.0.0.1:52002<->127.0.0.1:64042,Established, tcp4 127.0.0.1:51828<->127.0.0.1:51172,Established, java.22899,, tcp4 127.0.0.1:14542<->*:*,Listen, tcp4 127.0.0.1:51880<->*:*,Listen, tcp4 127.0.0.1:51173<->127.0.0.1:51828,Established, java.22900,, tcp4 127.0.0.1:4720<->*:*,Listen, tcp4 127.0.0.1:51884<->*:*,Listen, tcp4 127.0.0.1:51172<->127.0.0.1:51828,Established,- Parameters:
lines- aStreamdelivering lines from thenettopcommand- Returns:
- the list of busy ports
- each group begins with process line
-
processNettopEndpoints
private static void processNettopEndpoints(NetStat.BusyPort.IPVersion ipVersion, NetStat.BusyPort.Builder builder, String line, Pattern endpointPattern, Function<String, String> endpointHostMapper)
-