Exemple #1
0
def main():
    global log
    format = '%(asctime)s - %(filename)s - %(levelname)s - %(message)s'
    logging.basicConfig(level=logging.INFO, format=format)

    log = logging.getLogger(__name__)

    testbed_filename = '/home/cisco/labpyats/pyats_testbed.yaml'
    testbed = Genie.init(testbed_filename)

    commands_to_gather = {
        'asa': [
            'show inventory', 'show running-config', 'show route',
            'show ospf neighbor', 'show license all'
        ],
        'iosxe': [
            'show inventory', 'show running-config', 'show ip route vrf *',
            'show ip ospf neighbor', 'show license feature'
        ],
        'nxos': [
            'show inventory', 'show running-config', 'show ip route vrf all',
            'show ip ospf neighbor vrf all', 'show license usage'
        ]
    }

    dir_name = 'gathered_commands'

    collect_device_commands(testbed, commands_to_gather, dir_name)
Exemple #2
0
    def genie_testbed(self, testbed):
        '''Create the genie testbed'''
        try:
            # If pyATS, then call their use_testbed api, then convert
            self.builtin.get_library_instance('ats.robot.pyATSRobot').\
                         use_testbed(testbed)
            testbed = self.builtin.get_library_instance('ats.robot.'\
                                                        'pyATSRobot').testbed
        except RuntimeError:
            # No pyATS
            self.testbed = loader.load(testbed)
        else:
            # Has pyATS, so converted and then save locally and also for pyATS
            self.testbed = Genie.init(testbed)
            self.builtin.get_library_instance('ats.robot.'\
                                              'pyATSRobot').testbed =\
                                              self.testbed
        self.testscript.parameters['testbed'] = self.testbed

        # Load Genie Datafiles (Trigger, Verification and PTS)

        # This make UUT mandatory. When learning, aka no trigger
        # the UUT are not mandatory
        self.loaded_yamls = True
        self._load_genie_datafile()
        if not self.trigger_datafile:
            self.loaded_yamls = False
            log.warning("Could not load the Datafile correctly")
 def setup(self, testbed):
     # initalize genie testbed
     testbed = Genie.init(testbed)
     # execute check for each device in the testbed using loops
     device_names = [d for d in testbed.devices.keys()]
     aetest.loop.mark({{cookiecutter.testcase_class}}Device,
                      uids=device_names,
                      device=testbed.devices.values())
Exemple #4
0
def load(testbed, devices_name):
    tb = Genie.init(testbed)
    ret = []
    for device in devices_name:
        try:
            ret.append(tb.devices[device])
        except KeyError as e:
            raise KeyError("Could not find '{d}' within "
                           "testbed '{tb}'".format(d=device, tb=testbed))
    return ret
 def setUpClass(cls):
     testbed = """
     devices:
         R1:
             os: iosxe
             type: router
             connections: {}
     """
     cls.tb = Genie.init(testbed)
     cls.device = cls.tb.devices['R1']
     cls.device.parse = pos_parsed
    def get_devices(self, testbed):
        genie_testbed = Genie.init(testbed)
        self.parent.parameters["testbed"] = genie_testbed
        device_list = []

        # Attempt to establish connection with each device
        for device in genie_testbed.devices.values():
            if device.os in ["iosxe", "nxos", "iosxr"]:
                log.info(banner("Connect to device '{d}'".format(d=device.name)))
                device_list.append(device)

        # Pass list of devices the to testcases
        self.parent.parameters.update(dev=device_list)
Exemple #7
0
    def genie_init(self, testscript, testbed, uut_alias, helper_alias, steps,
                   context):
        """ Initialize the environment """

        with steps.start('Initializing the environment for'
                         ' Genie Configurable Objects'):

            # Context to mention which Commnand type to use. (Cli/Yang)
            # It has to be set before the Genie convertion, as the right
            # Interface has to be instantiated.
            # The default value is 'cli', so only do it in the 'yang' case
            if context == 'yang':
                # Set all device to yang
                # Or, only specific device could be set to use 'yang', and the
                # rest use 'cli'
                for dev in testbed.devices.values():
                    dev.context = 'yang'

            # Initialize Genie Testbed
            # It also sets the variable 'Genie.testbed', to hold the testbed
            Genie.init(testbed=testbed)

            # Test script parameters are added so
            # these can be passed on to the subsections.
            uut = Genie.testbed.find_devices(aliases=[uut_alias])[0]
            helper = Genie.testbed.find_devices(aliases=[helper_alias])[0]
            testscript.parameters['uut'] = uut
            testscript.parameters['helper'] = helper

            # Overwrite the pyATS testbed for Genie Testbed
            testscript.parameters['testbed'] = Genie.testbed

            # area_id from the datafile
            testscript.parameters['area_id'] = self.area_id

            # Mark testcase with looping information
            aetest.loop.mark(ping, device=[uut, helper])
Exemple #8
0
    def connect(self, testbed):
        genie_testbed = Genie.init(testbed)
        self.parent.parameters['testbed'] = genie_testbed
        device_list = []
        for device in genie_testbed.devices.values():
            log.info(banner("Connect to device '{d}'".format(d=device.name)))
            try:
                device.connect()
            except Exception as e:
                self.failed("Failed to establish connection to '{}'".format(
                    device.name))

            device_list.append(device)

        # Pass list of devices the to testcases
        self.parent.parameters.update(dev=device_list)
Exemple #9
0
    def establish_connections(self, testbed):
        """
        Establishes connections to all devices in testbed
        :param testbed:
        :return:
        """

        genie_testbed = Genie.init(testbed)
        self.parent.parameters['testbed'] = genie_testbed
        for device in genie_testbed.devices.values():
            log.info(banner(f"Connect to device '{device.name}'"))
            try:
                device.connect(log_stdout=False)
            except errors.ConnectionError:
                self.failed(f"Failed to establish "
                            f"connection to '{device.name}'")
Exemple #10
0
 def establish_connections(self, testbed):
     # Load testbed file which is passed as command-line argument
     genie_testbed = Genie.init(testbed)
     self.parent.parameters['testbed'] = genie_testbed
     device_list = []
     # Load all devices from testbed file and try to connect to them
     for device in genie_testbed.devices.values():
         log.info(banner(f"Connect to device '{device.name}'"))
         try:
             device.connect(log_stdout=False)
         except errors.ConnectionError:
             self.failed(f"Failed to establish "
                         f"connection to '{device.name}'")
         device_list.append(device)
     # Pass list of devices to testcases
     self.parent.parameters.update(dev=device_list)
Exemple #11
0
 def connect(self, testbed):
     # Get specified testbed
     genie_testbed = Genie.init(testbed)
     # Save in environment variables
     self.parent.parameters['testbed'] = genie_testbed
     device_list = []
     # Try connect one by one and save device objects in a list
     for device in genie_testbed.devices.values():
         log.info(banner("Connect to device '{d}'".format(d=device.name)))
         try:
             device.connect()
         except Exception as e:
             self.failed("Failed to establish connection to '{}'".format(
                 device.name))
         # Add device to list
         device_list.append(device)
     # Pass list of devices the to testcases
     self.parent.parameters.update(dev=device_list)
    def setup(self, testbed):
        # initalize genie testbed
        testbed = Genie.init(testbed)
        self.parent.parameters['testbed'] = testbed
        # used for tracking neighbor / recovery info
        self.parent.parameters['failed_nbrs'] = list()
        self.parent.parameters['recovery_queue'] = list()

        reason = "being conservative"
        aetest.skipUnless.affix(section=DoubleCheck,
                                condition=False,
                                reason=reason)

        aetest.skipUnless.affix(section=Recovery,
                                condition=False,
                                reason=reason)

        # neighbor check for each device in the testbed using loops
        aetest.loop.mark(CheckForNeighbor, device=testbed.devices)
Exemple #13
0
    def connect(self, testbed):
        genie_testbed = Genie.init(testbed)
        self.parent.parameters['testbed'] = genie_testbed
        device_list = []
        for d in genie_testbed.devices.keys():
            # Mark testcase with looping information
            device = genie_testbed.devices[d]

            log.info(banner("Connect to device '{d}'".format(d=device.name)))
            try:
                device.connect()
                device_list.append(d)

            except Exception as e:
                msg = "Failed to connect to {} will not be checked!"
                log.info(msg.format(device.name))

        # run local_user_check against each device in the list
        aetest.loop.mark(local_user_check, dev_name=device_list)
    def validate_testbed(self, testbed):
        '''
        basic checks of the testbed file
        '''
        # abort/fail the testscript if no testbed was provided
        if not testbed or not testbed.devices:
            self.failed('No testbed was provided to script launch',
                        goto=['exit'])

        # abort/fail the testscript if no matching device was provided
        if 'dist1' not in testbed:
            self.failed('testbed needs to contain device `dist1`',
                        goto=['exit'])

        genie_testbed = Genie.init(testbed)
        dist1 = genie_testbed.devices['dist1']

        # add device to testscript parameters so we can use it later
        self.parent.parameters.update(dist1=dist1)
def main():

    parser = argparse.ArgumentParser(description="standalone parser")
    parser.add_argument('--testbed', dest='testbed', type=loader.load)
    parser.add_argument('--neighbor', help="Expected neighbor RID")

    args, unknown = parser.parse_known_args()
    if not all([args.testbed, args.neighbor]):
        print(banner("Please specify a testbed file and expected neighbor"))

    else:

        # pyats testbed != genie testbed
        testbed = Genie.init(args.testbed)
        nbr = args.neighbor

        uut = testbed.devices['uut']
        uut.connect()

        # Retrieve Ospf Class for this device
        ospf_cls = get_ops('ospf', uut)
        # Instantiate the class, and provides some attributes
        # Attributes limit the # of clis to use;
        # It will only learn the neighbors,  nothing else.
        ospf = ospf_cls(uut,
                        attributes=['info[vrf][(.*)][address_family][ipv4]'
                                    '[instance][{OSPF_PROCESS}]'
                                    '[areas][{OSPF_AREA}]'
                                    '[interfaces][{EXPECTED_INTERFACE}]'
                                    '[neighbors][(.*)]'\
                                            .format(OSPF_PROCESS=OSPF_PROCESS,
                                                    OSPF_AREA=OSPF_AREA,
                                                    EXPECTED_INTERFACE=EXPECTED_INTERFACE)])
        ospf.learn()

        state = check_for_neighbor(ospf, nbr)
        if state:
            print(
                banner("Successfully verified neighbor {} is {}".format(
                    nbr, state)))
        else:
            print(banner("Failed to verify neighbor, now what?"))
    def connect(self, testbed, MAX_POOL_SIZE=31):
        genie_testbed = Genie.init(testbed)
        self.parent.parameters['testbed'] = genie_testbed
        uut = genie_testbed.devices['uut']

        # Create a connection pool
        if hasattr(testbed, 'custom') and hasattr(testbed.custom,
                                                  'MAX_POOL_SIZE'):
            MAX_POOL_SIZE = testbed.custom.MAX_POOL_SIZE

        uut.start_pool(alias='pool', size=MAX_POOL_SIZE)

        # A Connection pool workers need to have genie attributes to be able to
        # call ops
        for worker in uut.pool.workers:
            worker.mapping = {}
            worker.mapping['cli'] = 'vty'
            worker.cli = worker

        # Pass 'uut' to testcases
        self.parent.parameters.update(dev=uut)
Exemple #17
0
    def genie_testbed(self, testbed):
        '''Create the genie testbed'''
        try:
            # If pyATS, then call their use_testbed api, then convert
            self.builtin.get_library_instance('ats.robot.pyATSRobot').\
                         use_testbed(testbed)
            testbed = self.builtin.get_library_instance('ats.robot.'\
                                                        'pyATSRobot').testbed
        except RuntimeError:
            # No pyATS
            self.testbed = loader.load(testbed)
        else:
            # Has pyATS, so converted and then save locally and also for pyATS
            self.testbed = Genie.init(testbed)
            self.builtin.get_library_instance('ats.robot.'\
                                              'pyATSRobot').testbed =\
                                              self.testbed
        self.testscript.parameters['testbed'] = self.testbed

        # Load Genie Datafiles (Trigger, Verification and PTS)
        self._load_genie_datafile()
Exemple #18
0
    def connect(self, testbed, steps):
        genie_testbed = Genie.init(testbed)
        device_list = []
        device_name = []

        for device in genie_testbed.devices.values():
            with steps.start(
                    "Connecting to device '{d}'".format(d=device.name)):
                log.info(
                    banner("Connecting to device '{d}'".format(d=device.name)))
                try:
                    device.connect()
                except Exception as e:
                    self.failed(
                        "Failed to establish connection to '{}'".format(
                            device.name))

                device_list.append(device)
                device_name.append(device.name)
        log.info((device_list))
        aetest.loop.mark(device_commands,
                         devices=device_list,
                         uids=device_name)
Exemple #19
0
def get_details():
    # Load the testbed of devices and credentials
    testbed = Genie.init('testbed.yml')
    global results

    # Using Genie to gather the data from the devices and store in a list of lists.
    for name in testbed.devices:
        testbed.devices[name].connect()
        version = testbed.devices[name].parse("show version")
        # If is NXOS as uses completely different data structure
        if 'platform' in version.keys():
            dev_table = [name, version['platform']['os'], version['platform']['hardware']['chassis'],
                        version['platform']['software']['system_version'], version['platform']['hardware']['processor_board_id']]
        # If is IOSXE as chassis different in IOSXE and IOS data structures
        elif version['version']['os'] == 'IOS-XE':
            dev_table = [name, version['version']['os'], version['version']['chassis'],
                        version['version']['version_short'], version['version']['chassis_sn']]
        # IF IOSv
        elif version['version']['os'] == 'IOSv':
            dev_table = [name, version['version']['os'], version['version']['platform'],
                        version['version']['version_short'], version['version']['chassis_sn']]
        # Creates a list of lists of all devices
        results.append(dev_table)
        testbed.devices[name].disconnect()
Exemple #20
0
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

# Import the Genie library
from genie.conf import Genie

# Create a testbed object
testbed = Genie.init("testbed.yaml")

# Take a look at the devices that are in the testbed
print(testbed.devices)

# Create a "convenience" variable for one device
iosv1 = testbed.devices["iosv-1"]

# Connect to the router
iosv1.connect()

# Check that you are connected
iosv1.connected

# Run the "show interfaces" command and "parse" results to Python object
interfaces = iosv1.parse("show interfaces")
Exemple #21
0
        }
    },
)
build_device = Device(
    os.getenv("SWITCH_HOSTNAME"),
    os="nxos",
    type="nxos",
    connections={
        "default": {
            "protocol": "ssh",
            "ip": os.getenv("SWITCH_MGMT_IP")
        }
    },
)
build_device.testbed = build_testbed
genie_testbed = Genie.init(build_testbed)

# Grab the device and connect
device = genie_testbed.devices[os.getenv("SWITCH_HOSTNAME")]
device.connect(learn_hostname=True, log_stdout=False)


def platform_info():
    return device.learn("platform")


def interfaces_current():
    interfaces = device.learn("interface")

    return interfaces.info
Exemple #22
0
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

# Import Genie
from genie.conf import Genie

# Initialize a Testbed File
testbed = Genie.init("genie-testbed.yaml")

# Create variable for specific testbed device
device = testbed.devices["csr1000v-1"]

# Conenct to the device
device.connect()

# Learn the interfaces using Genie model
interfaces = device.learn("interface")

# Print out interface name
print("Here are the interfaces from device {}".format(device.name))
for interface in interfaces.info:
    print(interface)
Exemple #23
0
import os
from genie.conf import Genie
# import the topology module
from ats.topology import loader

# import the genie libs
from genie.abstract import Lookup
from genie.libs import ops

# load testbed file which describes our devices
pyats_testbed = loader.load('./default_testbed.yaml')

# pyats testbed != genie testbed
genie_testbed = Genie.init(pyats_testbed)
print(genie_testbed.devices)
all_interfaces = dict()

for name, device in genie_testbed.devices.items():
    print("Gathering Interface Information from {}".format(name))
    device.connect()
    abstract = Lookup.from_device(device)
    intf = abstract.ops.interface.interface.Interface(device)
    intf.learn()
    all_interfaces[name] = intf.info

for device, ints in all_interfaces.items():
    for name, props in ints.items():
        counters = props.get('counters')
        if counters:
            print('{}:{} CRC Errors: {}'.format(device, name,
                                                counters['in_crc_errors']))
# from testbed_from_inventory import create_testbed
# import configparser
from genie.abstract import Lookup
from genie.libs import ops  # noqa
from genie.conf import Genie
from prettytable import PrettyTable
import re
import pprint

testbed = Genie.init('tests/testbed.yml')

device = testbed.devices['fspipswi001']

# Conenct to the device
device.connect()
interfaces = device.parse("show interfaces")
switchports = device.parse("show interfaces switchport")

ntp = device.parse("show ntp associations")

interface_list = {}
for intf, v in interfaces.items():
    if 'description' in v:
        decsc = v['description']
    else:
        decsc = None
    if not 'Vlan' in intf:
        intf_obj = {}
        intf_obj[intf] = {
            'description': decsc,
            'access_vlan': switchports[intf]['access_vlan'],
from genie.conf import Genie

print(
    "Running script that applies a configuration change to device 'edge-sw01'."
)
print("The device 'edge-sw01' is defined in the testbed-file.")

# Carrega o testebed com todos os devices
testbed = Genie.init('multi-platform-network.yaml')

# Conecta ao device 'edge-sw01'
device = 'edge-sw01'
testbed.devices[device].connect(log_stdout=False)

# Aplica a mudança de configuração
testbed.devices[device].configure(
    ["interface Loopback799", "description Nova Interface Criada Via Script"])

# Desconecta
testbed.devices[device].disconnect()

print("Changes applied.")
print("We can now compare the current configuration against the previous one.")
Exemple #26
0
    # local imports
    import argparse

    parser = argparse.ArgumentParser(description="standalone parser")
    parser.add_argument('--testbed',
                        default='./default_testbed.yaml',
                        dest='testbed',
                        type=loader.load)
    parser.add_argument('--device', dest='device_name')

    args, unknown = parser.parse_known_args()
    print(args)

    device_name = args.device_name
    # pyats testbed != genie testbed
    genie_testbed = Genie.init(args.testbed)

    # this gives us device_name as Device Object e.g dist1
    vars()[device_name] = genie_testbed.devices[device_name]
    # or we can also just use `device`
    device = vars()[device_name]

    # connect to the device
    device.connect()

    # work with an abstracted device model
    abstract = Lookup.from_device(device)

    # interface info is always a good place to start..
    interfaces = abstract.ops.interface.interface.Interface(device)
    interfaces.learn()
import os
import sys
import pprint
from nornir_jinja2.plugins.tasks import template_file
from genie import testbed
from pyats import topology
from genie.conf import Genie
from pyats.topology import loader

testbedfile = os.path.join('../testbed.yml')
testbed = Genie.init(testbedfile)


def verify_vlan(task):
    device = testbed.devices[f'{task.host}']
    device.connect(log_stdout=False)
    output = device.parse('show vlan')
    available_vlans = output['vlans'].keys()
    configured_vlans = [10, 22, 35, 40, 44]
    result_dict = {}
    for vlan_id in configured_vlans:
        if vlan_id in available_vlans:
            result_dict.update({f"Vlan {vlan_id} present": True})
        else:
            result_dict.update({f"Vlan {vlan_id} present": False})
    if False in result_dict.values():
        raise Exception(["Vlans not properly configured", result_dict])
    else:
        return result_dict
Exemple #28
0
# cmdRouter.csv file must exist in current working directory
# Example of cmdRouter.csv:
#  command/rustphrbtqfb-1.txt,rustphrbtqfb-1
#  command/rustphrbtqfb-2.txt,rustphrbtqfb-2

#Import neccessary libraries/modules
import pickle
import sys
import pprint
from ats.topology import loader
from genie.conf import Genie
import csv

#Define global variables
testbed = loader.load('my_testbed.yaml')              #Load testbed yaml file
genie_testbed = Genie.init(testbed)                   #Init genie testbed
logfile = open('log.txt', 'w+')                       #Open log file

#Define readCommandRouterFile() --- read csv file & return to data structure obj
def readCommandRouterFile(cmdRouterFile):
  with open(cmdRouterFile) as f:
    csvf = csv.reader(f)
    data = list(csvf)
  return data

#Define readCommandFile() --- read command txt file & return to cmd structure obj
def readCommandFile(cmdFile):
  with open(cmdFile) as f:
    cmd = f.read().splitlines()
  return cmd
Exemple #29
0
def main():

    parser = argparse.ArgumentParser(description="standalone parser")
    parser.add_argument('--testbed', dest='testbed', type=loader.load)
    args, unknown = parser.parse_known_args()

    if not args.testbed:
        print("""
    **************************************************************
    You didn't specify a testbed, we'll load up a Devnet sandbox.

    Next time try with your own testbed file by running

    ipyats --testbed <your file>
    **************************************************************
        """)
        path = os.path.abspath(os.path.join(ipyats.__file__, os.pardir))
        testbed = path + '/public_testbeds/devnet_sandbox.yaml'
        testbed = loader.load(testbed)
        testbed = Genie.init(testbed)

    else:

        # pyats testbed != genie testbed
        testbed = Genie.init(args.testbed)

    print("""
    Welcome to ipyATS!

    your testbed is now available as the `testbed` object

    You can start by exploring some of the common operations available for the
    testbed by typing

    dir(testbed)

    To get you started, we've also included some common tasks, give them
    a try and then checkout the code in the `tasks` folder

    dir(tasks)

    You can checkout the source code for any of these funtions by passing
    them to show_source

    e.g show_source(tasks.get_routing_table)
    """)

    # if it's sandbox, these will come in handy
    if testbed.name == "devnet_always_on_sandbox":
        csr = testbed.devices['csr1000v'] # noqa
        nx = testbed.devices['sbx-n9kv-ao'] # noqa
        print("""
    Detected Always-On Sandbox, creating some handy objects

    Created objects `csr` and `nx`")

    csr = testbed.devices['csr1000v']
    nx = testbed.devices['sbx-n9kv-ao']
    """)

    # bail to iPython
    embed()
Exemple #30
0
from genie.conf import Genie

testbed = '/pyats/demos/default_testbed.yaml'
testbed = Genie.init(testbed)

# select a specific device from the testbed definition
csr = testbed.devices['csr1000v-1']

# connect to that device
csr.connect()

# execute a command in that device
csr.execute('show version')