-1

Got my first script working and very excited about it. Modifying it to work with functions and running into issues. Appreciate help with this so I can better understand. As I understand I can define a function and then I can call it again. So I created a function below "sh_ap_sum". I'm trying to run that so that it dumps the output of the command to a file.

Then I am calling that function again with "sh_ap_sum(cdp)" and trying to display the output of the file in the terminal window. Am I completely doing it wrong any hints will be appreciated greatly. Thank you.

from netmiko import ConnectHandler
import sys

def sh_ap_sum(ap_sum):

        ipaddr = input('IP:''')
        net_connect =  ConnectHandler(ip = ipaddr,port = 22,username = 'admin',password = 'mypassword',device_type = 'cisco_wlc_ssh')
        net_connect.find_prompt()
        '(Cisco Controller) >'
        output = net_connect.send_command('show ap summary')
        sys.stdout = open('wlccon.txt', 'w')
        net_connect.disconnect()

sh_ap_sum(cdp)

        output = net_connect.send_command('show ap cdp ne all')
        print(output)
Ali
  • 77
  • 6
  • Besides the cited duplicate, please look up other resources on passing information into and out of a function. Also see [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Prune Mar 17 '20 at 18:32
  • Thanks I thought I added all the explanation already in there including the full code, I am a bit confused on what else I should have added here? Very new to this like less than a day new so this isn't making sense much. Also checking out that scoping rules link as well. PS: I have posted this here after spending quit some time trying to utilize multiple other resources online. – Ali Mar 17 '20 at 19:34

1 Answers1

0

net_connect is defined in your function. You can't access it after you already called sh_ap_sum() on the third-to-last line. At that point, re-indenting and trying to re-enter the function scope is not possible. Plus, you've already called net_connect.disconnect() at the end of your function.

You will need to make another connection, or return the output you want from the function itself.

kojiro
  • 74,557
  • 19
  • 143
  • 201
  • Thank you makes sense. I ended up using two commands under the same function and dumped those to a text file which worked. I still would like to figure out few other things but that is a separate issue. Thank you for your help. – Ali Mar 17 '20 at 21:20