grelleum / youtube-network-automation

Files created in my YouTube series "Network Automation using Python and Netmiko."
MIT License
71 stars 26 forks source link

commands #3

Closed adityamoon closed 6 years ago

adityamoon commented 6 years ago

hi greg,

thanks for the scripts. I am facing one issue where only last command in commands.txt is getting parsed and saved in the file.

grelleum commented 6 years ago

Most likely the problem is that you are either executing the command outside of the loop, or you are saving the output of the command outside of the loop.

Consider the following code:

for command in commands:
    print('*** Inside the loop ***')
    print('***', command, '***')
print('### outside the loop ###')
print('###', command, '###')

Here is what the output of that code looks like:

*** show clock ***
*** Inside the loop ***
*** show interfaces ***
*** Inside the loop ***
*** show version ***
### outside the loop ###
### show version ###

Notice that the print statements that are indented each run three times, once for each command. Notice that the print statements that have the same indentation as the for command in commands: are not considered part of that for loop and only are executed once, after the loop completes. The command printed is the last value that was assigned to command before the loop completed.

Please let me know if that helps. If you need futher assitance, please post your code and the output using markdown to denote the code. https://guides.github.com/features/mastering-markdown/