Skip to content

Instantly share code, notes, and snippets.

@MaxMatti
Last active July 17, 2021 05:57
Show Gist options
  • Save MaxMatti/73b7a06b64b683924d1d8125fa0b5182 to your computer and use it in GitHub Desktop.
Save MaxMatti/73b7a06b64b683924d1d8125fa0b5182 to your computer and use it in GitHub Desktop.
This script monitors the incoming and outgoing bandwidth of a server on localhost connected to a client on localhost.
#!/usr/bin/env bash
SERVERPORT=$1
PROCESSNAME=$(sudo lsof -n -i :"$SERVERPORT" | grep LISTEN | awk '{print $1}')
PID=$(sudo lsof -n -i :"$SERVERPORT" | grep LISTEN | awk '{print $2}')
CONNECTEDPORTS=$(sudo netstat -ant | awk -v port="$SERVERPORT" '{if ($6 == "ESTABLISHED" && $4 == "127.0.0.1:" port) {print $5} }' | cut -d":" -f 2)
sudo iptables -A INPUT -p tcp --dport "$SERVERPORT"
echo "$CONNECTEDPORTS" | xargs -L 1 sudo iptables -A INPUT -p tcp --dport
watch -n 1 sudo iptables -n -L -v -Z
sudo iptables -D INPUT -p tcp --dport "$SERVERPORT"
echo "$CONNECTEDPORTS" | xargs -L 1 sudo iptables -D INPUT -p tcp --dport
@MaxMatti
Copy link
Author

MaxMatti commented Jan 27, 2021

Note: this measures the bandwidth for each client separately and only for clients that run on localhost. If you just want to measure your servers bandwidth use this script instead.

Usage:

  • download the script
  • mark it as executable chmod +x monitor-bandwidth.sh
  • start your server
  • connect your client(s) to it
  • then call the script with a port number ./monitor-bandwidth.sh 25565
  • When done, cancel the output with Ctrl+C

Sample output:

Chain INPUT (policy ACCEPT 6354 packets, 905K bytes)
 pkts bytes target     prot opt in     out     source               destination         
 1784 92796            tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:25565
 4566  812K            tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:54884

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 6350 packets, 904K bytes)
 pkts bytes target     prot opt in     out     source               destination         
Zeroing chain `INPUT'
Zeroing chain `FORWARD'
Zeroing chain `OUTPUT'

How to read the output:

The relevant part is the section labeled Chain INPUT, more specifically the first, second and last column of that section.

The first column displays the amount of packets and the second column the amount of bytes sent on the port displayed in the last column. So in the sample output the server is running on port 25565 with the client being connected to it via port 54884. The download bandwidth is about 812 KiB/s and the upload bandwidth is 92796 B/s. If more clients were connected, they would each have one line in that section.

Explanation:

The script runs the command sudo iptables -n -L -v -Z every second. This means that it lets iptables count the amount of packets and bytes sent via two ports (the server port and the client port) and then resets the counters, so that the output is effectively giving information about packets per second and bytes per second.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment