Skip to content

Instantly share code, notes, and snippets.

@dev-ir
Last active July 12, 2024 11:09
Show Gist options
  • Save dev-ir/9e0d30603a7f9c50700c1d48a206af4d to your computer and use it in GitHub Desktop.
Save dev-ir/9e0d30603a7f9c50700c1d48a206af4d to your computer and use it in GitHub Desktop.
#!/bin/bash
#add color for text
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
plain='\033[0m'
NC='\033[0m' # No Color
cur_dir=$(pwd)
# check root
[[ $EUID -ne 0 ]] && echo -e "${RED}Fatal error: ${plain} Please run this script with root privilege \n " && exit 1
install_jq() {
if ! command -v jq &> /dev/null; then
# Check if the system is using apt package manager
if command -v apt-get &> /dev/null; then
echo -e "${RED}jq is not installed. Installing...${NC}"
sleep 1
sudo apt-get update
sudo apt-get install -y jq
else
echo -e "${RED}Error: Unsupported package manager. Please install jq manually.${NC}\n"
read -p "Press any key to continue..."
exit 1
fi
fi
}
require_command(){
sudo apt update
install_jq
check_and_install_netstat
sudo apt install nethogs
}
check_and_install_netstat() {
if ! command -v netstat >/dev/null; then
echo "netstat ......."
sudo apt install net-tools -y
if command -v netstat >/dev/null; then
echo "netstat installed."
else
echo "wrong netstat."
exit 1
fi
else
echo "netstat exist"
fi
if ! command -v bc &> /dev/null; then
sudo apt update
sudo apt install -y bc
fi
}
list_ports(){
clear
ports=$(ss -tnlp | grep LISTEN | awk '{print $4}' | awk -F':' '{print $NF}' | sort -n | uniq)
echo "+=====================+"
echo "| List port by Usage |"
echo "+=====================+"
for port in $ports; do
total_bytes=$(sudo iptables -nvxL | grep "dpt:$port" | awk '{total+=$2} END {print total}')
if [[ ! -z "$total_bytes" ]]; then
total_gb=$(echo "scale=2; $total_bytes/1024/1024" | bc)
echo -e "|${GREEN}$port${NC}=${RED}${total_gb}GB${NC}"
fi
done
echo "+=====================+"
echo "CHANNEL : @DVHOST_CLOUD"
read -p "1- again | ctrl + c to exit : " menu
case $menu in
1)
list_ports
;;
esac
}
require_command
clear
list_ports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment