ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.61k stars 1.31k forks source link

My code doesn't read the list of switches, only the first one, and hangs #3272

Open joand3512004 opened 1 year ago

joand3512004 commented 1 year ago

I had a perfectly working code, but I lost my computer, and I didn't save the backup. Now, I need to back up several switches again, and my code reads 2 files:

1 = cli 2 = device_list

However, for some reason, the script only reads the first switch, and stops running, anyone to help?

my_devices = open("device_list.txt")
comandos = open("cli.txt")
devyces = list()
username = input("Digite o seu usuário: ")
passw = stdiomask.getpass("Digite a sua senha: ")

for device_ip in my_devices:
    device = {
        "device_type": 'cisco_ios',
        "ip": device_ip,
        "username": username,
        "password": passw,
}    
    devyces.append(device)      

for device_ip in devyces:
    for comand in comandos:
        try:

            net_connect = ConnectHandler(**device_ip, session_timeout=100)
            print(f'\n Conectando no Switch:{device_ip["ip"]}')

            output = net_connect.send_command(comand)
            print (f'\n Enviando o comando: *{comand}*\n')
            print(output)

            print ('== Logout device', device_ip['ip'],' ==')

            net_connect.disconnect()
            #continue

        except NetMikoTimeoutException:
             print(f'\n Erro de TimeOut: *{device_ip}*')
             pass

        except AuthenticationException:
             print(f'\n Erro de Credencial: *{device_ip}*')
             pass

        except SSHException:
             print(f'\n Erro de Acesso: *{device_ip}*')
             pass

net_connect.disconnect()
ktbyers commented 1 year ago

Did you work this out?

Why do you try to make a new SSH connection for each command that you send?

What is the error that you run into?

nullbitzero commented 10 months ago

Hi, Maybe your code is missing reading the files you open. Try adding these parameters:

my_devices = open("device_list.txt") my_device_list=my_devices.read().splitlines() comandos = open("cli.txt") comandos_list=comandos.read().splitlines() devyces = []

for device_ip in my_device_list: device = { "device_type": 'cisco_ios', "ip": device_ip, "username": username, "password": passw, }
devyces.append(device)

for device_ip in devyces: for comand in comandos_list: try:

        net_connect = ConnectHandler(**device_ip, session_timeout=100)
        print(f'\n Conectando no Switch:{device_ip["ip"]}')

        output = net_connect.send_command(comand)
        print (f'\n Enviando o comando: *{comand}*\n')
        print(output)

        print ('== Logout device', device_ip['ip'],' ==')

        net_connect.disconnect()
        #continue

    except NetMikoTimeoutException:
         print(f'\n Erro de TimeOut: *{device_ip}*')
         pass

    except AuthenticationException:
         print(f'\n Erro de Credencial: *{device_ip}*')
         pass

    except SSHException:
         print(f'\n Erro de Acesso: *{device_ip}*')
         pass

    net_connect.disconnect()

regards. Nbz