0

Trying to write a script to do some auto configuration for network devices using data from NetBox. I want the users to be able to input a list of sites and have the script check if those sites are valid. Code is this:

while True:
    sites = [str(x) for x in input("Which site(s) need to be configured: ").split(', ')]
    for site in sites:
        if not nb.dcim.sites.filter(name=sites):
            print(f"{site} not a valid site")
            continue
        else:
            print(f"{site} is valid")
            break

When running it, the code correctly loops if the site is not valid. But when a valid site is entered it still loops. See output below

» python sites.py
Which site(s) need to be configured: x
x not a valid site
Which site(s) need to be configured: x
x not a valid site
Which site(s) need to be configured: x
x not a valid site
Which site(s) need to be configured: SITE1
SITE1 is valid
Which site(s) need to be configured: SITE1
SITE1 is valid

I think the while True needs to be something else perhaps? or the break is not working as expected

Jeff-C
  • 77
  • 1
  • 7
  • I linked a duplicate which is exactly what you are looking for. In particular, you want to have a look at this answer: https://stackoverflow.com/a/654002/5079316 – Olivier Melançon Oct 14 '20 at 15:54
  • why do you need 2 loops in the first place? Take one site per iteration. If you insist, set an break out of the while loop input and only then enter the for loop – buran Oct 14 '20 at 15:55
  • 2 loops are needed because the input could be a list, so need to loop over that to check vlaidilty – Jeff-C Oct 14 '20 at 15:59

0 Answers0