Getting High-Level Per-Protocol Statistics with netstat on macOS (IPv4/IPv6)
With the recent addition of IPv6 access to your Internet Service Provider (ISP), you might be interested in exploring some high-level per-protocol statistics on your macOS system. One of the most effective ways to achieve this is by using the built-in netstat command.
What is netstat?
netstat is a versatile command-line tool available on Unix-based systems, including macOS. It is used for monitoring network connections, routing tables, and network interface statistics. With netstat, you can obtain detailed information about your system's network status, making it a valuable tool for network troubleshooting and performance analysis.
Preparing for netstat
Before you start, ensure that your macOS system has the necessary support for IPv6. To check, open the Terminal application and enter the following command:
sysctl net.inet6.ip6.forwarding
If the output is:
net.inet6.ip6.forwarding: 0
IPv6 is disabled. To enable IPv6, use the following command:
sudo sysctl -w net.inet6.ip6.forwarding=1
Now, let's dive into obtaining high-level per-protocol statistics using netstat.
Displaying Per-Protocol Statistics
To display per-protocol statistics, use the following command:
netstat -I -n -p
Here's a breakdown of the options used:
-I: Displays statistics for a specific network interface. Usenetstat -I -hfor a list of interfaces.-n: Displays numerical addresses instead of resolving hostnames and port names.-p: Displays the process ID (PID) and process name owning the socket.
For example, to display statistics for the en0 interface, use:
netstat -I en0 -n -p
This command will display a table with the following columns:
- Proto: The protocol (TCP, UDP, or IP)
- Recv-Q: The count of bytes not copied by the user program connected to this socket
- Send-Q: The count of bytes not acknowledged by the remote host
- Local Address: The local address and port number
- Foreign Address: The remote address and port number
- State: The state of the socket (ESTABLISHED, LISTEN, etc.)
- PID/Program name: The ID and name of the process that owns the socket
Filtering Results by Protocol
To filter the results by protocol, use the grep command. For example, to display only IPv4 TCP connections, use:
netstat -I en0 -n -p | grep tcp4
For IPv6 UDP connections, use:
netstat -I en0 -n -p | grep udp6
With the netstat command and its options, you can obtain high-level per-protocol statistics on your macOS system. This information is useful for monitoring network performance and troubleshooting issues related to IPv4 and IPv6 connections.
References
- Apple Developer: netstat Manual Page
- Red Hat: Using netstat to Monitor Network Connections
- Man7: netstat Manual Page