Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

continue loop even if one host is not available

import time

import paramiko

import sys

from getpass import getpass

#First Unifi AP IP Address
firstip=3

fistdigits = '10.0.0.'

#how can we prevent from crashing if 10.0.0.19 is not an available device
while firstip<=100:

    host= f'{fistdigits}{firstip}'
    #first one will be 10.0.0.3 then 10.0.0.4 etc

    username="username"
    password="password"

    session= paramiko.SSHClient()

    session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    session.connect(hostname=host,
                   username=username,
                   password=password)

    #upgradeurl="https://dl.ui.com/unifi/firmware/U7PG2/4.3.13.11253/BZ.qca956x.v4.3.13.11253.200430.1420.bin"
    #stdin, stdout, stderr = session.exec_command("upgrade "+{upgradeurl})
    stdin, stdout, stderr = session.exec_command("reboot")

    output=stdout.read()
    print(output)
    print("Command executed for device:   "+host)
    firstip +=1
    time.sleep(.5)
    print(stdout.read().decode())


session.close()

>Solution :

Use try except? – https://docs.python.org/3/tutorial/errors.html#handling-exceptions

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import time

import paramiko

import sys

from getpass import getpass

#First Unifi AP IP Address
firstip=3

fistdigits = '10.0.0.'

#how can we prevent from crashing if 10.0.0.19 is not an available device
while firstip<=100:

    host= f'{fistdigits}{firstip}'
    #first one will be 10.0.0.3 then 10.0.0.4 etc

    username="username"
    password="password"

    try:

        session= paramiko.SSHClient()

        session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        session.connect(hostname=host,
                       username=username,
                       password=password)

        #upgradeurl="https://dl.ui.com/unifi/firmware/U7PG2/4.3.13.11253/BZ.qca956x.v4.3.13.11253.200430.1420.bin"
        #stdin, stdout, stderr = session.exec_command("upgrade "+{upgradeurl})
        stdin, stdout, stderr = session.exec_command("reboot")

        output=stdout.read()
        print(output)
        print(stdout.read().decode())
        print("Command executed for device:   "+host)
    except TimeoutError as e:
        print("Exception occurred while interacting with host {}: {}".format(host, e))
    finally:
        session.close()
    firstip +=1
    time.sleep(0.5)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading