1

I have 3 routers on my test lab environment, I'm trying to reach these routers and send some commands using for-loop and send output in the way I type/list them.

I have 3 1841 Cisco routers and running 12.4 IOS:

    multi_ip_addr = ['192.168.1.1', '192.168.2.1', '192.168.3.1']
    for ips in multi_ip_addr:
        pass
    len_ip_addr = len(multi_ip_addr)
    for len_ip in range(len_ip_addr):
        device = {'ip': ips, 
        'username': 'admin', 
        'password': 'password'}
        print(device)

My expected result is:

{'ip': '192.168.1.1', 'username': 'admin', 'password': 'password'} {'ip': '192.168.2.1', 'username': 'admin', 'password': 'password'} {'ip': '192.168.3.1', 'username': 'admin', 'password': 'password'}

however I am getting this output:

{'ip': '192.168.3.1', 'username': 'admin', 'password': 'password'} {'ip': '192.168.3.1', 'username': 'admin', 'password': 'password'} {'ip': '192.168.3.1', 'username': 'admin', 'password': 'password'}
4
  • for len_ip in range(len): That is an error. len is a function. You can't call range() on a function. Presumably you meant range(len_ip_addr ), but we can't tell for sure. Please post your actual code. Commented May 31, 2019 at 18:51
  • you mean range(len_ip_addr) ? Commented May 31, 2019 at 18:52
  • sorry, I have edited my post. Thanks for the comments. Commented May 31, 2019 at 18:55
  • How does the output you get differ from what you want? Commented May 31, 2019 at 20:32

4 Answers 4

2
multi_ip_addr = ['192.168.1.1', '192.168.2.1', '192.168.3.1']
for ips in multi_ip_addr:
    device = {'ip':ips, 'username':'admin', 'password':'password'}
    print(device)
Sign up to request clarification or add additional context in comments.

Comments

2

You need to get the item out of the list of IPs. Use the for loop for that, not a range:

for ip in multi_ip_addr:
    device = {
        'ip': ip,
        'username': 'admin',
        'password': 'password'
    }
    print(device)

The first for loop will overwrite the value of ips on each loop, so really by the end of the loop, ips equals the last value of multi_ip_addr.

Comments

1
for ips in multi_ip_addr:
    pass

As this loop executes, ips takes on the value of each item in multi_ip_addr. When the loop ends, ips retains the value of the last item.

In the next loop, you never change the value of ips, so it has the same value every time.

Comments

0

Python determines context with indentation, so if you want to use your ips variable for each entry, you need to do it within the for ips in multi_ip_addr context (where you currently only have pass.) I might also change the name of ips to ip if I were you, just to make things clearer.

Here's how I would rewrite your loop:

template_dict = {'ip' : None, 'username' : 'admin', 'password' : 'password'}
multi_ip_addr = ['192.168.1.1', '192.168.2.1', '192.168.3.1']

for ip in multi_ip_addr:
    print(template_dict.update({'ip' : ip})

Comparison with JS:

Using range(len(multi_ip_addr)) will give you a set of index values, but this is unnecessary for what you are trying to accomplish. If you're familiar with JavaScript: the default for loop behavior in python is like the using the for...of pattern:

EX:

for (let i of <some_iterable>) {
    console.log(i);
}

would be equivalent to:

for i in <some_iterable>:
    print(i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.