即使一台主机不可用也继续循环
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()
使用try except? - https://docs.python.org/3/tutorial/errors.html#handling-exceptions
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)
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()
使用try except? - https://docs.python.org/3/tutorial/errors.html#handling-exceptions
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)