Skip to content
Advertisement

The best way to call a function automatically, each time a class is used?

I’m creating a class to hold functions that all have a similar goal and as I’m typing this out I realize that all of the functions will need to use the same 80% of their code, which involves connecting to the networking devices first.

What would be the most efficient way to run the Netmiko(**device) function below and only type it out in my code once? After connecting I would need to continue on to the specific function that held the command. Maybe there is an even more effective method using a switch case? I don’t know classes very well though, so I was curious if there was a nice way to do this within the class functions.

class DisplayDeviceInfo:

    # Each function will need to run net_connect = Netmiko(**device), to connect to the network device.  Is there a clean way of grabbing the 'networkdevice' variable I'm passing and connect here and then continue on to the original function that was called?

    #I was thinking doing it here but this area is only read once when the class is created.
    
    def int_status(networkdevice):
        try:
            device = {
                'host': networkdevice[1],
                'username': username,
                'password': password,
                'device_type': 'cisco_ios',
            }
            net_connect = Netmiko(**device)


            
            output = net_connect.send_command('show interface status')
            print(output)
        except (NetMikoTimeoutException, NetMikoAuthenticationException):
            print('Unable to connect to the device :(')

    def err_disabled(networkdevice):
        try:
            device = {
                'host': networkdevice[1],
                'username': username,
                'password': password,
                'device_type': 'cisco_ios',
            }
            net_connect = Netmiko(**device)



        
            output = net_connect.send_command('show interfaces status err-disabled')
            if(len(output) == 0):
                print()
                print('This switch has no ports in an err-disabled state.')
                print()
            else:
                print(output)

        except (NetMikoTimeoutException, NetMikoAuthenticationException):
            print('Unable to connect to the device :(')

Advertisement

Answer

You’re using your class as a “pure class”. You need to create instances instead.

class NetDevice(object):
    def __init__(self, address, username, password, device_type = "cisco_ios"):
        self.address = address
        self.username = username
        self.password = password
        self.device_type = device_type
        self.session = None

    @property
    def netmiko_params(self) -> dict:
        return {
            'host': self.address,
            'username': self.username,
            'password': self.password,
            'device_type': self.device_type,
        }

    def connect(self):
        self.session = Netmiko(**self.netmiko_params)

    def err_disable(self):
        # Simply use self.session where you were using net_connect
        s = self.session.command("show interfaces")
        ...
        return results

Usage:

device = NetDevice("10.1.0.1", "bob", "cisco", "cisco_ios")
device.connect()
device.err_disable()


username = "bob"
password = "cisco"
device_type = "cisco_ios"

for address in addresses:
    device = NetDevice(address, username, password, device_type)
    device.connect()
    device.err_disable()
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement