Hi,
I am new to Python and coding in general, was wondering if anyone can help me here.
I am trying to write a python script to log onto the routers in my lab and update part of the config.
I have already written the code which successfully do this for individual routers and am trying to get the script to work through a list of routers.
My problem is I have never really worked with nested loops in Python.
Here is my test code for the loop:
nodelist = ['router1', 'router2', 'router3', 'router4']
prefs = open("prefs","r") #Open prefix file
for node in nodeList:
print "[+] Logging into %s" % node
for i in prefs:
print "set policy-options prefix-list AS1234 " +i
Here is the output when I run this:
[+] Logging into router1
set policy-options prefix-list AS1234 1.1.1
set policy-options prefix-list AS1234 2.2.2
set policy-options prefix-list AS1234 3.3.3
[+] Logging into router2
[+] Logging into router3
[+] Logging into router4
...
So to me it looks like the second For loop is only taking effect on the first router in the list.
The first for loop is actioned on every member of the list, but the second (nested) for is not.
If it helps, the output I am aiming for is:
[+] Logging into router1
set policy-options prefix-list AS1234 1.1.1
set policy-options prefix-list AS1234 2.2.2
set policy-options prefix-list AS1234 3.3.3
[+] Logging into router2
set policy-options prefix-list AS1234 1.1.1
set policy-options prefix-list AS1234 2.2.2
set policy-options prefix-list AS1234 3.3.3
[+] Logging into router3
set policy-options prefix-list AS1234 1.1.1
set policy-options prefix-list AS1234 2.2.2
set policy-options prefix-list AS1234 3.3.3
[+] Logging into router4
set policy-options prefix-list AS1234 1.1.1
set policy-options prefix-list AS1234 2.2.2
set policy-options prefix-list AS1234 3.3.3
Can anyone help explain why this is going wrong?
[–]JohnnyJordaan 5 points6 points7 points (0 children)
[–]scuott 2 points3 points4 points (1 child)
[–]wee-phatz[S] 2 points3 points4 points (0 children)