Script for obtaining an overview of all users and their corresponding domains, along with DNS data

Post Reply
User avatar
isscbta
Team Member
Posts: 130
Joined: Mon Jul 19, 2021 1:41 am
Has thanked: 16 times
Been thanked: 3 times

This script is designed to efficiently gather and display detailed information about users and their associated domains on a hosting server. For each user, it fetches and neatly presents essential data such as full name, email, package details, disk usage, and bandwidth. Additionally, for every domain linked to a user, the script retrieves DNS records, including A, NS, and MX records, along with their respective IP addresses. The output is formatted for enhanced readability, making it an valuable tool for system administrators and hosting providers who need to quickly assess and oversee their hosting environment.

In your SSH, as root, run:

Code: Select all

mcedit script-name.sh
Paste following code

Code: Select all

#!/bin/bash

for user in $(grep '@' /etc/passwd |cut -f1 -d:); do
    if [ ! -f "/usr/local/vesta/data/users/$user/user.conf" ]; then
        continue;
    fi
    FULLNAME=$(v-list-user $user | grep 'FULL NAME:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    EMAIL=$(v-list-user $user | grep 'EMAIL:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    PACKAGE=$(v-list-user $user | grep 'PACKAGE:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    DISK=$(v-list-user $user | grep 'DISK:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    BANDWIDTH=$(v-list-user $user | grep 'BANDWIDTH:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    SUSPENDED=$(v-list-user $user | grep 'SUSPENDED:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    if [ "$SUSPENDED" = "yes" ]; then
        continue;
    fi

    echo "USER: $user = [$FULLNAME] | [$EMAIL] | [PACK: $PACKAGE] | [DISK: $DISK] | [BNDT: $BANDWIDTH]"
    for domain in $(/usr/local/vesta/bin/v-list-web-domains $user plain |cut -f 1); do
        site=$domain
        aip=$(dig +short $site)
        ns=$(dig -t ns +short $site | tr '\n' ' ')
        mx=$(dig -t mx +short $site | head -n 1 | awk '{ print $2 }' | sed "s|\.$||g")
        mxip=$(dig +short $mx | tr '\n' ',')

        echo "=== Domain: $domain"
        echo "    A Record: $aip"
        echo "    NS Records: $ns"
        echo "    MX Record: $mx ($mxip)"
        echo ""
    done
    echo ""
done

Code: Select all

chmod a+x script-name.sh
At the end, run the script:

Code: Select all

./script-name.sh
ADDITIONAL:

If you want to view user or domain data without DNS data, use this code for faster processing:

Code: Select all

#!/bin/bash

for user in $(grep '@' /etc/passwd |cut -f1 -d:); do
    if [ ! -f "/usr/local/vesta/data/users/$user/user.conf" ]; then
        continue;
    fi
    FULLNAME=$(v-list-user $user | grep 'FULL NAME:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    EMAIL=$(v-list-user $user | grep 'EMAIL:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    PACKAGE=$(v-list-user $user | grep 'PACKAGE:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    DISK=$(v-list-user $user | grep 'DISK:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    BANDWIDTH=$(v-list-user $user | grep 'BANDWIDTH:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    SUSPENDED=$(v-list-user $user | grep 'SUSPENDED:' | cut -d ":" -f2- | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' -e 's/[[:blank:]]+/ /g')
    if [ "$SUSPENDED" = "yes" ]; then
        continue;
    fi
   
    echo "USER: $user = [$FULLNAME] | [$EMAIL] | [PACK: $PACKAGE] | [DISK: $DISK] | [BNDT: $BANDWIDTH]"
    for domain in $(/usr/local/vesta/bin/v-list-web-domains $user plain |cut -f 1); do
        echo "=== $domain"
    done
    echo ""
    echo ""
done
Post Reply