Mini Shell

Direktori : /opt/sharedrads/
Upload File :
Current File : //opt/sharedrads/listacct

#!/bin/bash
# WTG - 4/25/2010
# Vanessa V 2/22/12 - support for multiple main IPs
# Vanessa V 8/12/13 - support for resellers
# Yuriy S 8/26/16 - el7 ifconfig compat

if [ -f /var/cpanel/mainips/root ];then
    MAINIPS=($(cat /var/cpanel/mainip ; echo '' ; cat /var/cpanel/mainips/root))
else
    MAINIPS=$(cat /var/cpanel/mainip)
fi

function pointed_local {
    for DNS in $(grep ^DNS /var/cpanel/users/$1);
    do
        domain=$(echo $DNS|awk '{gsub(/DNS.*=/,"",$0);print}')
        siteip=$(dig +short $domain)
        plocal="NO"
        if grep -q "release 7" /etc/redhat-release; then
            localinterfaces=($(/sbin/ifconfig | grep 'inet ' | awk '{gsub(/addr:/,"",$0);printf "%s ",$2}END{printf "\n"}'))
        else
            localinterfaces=($(/sbin/ifconfig | grep 'inet addr:' | awk '{gsub(/addr:/,"",$0);printf "%s ",$2}END{printf "\n"}'))
        fi
        for localip in ${localinterfaces[@]};
        do
            # check if the domain is pointed to the server
            if [ "$localip" == "$siteip" ];then
                 return 1;
            fi
        done

    done
    return 0;

}

function in_array() {
    local hay needle=$1
    shift
    for hay; do
        [[ $hay == $needle ]] && return 0
    done
    return 1
}

function check_suspended () {
    if [ -f /var/cpanel/suspended/$1 ];
    then
        AGE=$(stat --format=%Y /var/cpanel/suspended/$1)
    AGE=$(($(date +%s)-$AGE))
    AGE=$(($AGE/86400))
    suspended="SUSPENDED $AGE DAYS $(cat /var/cpanel/suspended/$1)"

    fi
}

function has_ded_ip () {
        SITEIP=$(grep IP /var/cpanel/users/$1 | sed -e 's_IP=__')
        if ! in_array $SITEIP "${MAINIPS[@]}"; then
           return 1;
        fi
    return 0;
}

function has_ssl () {
    test -f /var/cpanel/userdata/$1/*SSL && return 1
    return 0;

}

function homedir () {
    homedir=$(cat /etc/passwd | grep '^'$1':' | cut -d: -f6)
}

function get_owner () {
    owner=$(egrep "^OWNER=" /var/cpanel/users/$1 | cut -d= -f2)
}

function is_reseller () {
    if egrep ^$1: /var/cpanel/resellers >/dev/null; then
        return 1;
    fi
    return 0;
}

function has_local_mx () {
    for DNS in $(grep ^DNS /var/cpanel/users/$1);
    do
        domain=$(echo $DNS|awk '{gsub(/DNS.*=/,"",$0);print}')
        mxip=$(dig +short $(dig MX $domain +short | awk '{print $2}'))
        if grep -q "release 7" /etc/redhat-release; then
            localinterfaces=($(/sbin/ifconfig | grep 'inet ' | awk '{gsub(/addr:/,"",$0);printf "%s ",$2}END{printf "\n"}'))
        else
            localinterfaces=($(/sbin/ifconfig | grep 'inet addr:' | awk '{gsub(/addr:/,"",$0);printf "%s ",$2}END{printf "\n"}'))
        fi
        for localip in ${localinterfaces[@]};
        do
            # check if the domain is pointed to the server
            if [ ! "$localip" == "$mxip" ];then
                 return 0;
            fi
        done

    done
    return 1;
}

function large_disk {
    TMPFILE=$(mktemp);
    echo "Fetching Largest Accounts"
    for username in $(find /var/cpanel/users/ -type f -exec basename '{}' \;);
    do
        #diskspace in megabytes
        diskspace=$(quota -v $username 2>/dev/null| awk '/\/dev\// { sum +=$2 } END { print (sum/1024)}' )
        echo $username $diskspace
    done  | sort -nk2  >> $TMPFILE

    printf "%10s %10s %10s %15s %15s %15s %18s %18s %18s    %s\n" "Username" "Owner" "Reseller?" "Diskspace" "Homedir" "Pointed Local?" "Dedicated IP?" "Has SSL?" "local MX?" "Suspended?"
    for username in $(tail -50 $TMPFILE|awk '{print $1}');
    do
        #account owner
        get_owner $username
        #diskspace in megabytes
        diskspace=$(quota -v $username 2>/dev/null| awk '/\/dev\// { sum +=$2 } END { print (sum/1024)}' )
        suspended="ACTIVE"
        check_suspended $username
        plocal="NO"
        pointed_local $username
        test $? -gt 0 && plocal="YES"
        is_reseller="NO"
        is_reseller $username
        test $? -eq 1 && is_reseller="YES"
        dedicated_ip="USES MAIN IP"
        has_ded_ip $username
        test $? -eq 1  && dedicated_ip="DEDICATED IP"
        SSL="NO CERT"
        has_ssl $username
        homedir $username
        test $? -eq 1 && SSL="SSL CERT"
        localMX="LOCAL MX"
        has_local_mx $username
        test $? -eq 1 && SSL="REMOTE MX"
        printf "%10s %10s %10s %15s %15s %15s %18s %18s %18s    %s\n" "$username" "$owner" "$is_reseller" "$diskspace" "$homedir" "$plocal" "$dedicated_ip" "$SSL" "$localMX" "$suspended"

    done
    rm $TMPFILE
}

function only_suspended () {

    printf "%10s %10s %10s %10s %10s %10s %15s %15s %15s    %s\n" "Username" "Owner" "Reseller?" "Diskspace" "Homedir" "Pointed Local?" "Dedicated IP?" "Has SSL?" "local MX?" "Suspended?"
    for username in $(find /var/cpanel/suspended/ -type f -not -name '*.lock' -printf '%f\n')
    do
        diskspace=$(quota -v $username 2>/dev/null| awk '/\/dev\// { sum +=$2 } END { print (sum/1024)}' )
        suspended="ACTIVE"
        check_suspended $username
        plocal="NO"
        pointed_local $username
        dedicated_ip="USES MAIN IP"
        get_owner $username
        is_reseller="NO"
        is_reseller $username
        test $? -eq 1 && is_reseller="YES"
        has_ded_ip $username
        test $? -eq 1  && dedicated_ip="DEDICATED IP"
        SSL="NO CERT"
        has_ssl $username
        homedir $username
        test $? -eq 1 && SSL="SSL CERT"
        localMX="LOCAL MX"
        has_local_mx $username
        test $? -eq 1 && SSL="REMOTE MX"
        printf "%10s %10s %10s %10s %10s %10s %15s %15s %15s    %s\n" "$username" "$owner" "$is_reseller" "$diskspace" "$homedir" "$plocal" "$dedicated_ip" "$SSL" "$localMX" "$suspended"
    done
}


#the current layout is stupid.  there should be a gen_report function that takes a username and gets the data,
#this would also allow you to look up only one user.  this will do until me or someone else comes along to write it. -t.
case $1 in
 --suspended)
    only_suspended
;;
--large_disk)
    large_disk
;;
*)
    large_disk
;;
esac

Zerion Mini Shell 1.0