How to stop while loop after running twice if the condition is satisfied
Budget: ₹600 – ₹1,500 INR
I want stop this while loop after running twice if the condition is satisfied. Please help
# Loop through devices and log in every 5 minutes
while True:
for device in devices:
log_into_device()
time.sleep(30) # polling interval in seconds before running again
def log_into_device():
# send a command to the device and print the output
cpu_output = ssh_conn.send_command("show cpu")
# Check if CPU utilization is over 50%
for line in cpu_output.splitlines(): # use splitlines() instead of split("\n")
if "300 sec avg:" in line:
cpu_utilization = line.split()
test = int(cpu_utilization[3])
if test >= 1: # check if utilization is over 50%
# print a message and save output to a file
print(f"CPU utilization on {devices['ip']} is over 50%")
# Loop through devices and log in every 5 minutes
while True:
for device in devices:
log_into_device()
time.sleep(30) # polling interval in seconds before running again
def log_into_device():
# send a command to the device and print the output
cpu_output = ssh_conn.send_command("show cpu")
# Check if CPU utilization is over 50%
for line in cpu_output.splitlines(): # use splitlines() instead of split("\n")
if "300 sec avg:" in line:
cpu_utilization = line.split()
test = int(cpu_utilization[3])
if test >= 1: # check if utilization is over 50%
# print a message and save output to a file
print(f"CPU utilization on {devices['ip']} is over 50%")