0

I have a function script using netmiko and I want to create a function for the send_command but I really got no idea how to do it.

def estb_conn(ip,uname,pname,instruct):
cisco_ios = {
    'device_type': 'cisco_ios',
    'ip': ip,
    'username': uname,
    'password': pname,
}

ios_connect = netmiko.ConnectHandler(**cisco_ios)
display = ios_connect.find_prompt()

**** I want to make this send_command as new function so I can freely call it anywhere when I need a multiple command or for loop****

command = ios_connect.send_command(instruct)
time.sleep(1)
Klaus D.
  • 13,874
  • 5
  • 41
  • 48

1 Answers1

1

Yes, you can create new functions in Python. You can even do what you want (it's called partial application and closure).

def netmiko_partial_from_stackoverflow (ip, uname, pname):
    cisco_ios = {
        'device_type': 'cisco_ios',
        'ip': ip,
        'username': uname,
        'password': pname,
    }

    ios_connect = netmiko.ConnectHandler(**cisco_ios)
    display = ios_connect.find_prompt()

    def inner_function_from_stackoverflow(instruct):
        res = ios_connect.send_command(instruct)
        time.sleep(1)
        return res
    

After that you can use it like that:

mycommand_from_stackoverflow = netmiko_partial_from_stackoverflow(someip, someuname, somepnmae)
mycommand_from_stackoverflow('show running-config')
mycommand_from_stackoverflow('show inferfaces')

George Shuklin
  • 6,952
  • 10
  • 39
  • 80