fix(systemadmin): don't hardcode http20 network interface

The hardcoded `eth0` network interface in the `http20` function doesn't
exist on macOS. It's more reliable to dynamically determine the
appropriate interface, so this change checks what would be used to reach
Cloudflare's public 1.1.1.1 DNS service (yes, another hardcoded thing,
but at least it's common across the Internet) and uses that interface.

For example, on my macOS system, it returns either `en0` (Wi-Fi) or
`en7` (the Ethernet slot on my Thunderbolt dock).

On Linux, this uses the commonly installed `ip route` command from the
[iproute2](https://wiki.linuxfoundation.org/networking/iproute2)
project. The macOS port of some of these utilities is available from
[Homebrew](https://brew.sh) by running `brew install iproute2mac`.

This change also splits the pipeline in the function across multiple
lines per this project's
[Code Style Guide](https://github.com/ohmyzsh/ohmyzsh/wiki/Code-Style-Guide).
This commit is contained in:
Mark Gardner 2025-03-07 11:39:37 -06:00
parent 1c2127727a
commit f695d1264b
No known key found for this signature in database

View file

@ -94,7 +94,13 @@ function req20() {
# top20 of Using tcpdump port 80 access to view
function http20() {
sudo tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr | head -n 20
sudo tcpdump -i $(ip route get 1.1.1.1 \
| awk '/^1.1.1.1 via / {print $5}') -tnn dst port 80 -c 1000 \
| awk -F"." '{print $1"."$2"."$3"."$4}' \
| sort \
| uniq -c \
| sort -nr \
| head -n 20
}
# top20 of Find time_wait connection