Skip to content

Lab 17 - Getting Started with Functions

This lab walks you through an introduction of using functions so you can start writing more modular code and eliminate any duplicate code in your scripts.

Task 1 - Introduction to Functions

Step 1-1

In /home/ntc/labs/python directory, create a script called functions.py.

Step 1-2

Open the script in your text editor.

Step 1-3

Create a function that returns a hard-coded list of VLANs.

  • The function should be called get_vlans.
  • The function should return the list that has VLANs 1, 5, 10, and 20.
  • You should then call the function, save the list into a new variable called vlans, and print it out.

The script should look like this:

#! /usr/bin/env python


def get_vlans():
    return [1, 5, 10, 20]


vlans = get_vlans()

print(vlans)

Save the script.

Step 1-4

Execute the script. You'll see a basic output:

ntc@ntc-training:python$ python functions.py
[1, 5, 10, 20]

Step 1-5

Add another function called vlan_exists in functions.py. It should return True or False if the VLAN ID you pass into the function exists.

It should look like this:

#! /usr/bin/env python


def get_vlans():
    return [1, 5, 10, 20]


def vlan_exists(vlan_id):
    return vlan_id in get_vlans()


vlans = get_vlans()
print(vlans)

print(vlan_exists(10))
print(vlan_exists(12))

Note: You could have also broken it down in multiple statements like this:

def vlan_exists(vlan_id):
    vlans = [1, 5, 10, 20]
    is_vlan_valid = vlan_id in vlans
    return is_vlan_valid

Step 1-6

Save, then execute the script:

ntc@ntc-training:python$ python functions.py
[1, 5, 10, 20]
True
False

Task 2 - Use Functions to Connect to Network Devices

Step 2-1

In /home/ntc/labs/python directory, create a script called cisco-connect.py.

Step 2-2

In the cisco-connect.py file, create a function that connects to a Cisco IOS device and call it ez_cisco.

ez_cisco should accept four parameters:

  • hostname of the device to connect to
  • username of the device
  • password of the device
  • show command to execute on the device

The function header and layout should look like this, for now just printing all parameters passed to the function:

def ez_cisco(hostname, username, password, show_command):
    print(hostname)
    print(username)
    print(password)
    print(show_command)

Step 2-3

You can test it by using the following statement:

ez_cisco("csr1", "ntc", "ntc123", "show version")

The updated script should look like this:

def ez_cisco(hostname, username, password, show_command):
    print(hostname)
    print(username)
    print(password)
    print(show_command)


ez_cisco("csr1", "ntc", "ntc123", "show version")

Step 2-4

Save and Execute the script. You should see the following:

ntc@ntc-training:python$ python cisco-connect.py
csr1
ntc
ntc123
show version

Step 2-5

Make the following changes to the cisco-connect.py script:

  • Import the ConnectHandler netmiko object.
  • Perform the desired action - pass in the show_command parameter.
  • The function should return the command output as a string.
  • Test the script by issuing the show version to csr1.
from netmiko import ConnectHandler


def ez_cisco(hostname, username, password, show_command):
    platform = "cisco_ios"
    device = ConnectHandler(
        ip=hostname, username=username, password=password, device_type=platform
    )

    output = device.send_command(show_command)
    device.disconnect()

    return output


response = ez_cisco("csr1", "ntc", "ntc123", "show version")

print(response)

Step 2-6

Save and Execute the script. You should see the following:

ntc@ntc-training:python$ python cisco-connect.py
Cisco IOS XE Software, Version 17.01.01
Cisco IOS Software [Amsterdam], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 17.1.1, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2019 by Cisco Systems, Inc.
Compiled Fri 22-Nov-19 03:39 by mcpre
--- OUTPUT SNIPPED ---

Step 2-7

Since the credentials are the same for all of our devices, we can treat them as "optional" arguments. Let's update the function header so they are optional, but provide defaults of ntc and ntc123 that the user can override.

from netmiko import ConnectHandler


def ez_cisco(hostname, show_command, username="ntc", password="ntc123"):
    platform = "cisco_ios"
    device = ConnectHandler(
        ip=hostname, username=username, password=password, device_type=platform
    )

    output = device.send_command(show_command)
    device.disconnect()

    return output


response = ez_cisco("csr1", "show version")

print(response)

When you don't use "keyword" arguments, the arguments are positional and are required. In order to accomplish this, we also needed to re-locate show_command as the second argument.

Now you only need to pass the hostname of the device and desired show command to run.

Step 2-8

Execute the following function calls in your script:

response = ez_cisco("csr1", "show version")
print(response)

response = ez_cisco("csr2", "show ip int brief")
print(response)

response = ez_cisco("csr3", "show run | inc snmp")
print(response)

The final script looks like this:

from netmiko import ConnectHandler


def ez_cisco(hostname, show_command, username="ntc", password="ntc123"):
    platform = "cisco_ios"
    device = ConnectHandler(
        ip=hostname, username=username, password=password, device_type=platform
    )

    output = device.send_command(show_command)
    device.disconnect()

    return output


response = ez_cisco("csr1", "show version")
print(response)

response = ez_cisco("csr2", "show ip int brief")
print(response)

response = ez_cisco("csr3", "show run | inc snmp")
print(response)

Step 2-9

Save and Execute the script, checking that you get the desired output from each device.