Mini Shell

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

#!/opt/imh-python/bin/python3
__author__ = "NickWe"
# This is hostfilemods rewritten from bash to python

import argparse
import sys

# FUNCTIONS
def parse_args():
    parser = argparse.ArgumentParser(description='Output hosts file entries')
    group = parser.add_mutually_exclusive_group()

    group.add_argument(
        "user",
        nargs="?",
        default="",
        action="store",
        type=str,
        help="print hosts entries for specified user",
    )

    group.add_argument(
        "-a",
        "--all",
        action="store_true",
        default=False,
        help="output hosts entries for all users",
    )

    group.add_argument(
        "-l",
        "--list",
        action="store_true",
        default=False,
        help="list all cPanel users",
    )

    parser.add_argument(
        "-s",
        "--separate-line",
        action="store_true",
        default=False,
        help="print www. subdomains on a separate line",
    )

    parser.add_argument(
        "-r",
        "--reseller",
        action="store_true",
        default=False,
        help="include hosts for accounts owned by reseller",
    )

    return parser.parse_args()


# list all cPanel users
def list_users():
    user_file = "/etc/trueuserowners"
    users = []

    try:
        with open(user_file) as raw_users:
            for user in raw_users:
                user = user.rstrip("\n")
                users.append(user.partition(":")[0])
    except OSError:
        print("Error reading file " + user_file)
        sys.exit(1)

    return users


def get_reseller_children(reseller):
    user_file = "/etc/trueuserowners"
    reseller_file = "/var/cpanel/resellers"
    children = []

    try:
        with open(user_file) as raw_users:
            with open(reseller_file) as resellers:
                if reseller not in resellers.read():
                    print(f"{reseller} is not a reseller!")
                    sys.exit(1)
                for line in raw_users:
                    if reseller in line:
                        line = line.rstrip("\n")
                        children.append(line.partition(":")[0])

    except OSError:
        print("Error reading file" + user_file)
        sys.exit(1)

    return children


def obtain_hosts_file_entries(user):
    userdata_file = "/var/cpanel/users/" + user

    try:
        with open(userdata_file) as userdata:
            domains = []
            for line in userdata:
                line = line.rstrip("\n")
                if "IP=" in line:
                    ip = line.partition("=")[2]  # [2] -> left side
                if "DNS" in line and not "X" in line:
                    domains.append(line.partition("=")[2])

    except OSError:
        print("Error reading file " + userdata_file)
        sys.exit(1)
    return (ip, domains)


# separate indicates to print www. lines separately
def print_hosts_entries(ip_domains, separate):
    (ip, domains) = ip_domains
    for domain in domains:
        if separate:
            print(ip + "\t" + domain + "\n" + ip + "\t" + "www." + domain)
        else:
            print(ip + "\t" + domain + " " + "www." + domain)


# main
def main():
    args = parse_args()

    if args.user is None:
        print("must specify a user or reseller user")
        sys.exit(1)

    if args.reseller and args.user:
        for child in get_reseller_children(args.user):
            print_hosts_entries(
                obtain_hosts_file_entries(child), args.separate_line
            )

    elif args.reseller and args.user is None:
        print("Must specify a reseller!")
        sys.exit(1)

    if args.list:
        print(list_users())

    if args.all:
        # print hosts entries for all users
        for user in list_users():
            print_hosts_entries(
                obtain_hosts_file_entries(user), args.separate_line
            )

    try:
        if args.user and not args.reseller:
            with open("/etc/trueuserowners") as user_file:
                if args.user not in user_file.read():
                    print(f"{args.user} is not a valid user!")
                    sys.exit(1)
            print_hosts_entries(
                obtain_hosts_file_entries(args.user), args.separate_line
            )

    except OSError:
        print("Bad input, goodbye")


if __name__ == "__main__":
    main()

Zerion Mini Shell 1.0