Data Type

  • Primitive
  • Nonprimitive

Primitive Data:

  • Integer
  • Float
  • String
  • Boolean

Nonprimitive:

  • List
    >>> devices = ["ASA", "NEXUS", "CATALYST", "ASR"]
    
  • Tuple
      ip_addr = ("10.254.0.1", "10.254.0.2", "10.254.0.3")
    
  • Dictionary
    if_state = {"Gi0/1":"shutdown", "Gi0/2":"no shutdown"}
    
  • Set
    {"Gi0/1", "Gi0/2", "Gi0/3", "Gi0/1"}
    

To list the methods that can be used on a particular data type, create a variable with that type and then issue the dir() built-in method.

>>> vendor = "Cisco"
>>> dir(vendor)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

There are four collection data types in the Python programming language:

List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
Dictionary is a collection which is ordered** and changeable. No duplicate members.

Modules and Packages

A module is a single file with the .py extension that contains some usable code. A package is a collection of modules stored in a folder where the name of the folder is the name of the package.

import netmiko: This method will import everything in the Netmiko module. Suppose that Netmiko has a variable called VERSION that is defined in its file. To use that variable, you will need to use the following format to refer to it: netmiko.VERSION. The same requirement applies to the classes and functions. ConnectHandler is part of the Netmiko module, and you can use it by following the same syntax: netmiko.ConnectHandler().

from netmiko import ConnectHandler: This method tells python to extract the object (in this instance, the object is the ConnectHandler) from the module and make it directly callable. Directly callable means that you do not need to refer to the module name and the object. Instead, you can use the class name directly. The same approach applies to the functions and variables.

from netmiko import ConnectHandler as ch: Some objects have long names, and to make them shorter, you can create aliases of the original object. Now, instead of ConnectHandler, you can use ch.

Here are some examples of Python modules:

device_data.py

USERNAME = "cisco"
PASSWORD = "cisco"
DEVICE_TYPE = "cisco_xe"
INVENTORY = ["csr1kv1", "csr1kv2", "csr1kv3"]
This module only has variables. Other Python scripts and other Python modules can use these variables. Here is an example of a script that uses the device_data.py module:

automation_code.py

import device_data
print(device_data.USERNAME)
print(device_data.INVENTORY)
import device_data as dd
print(dd.USERNAME)

from device_data import USERNAME
print(USERNAME)

from device_data import USERNAME as user 
print(user)

from device_data import PASSWORD 
print(PASSWORD)

Netmiko

wrapper for paramiko (default SSH library in Python)

Netmiko common methods:

  • ConnectHandler(): initiates SSH connections, need to provide hostname, user, password, device_type
    device = ConnectHandler(host="csr1kv1", username="cisco", password="cisco", device_type="cisco_xe")
    
  • device.is_alive() - method to dertermine if connection with device is still alive
  • establish_connection() - to reiniciniate connection
    device.is_alive() False >>> device.establish_connection() >>> device.is_alive() True
    
  • send_command
    >>> device.send_command("show version")
    'Cisco IOS XE Software, Version 16.09.03\nCisco IOS Software [Fuji], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.9.3, RELEASE SOFTWARE (fc2)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2019 by Cisco Systems, Inc.\nCompiled Wed 20-Mar-19 07:56 by mcpre\n\n\nCisco IOS-XE software, Copyright (c) 2005-2019 by cisco Systems, Inc.\nAll rights reserved.  Certain components of Cisco IOS-XE software are\nlicensed under the GNU General Public License ("GPL") 
    <... output omitted ...>
    
  • send_config_from_file()
    % cat interface_conf.cfg
    interface GigabitEthernet1
    description SALES
    >>> device.send_config_file( "/home/student/config_files/interface_conf.cfg")
    'config term\nEnter configuration commands, one per line.  End with CNTL/Z.\ncsr1kv1(config)#interface GigabitEthernet1\ncsr1kv1(config-if)#  description SALES\ncsr1kv1(config-if)#end\ncsr1kv1#'
    >>>
    
  • send_config_set([, ]):
  • open_session_log(): Use this method to log your session to a file for debugging purposes. ```

device.open_session_log(“/home/student/logs/CSR.log”) % cat CSR.log csr1kv1#show version Cisco IOS XE Software, Version 16.09.03 Cisco IOS Software [Fuji], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.9.3, RELEASE SOFTWARE (fc2) Technical Support: http://www.cisco.com/techsupport Copyright (c) 1986-2019 by Cisco Systems, Inc. Compiled Wed 20-Mar-19 07:56 by mcpre Cisco IOS-XE software, Copyright (c) 2005-2019 by cisco Systems, Inc. All rights reserved. Certain components of Cisco IOS-XE software are licensed under the GNU General Public License (“GPL”) Version 2.0. The software code licensed under GPL Version 2.0 is free software that comes with ABSOLUTELY NO WARRANTY. You can redistribute and/or modify such GPL code under the terms of GPL Version 2.0. For more details, see the documentation or “License Notice” file accompanying the IOS-XE software, or the applicable URL provided on the flyer accompanying the IOS-XE software. <… output omitted …> ```

  • session_timeout: This variable defines the number of seconds after which the session should time out.

<
Previous Post
Git
>
Next Post
BGP Summary Checkpoint