Skip to content

Commit d2491d1

Browse files
authored
Merge pull request #2 from shubhamku044/mac-network_bandwidth
fix: network_bandwidth for mac
2 parents 51cd715 + 8bfecea commit d2491d1

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

scripts/network_bandwidth.sh

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@
22

33
INTERVAL="1" # update interval in seconds
44

5-
network_name=$(tmux show-option -gqv "@tmux2k-network-bandwidth")
5+
# Network interface to monitor
6+
network_name="en0"
7+
8+
if [[ $(uname -s) == "Darwin" ]]; then
9+
network_name="en0" # en0 is wifi, TODO: Update network_name
10+
elif [[ $(uname -s) == "Linux" ]]; then
11+
network_name=$(tmux show-option -gqv "@dracula-network-bandwidth")
12+
else
13+
# update this later for window
14+
echo "Unknown operating system"
15+
exit 1
16+
fi
617

718
main() {
819
while true
@@ -12,40 +23,50 @@ main() {
1223
output_download_unit=""
1324
output_upload_unit=""
1425

15-
initial_download=$(cat /sys/class/net/"$network_name"/statistics/rx_bytes)
16-
initial_upload=$(cat /sys/class/net/"$network_name"/statistics/tx_bytes)
26+
if [[ $(uname -s) == "Linux" ]]; then
27+
initial_download=$(cat /sys/class/net/"$network_name"/statistics/rx_bytes)
28+
initial_upload=$(cat /sys/class/net/"$network_name"/statistics/tx_bytes)
29+
30+
sleep "$INTERVAL"
1731

18-
sleep "$INTERVAL"
32+
final_download=$(cat /sys/class/net/"$network_name"/statistics/rx_bytes)
33+
final_upload=$(cat /sys/class/net/"$network_name"/statistics/tx_bytes)
34+
else
35+
initial_download=$(netstat -I "$network_name" -b | tail -n 1 | awk '{print $7}')
36+
initial_upload=$(netstat -I "$network_name" -b | tail -n 1 | awk '{print $10}')
1937

20-
final_download=$(cat /sys/class/net/"$network_name"/statistics/rx_bytes)
21-
final_upload=$(cat /sys/class/net/"$network_name"/statistics/tx_bytes)
38+
sleep $INTERVAL
39+
40+
final_download=$(netstat -I "$network_name" -b | tail -n 1 | awk '{print $7}')
41+
final_upload=$(netstat -I "$network_name" -b | tail -n 1 | awk '{print $10}')
42+
fi
2243

23-
total_download_bps=$(expr "$final_download" - "$initial_download")
24-
total_upload_bps=$(expr "$final_upload" - "$initial_upload")
44+
total_download_bps=$(expr $final_download - $initial_download)
45+
total_upload_bps=$(expr $final_upload - $initial_upload)
2546

26-
if [ "$total_download_bps" -gt 1073741824 ]; then
47+
if [ $total_download_bps -gt 1073741824 ]; then
2748
output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}')
2849
output_download_unit="gB/s"
29-
elif [ "$total_download_bps" -gt 1048576 ]; then
50+
elif [ $total_download_bps -gt 1048576 ]; then
3051
output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}')
3152
output_download_unit="mB/s"
3253
else
3354
output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/$2}')
3455
output_download_unit="kB/s"
3556
fi
3657

37-
if [ "$total_upload_bps" -gt 1073741824 ]; then
58+
if [ $total_upload_bps -gt 1073741824 ]; then
3859
output_upload=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}')
3960
output_upload_unit="gB/s"
40-
elif [ "$total_upload_bps" -gt 1048576 ]; then
61+
elif [ $total_upload_bps -gt 1048576 ]; then
4162
output_upload=$(echo "$total_upload_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}')
4263
output_upload_unit="mB/s"
4364
else
4465
output_upload=$(echo "$total_upload_bps 1024" | awk '{printf "%.2f \n", $1/$2}')
4566
output_upload_unit="kB/s"
4667
fi
4768

48-
echo "$output_download $output_download_unit • ↑ $output_upload $output_upload_unit"
69+
echo "$output_download$output_download_unit • ↑ $output_upload$output_upload_unit"
4970
done
5071
}
5172
main

0 commit comments

Comments
 (0)