import json
import subprocess
from pathlib import PurePath, Path
from gen_pass import gen_pass

class PasswordReset:
    def __init__(self, default_pass, ip_addrs):
        self.password = default_pass
        self.ip_addrs = ip_addrs

    def change(self):
        password_map = {}

        for ip in self.ip_addrs:
            new_pass = gen_pass()
            script_dir = PurePath(__file__).parent
            script_path = str(Path(script_dir) / 'reset_scripts' / 'ssh_linux.exp') # Should be dynamic
            command = ['expect', '-f', script_path, 'tester', self.password, ip, new_pass,]

            try:
                subprocess.run(command, check=True)
            except subprocess.CalledProcessError as e:
                print("Error changing password: ", e)

            password_map[ip] = new_pass

        return password_map


def main():
    res = PasswordReset("badpass", ["192.168.7.242"])
    print(json.dumps(res.change()))

if __name__ == '__main__':
    main()
