Find user Bandwidth via SSH shell (CPanel/WHM)
Introduction:
Monitoring bandwidth usage per user is useful for identifying high-traffic accounts, troubleshooting resource usage, and managing hosting limits. If you have SSH access to a cPanel server, you can quickly check individual user bandwidth consumption using a simple shell script.
Step 1: Create the Script File
Log in to your server via SSH and create a new file:
vi bandwidth
Add the following script:
#!/bin/bash
cd /var/cpanel/bandwidth/
ls | grep -v "\." | xargs -n 1 -izzz sh -c '
echo -n zzz " = ";
egrep "^$1\..*\.$2-all" zzz | awk -F"=" "
BEGIN {bytes=0}
{ bytes+=\$2 }
END {print bytes/1024/1024 \" MB\"}"
'
cd -
Step 2: Make the Script Executable
Save the file and provide execute permissions:
chmod +x bandwidth
Step 3: Run the Script
Execute the script by passing two parameters:
- Month
- Year
Syntax:
./bandwidth month year
Example:
To check bandwidth usage for January 2012:
./bandwidth 1 2012
Step 4: Display Top Bandwidth Consumers
To list the top 10 users consuming the highest bandwidth:
./bandwidth 1 2012 | sort -nrk 3 | head -10
This command will:
- Sort the output in descending order
- Display the accounts with the highest bandwidth usage first
- Show bandwidth usage in MB
Example output:
user1 = 2048 MB
user2 = 1850 MB
user3 = 1502 MB
Mapping Usernames to Domain Names
If you want to identify which domain belongs to a username, check:
/etc/trueuserdomains
This file maps cPanel usernames to their corresponding domains.
Example:
example.com: user1
testsite.com: user2
Conclusion
Using this simple SSH script, you can quickly monitor bandwidth consumption for individual cPanel users without navigating through the control panel. This is especially useful for server administrators who need to track heavy bandwidth users and optimize server resource usage efficiently.
