D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
2
/
cwd
/
opt
/
dedrads
/
Filename :
reset_cpanel
back
Copy
#!/usr/lib/rads/venv/bin/python3 """Resets cPanel passwords on VPS/Dedi""" from os import listdir import sys from pathlib import Path import os import random import string import argparse import logging from cpapis import whmapi1 from rads import color, setup_logging, is_cpuser def api_success(json_response): """deprecated""" try: return int(json_response['status']) == 1 except (TypeError, KeyError, ValueError): pass try: return int(json_response['metadata']['result']) == 1 except (TypeError, KeyError, ValueError): pass try: return int(json_response['result']['status']) == 1 except (TypeError, KeyError, ValueError): try: return int(json_response['result'][0]['status']) == 1 except (TypeError, IndexError, KeyError, ValueError): pass return False def get_args(): """Parse users from commandline, prompt for them, or take option for all""" parser = argparse.ArgumentParser(description=__doc__) group = parser.add_mutually_exclusive_group() # fmt: off group.add_argument( '-u', '--user', dest='users', nargs='*', default=[], help='user(s) to reset password for', ) group.add_argument( '-a', '--all', action="store_true", help='reset ALL cPanel passwords' ) parser.add_argument( '-m', '--message', help='This flag is here for compatibility with sharedrads and ' 'does nothing.', ) # fmt: on args = parser.parse_args() if args.all: return get_cpusers() if args.users: for user in args.users: if not is_cpuser(user): print(f'{user} is not a valid cpanel user') args.users.remove(user) return args.users sys.exit('-u [user [user...]] or -a expected') def get_cpusers(): """get a list of all cPanel users""" cpaneluserdir = '/var/cpanel/users/' cpanelusers = [f for f in listdir(cpaneluserdir) if is_cpuser(f)] return cpanelusers def reset_users_from_list(user_list): """loop through user_list and reset password""" length = 13 chars = '{0.ascii_letters}{0.digits}!@#$%^&*()'.format(string) for editinguser in user_list: random.seed = os.urandom(1024) newpass = ''.join(random.choice(chars) for i in range(length)) print(f"New cPanel Password for {editinguser}: {color.yellow(newpass)}") if api_success( whmapi1('passwd', {"user": editinguser, "password": newpass}) ): print(color.green('success')) else: print(color.red('FAILED')) # clear sessions in whm/cpanel sessions = Path("/var/cpanel/sessions/") for file in (sessions / "raw").glob(f"{editinguser}:*"): file.unlink() for file in (sessions / "cache").glob(f"{editinguser}:*"): file.unlink() def main(): """Main function: setup loging, obtain args, reset passwords""" setup_logging( path='/var/log/suspension.log', loglevel=logging.INFO, print_out=False ) results = get_args() reset_users_from_list(results) if __name__ == "__main__": main()