[ SEA-GHOST MINI SHELL]
#!/bin/env python2.7
# Script to collect and calculate total disk usage by all email accounts
# ARGS:
# RETURNS:
# total_accounts: 123 total_space_used: 123121
# total_accounts: amount of email accounts hosted on server
# total_space_used: total disk space used by all email accounts in Megabytes
import os
import json
import subprocess
# SPAGETTI CODE FOR SIMPLE TASK
OUTPUT_TYPE = "json"
API_CALL_ARGS = "Email listpopswithdisk --output={ot}".format(ot=OUTPUT_TYPE)
CPAPI_COMMAND = "cpapi2"
OUTPUT_TEMPLATE = "total_accounts: {ta}\t total_space_used: {ts}"
def users_from_domainusers(path="/etc/domainusers"):
users = []
with open(path, 'r') as wf:
for line in wf.xreadlines():
username, domain = line.split(':')
users.append(username.strip())
return users
def listpops_with_disk_api_call(username):
command = "{cp} --user={u} {args}".format(
cp=CPAPI_COMMAND,
u=username,
args=API_CALL_ARGS
)
pipe = subprocess.Popen([command], shell=True, stdout=subprocess.PIPE)
return json.loads(pipe.stdout.read())
def disk_usage_from_json(json_data):
data = json_data["cpanelresult"]["data"]
disk_used = 0
for account in data:
disk_used += float(account["diskused"])
return disk_used
def emailacc_count_from_json(json_data):
data = json_data["cpanelresult"]["data"]
return len(data)
if __name__ == "__main__":
total_usage = 0.0
total_accounts = 0
for account in users_from_domainusers():
json_data = listpops_with_disk_api_call(account)
total_usage += disk_usage_from_json(json_data)
total_accounts += emailacc_count_from_json(json_data)
print(OUTPUT_TEMPLATE.format(ta=total_accounts, ts=total_usage))
SEA-GHOST - SHELL CODING BY SEA-GHOST