From 5f47b1644d4dd4c045c22145c093c9e919281b2a Mon Sep 17 00:00:00 2001 From: Emma Date: Tue, 30 Jan 2024 11:16:57 -0800 Subject: [PATCH 1/3] add a way to detect network interface on macOS - typically returns en0, or if a VPN is on it will most likely show one of the `utun` interfaces --- scripts/network_bandwidth.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/network_bandwidth.sh b/scripts/network_bandwidth.sh index 939052a..b560812 100755 --- a/scripts/network_bandwidth.sh +++ b/scripts/network_bandwidth.sh @@ -26,6 +26,11 @@ interface_get() { name="$(ip -o route get 192.168.0.0 | awk '{print $5}')" fi ;; + Darwin) + if type route >/dev/null; then + name="$(route -n get 192.168.0.0 2>/dev/null | awk '/interface: / {print $2}')" + fi + ;; esac fi From 509c2c114f7a67838ab51e50cd81144e43ca4c4e Mon Sep 17 00:00:00 2001 From: Emma Date: Tue, 30 Jan 2024 13:00:12 -0800 Subject: [PATCH 2/3] update interface_bytes and get_bandwidth to support macOS as well --- scripts/network_bandwidth.sh | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/scripts/network_bandwidth.sh b/scripts/network_bandwidth.sh index b560812..b381e24 100755 --- a/scripts/network_bandwidth.sh +++ b/scripts/network_bandwidth.sh @@ -37,23 +37,38 @@ interface_get() { echo "$name" } -# interface_bytes give interface name and signal tx/rx return Bytes +# interface_bytes give an interface name and return both tx/rx Bytes, separated by whitespace (upload first) interface_bytes() { - cat "/sys/class/net/$1/statistics/$2_bytes" + case "$(uname -s)" in + Linux) + upload=$(cat "/sys/class/net/$1/statistics/tx_bytes") + download=$(cat "/sys/class/net/$1/statistics/rx_bytes") + + echo "$upload $download" + ;; + Darwin) + # column 7 is Ibytes (in bytes, rx, download) and column 10 is Obytes (out bytes, tx, upload) + netstat -nbI "$1" | tail -n1 | awk '{print $10 " " $7}' + ;; + esac } # get_bandwidth return the number of bytes exchanged for tx and rx get_bandwidth() { - upload="$(interface_bytes "$1" "tx")" - download="$(interface_bytes "$1" "rx")" + local upload=0 + local download=0 - #wait the interval for Wait for interval to calculate the difference + IFS=' ' read -r upload download <<< "$(interface_bytes "$1")" + + # wait for interval to calculate the difference sleep "$INTERVAL" - upload="$(bc <<<"$(interface_bytes "$1" "tx") - $upload")" - download="$(bc <<<"$(interface_bytes "$1" "rx") - $download")" + IFS=' ' read -r new_upload new_download <<< "$(interface_bytes "$1")" - #set to 0 by default useful for non-existent interface + upload=$(( $new_upload - $upload )) + download=$(( $new_download - $download )) + + # set to 0 by default echo "${upload:-0} ${download:-0}" } From 43ee6f2cb0d62ea8804081117c246ff02c220720 Mon Sep 17 00:00:00 2001 From: Emma Date: Tue, 30 Jan 2024 14:14:10 -0800 Subject: [PATCH 3/3] replace bc usage with an awk floating point math limiting to 2 decimal places --- scripts/network_bandwidth.sh | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/scripts/network_bandwidth.sh b/scripts/network_bandwidth.sh index b381e24..bad4138 100755 --- a/scripts/network_bandwidth.sh +++ b/scripts/network_bandwidth.sh @@ -85,7 +85,7 @@ bandwidth_to_unit() { local result="0.00" if (($1 != 0)); then - result="$(bc <<<"scale=2; $1 / $size")" + result="$(awk -v a="$1" -v b="$size" 'BEGIN { printf "%.2f\n", a / b }' /dev/null - then - echo "command bc could not be found!" - exit 1 - fi - while true; do if ((counter == 0)); then counter=60