import json
from napalm import get_network_driver
def save_built_config(file_name, data):
    with open(file_name, "w") as f:
        f.write(data)
    return file_name

def get_inventory():
    """
    Lit le fichier inventory/hosts.json et retourne son contenu.
    """
    inventory_file = "inventory/hosts.json"
    try:
        with open(inventory_file, "r") as f:
            data = json.load(f)
        return data
    except FileNotFoundError:
        print(f"Erreur : le fichier {inventory_file} n'existe pas.")
        return []

def get_json_data_from_file(file):
    pass

def question_26(device):
    command = ['show ip interface brief']
    output = device.cli(command)

def question_27(device):
    pass


def question_28(device):
    command = ['show ip interface brief']
    output = device.cli(command)
    print(type(output))

def question_29(device):
    #output = device.get_config()
    output = device.get_arp_table()
    print(output)


def question_30(device):
    output = device.get_arp_table()
    print(type(output))


def question_31():
    device.load_merge_candidate(filename='config/loopback_R01.conf')
    print(device.compare_config())
    device.commit_config()


def question_33():
    r01 = {
        'hostname': '172.16.100.62',
        'username': 'cisco',
        'password': 'cisco'
    }
    r02 = {
        'hostname': '172.16.100.190',
        'username': 'cisco',
        'password': 'cisco'
    }
    r03 = {
        'hostname': '172.16.100.254',
        'username': 'cisco',
        'password': 'cisco'
    }

    routers = {'1': r01, '2': r02, '3': r03}

    for i in range(1, 4):
        driver = get_network_driver('ios')
        device = driver(**routers[str(i)])
        device.open()
        device.load_merge_candidate(filename=f'config/ospf_R{i}.conf')
        print(device.compare_config())
        device.commit_config()
        device.close()

def question_35():
    liste_hosts = get_inventory()

    for host in liste_hosts:
        nom_host = host['hostname']

        connexion_info = {
            'hostname': host['ip'],
            'username': host['username'],
            'password': host['password']
        }

        try:
            driver_ios = get_network_driver('ios')
            device = driver_ios(**connexion_info)
            device.open()
            config_sauvegarde = device.get_config()

            with open(f'backup/{nom_host}.bak', 'w') as fichier_sauvegarde:
                fichier_sauvegarde.write(str(config_sauvegarde))

            device.close()

        except Exception as erreur:
            print(f"Erreur lors de la connexion à {nom_host} : {erreur}")
            continue



if __name__ == "__main__":
    r01 = {
        'hostname':'172.16.100.62',
        'username': "cisco",
        'password': "cisco"
    }

    driver = get_network_driver('ios')
    device  = driver(**r01)
    device.open()
    
    #question_26(device)
    #question_27(device)
    #question_28(device)
    #question_29(device)
    #question_30(device)
    #question_31()
    #question_33()
    question_35()