예제 #1
0
def main():

    # Initialize Nornir object using hosts.yaml/groups.yaml/defaults.yaml
    norn = InitNornir(config_file="/home/kbyers/nornir_inventory/config.yaml")
    nornir_set_creds(norn)
    result = norn.run(test_task, num_workers=20)
    std_print(result)
def main():

    # Initialize Nornir object using hosts.yaml and groups.yaml
    norn = InitNornir(config_file="nornir.yml")
    nornir_set_creds(norn)

    print("Transferring files")
    result = norn.run(
        task=os_upgrade,
        num_workers=20,
    )
    std_print(result)

    # Filter to only a single device
    norn_ios = norn.filter(hostname="cisco1.twb-tech.com")

    aggr_result = norn_ios.run(task=set_boot_var)

    # If setting the boot variable failed (assumes single device at this point)
    for hostname, val in aggr_result.items():
        if val[0].result is False:
            sys.exit("Setting the boot variable failed")

    # Verify the boot variable
    result = norn_ios.run(
        netmiko_send_command,
        command_string="show run | section boot",
        num_workers=20,
    )
    std_print(result)
    continue_func()
예제 #3
0
def main():
    # Initialize Nornir object using default "SimpleInventory" plugin
    nr = InitNornir()
    nornir_set_creds(nr)
    result = nr.run(
        task=os_upgrade,
        num_workers=20,
    )
    std_print(result)

    # Filter to only a single device
    nr_ios = nr.filter(hostname="cisco1.domain.com")

    aggr_result = nr_ios.run(task=set_boot_var)

    # If setting the boot variable failed (assumes single device at this point)
    for hostname, val in aggr_result.items():
        if val[0].result is False:
            sys.exit("Setting the boot variable failed")

    # Verify the boot variable
    result = nr_ios.run(
        task=netmiko_send_command,
        command_string="show run | section boot",
        num_workers=20,
    )
    std_print(result)
    continue_func()
예제 #4
0
def main():
    # Initialize Nornir object using default "SimpleInventory" plugin
    nr = InitNornir()
    nornir_set_creds(nr)
    result = nr.run(
        task=os_upgrade,
        num_workers=20,
    )
    std_print(result)

    # Filter to only a single device
    nr_ios = nr.filter(hostname="cisco1.domain.com")

    aggr_result = nr_ios.run(task=set_boot_var)

    # If setting the boot variable failed (assumes single device at this point)
    for hostname, val in aggr_result.items():
        if val[0].result is False:
            sys.exit("Setting the boot variable failed")

    # Verify the boot variable
    result = nr_ios.run(
        task=netmiko_send_command,
        command_string="show run | section boot",
        num_workers=20,
    )
    std_print(result)
    continue_func()
def main():

    # Initialize Nornir object using hosts.yaml and groups.yaml
    norn = InitNornir(config_file="nornir.yml")
    nornir_set_creds(norn)

    print("Transferring files")
    result = norn.run(
        task=os_upgrade,
        num_workers=20,
    )
    std_print(result)

    # Filter to only a single device
    norn_ios = norn.filter(hostname="cisco1.twb-tech.com")

    aggr_result = norn_ios.run(task=set_boot_var)

    # If setting the boot variable failed (assumes single device at this point)
    for hostname, val in aggr_result.items():
        if val[0].result is False:
            sys.exit("Setting the boot variable failed")

    # Verify the boot variable
    result = norn_ios.run(
        netmiko_send_command,
        command_string="show run | section boot",
        num_workers=20,
    )
    std_print(result)
    continue_func()

    # Save the config
    result = norn_ios.run(
        netmiko_send_command,
        command_string="write mem",
    )
    std_print(result)

    # Reload
    continue_func(msg="Do you want to reload the device (y/n)? ")
    result = norn_ios.run(
        netmiko_send_command,
        use_timing=True,
        command_string="reload",
    )

    # Confirm the reload (if 'confirm' is in the output)
    for device_name, multi_result in result.items():
        if 'confirm' in multi_result[0].result:
            result = norn_ios.run(
                netmiko_send_command,
                use_timing=True,
                command_string="y",
            )

    print("Devices reloaded")
예제 #6
0
def main():
    nr = InitNornir(config_file="config.yaml")
    nr = nr.filter(F(groups__contains="iosv"))
    print(
        'Running iosstaging.py against the following Nornir inventory hosts:',
        nr.inventory.hosts.keys())
    nornir_set_creds(nr)
    print("Starting Image Transfer")
    result = nr.run(task=os_staging)
    print_result(result)
예제 #7
0
def main():
    # Initialize Nornir object using default "SimpleInventory" plugin
    nr = InitNornir()
    nornir_set_creds(nr)
    result = nr.run(
        task=os_upgrade,
        num_workers=20,
    )
    std_print(result)

    # Filter to only a single device
    nr_ios = nr.filter(hostname="cisco1.domain.com")

    aggr_result = nr_ios.run(task=set_boot_var)

    # If setting the boot variable failed (assumes single device at this point)
    for hostname, val in aggr_result.items():
        if val[0].result is False:
            sys.exit("Setting the boot variable failed")

    # Verify the boot variable
    result = nr_ios.run(
        task=netmiko_send_command,
        command_string="show run | section boot",
        num_workers=20,
    )
    std_print(result)
    continue_func()

    # Save the config
    result = nr_ios.run(
        task=netmiko_send_command,
        command_string="write mem",
    )
    std_print(result)

    # Reload
    continue_func(msg="Do you want to reload the device (y/n)? ")
    result = nr_ios.run(
        task=netmiko_send_command,
        use_timing=True,
        command_string="reload",
    )

    # Confirm the reload (if 'confirm' is in the output)
    for device_name, multi_result in result.items():
        if 'confirm' in multi_result[0].result:
            result = nr_ios.run(
                task=netmiko_send_command,
                use_timing=True,
                command_string="y",
            )

    print("Devices reloaded")
예제 #8
0
def main():
    nr = InitNornir(config_file="config.yaml")
    #Filter devices to run against
    nr = nr.filter(F(groups__contains="iosv"))
    print('Running postvalidaiton.py against the following Nornir inventory hosts:', nr.inventory.hosts.keys())
    # Ask for credentials at runtime instead of storing.
    nornir_set_creds(nr)

    print("Collecting running configurations and operational values\n")
    resultconf = nr.run(task=collect_configs)
    resultgetters = nr.run(task=collect_getters)
    #import ipdb; ipdb.set_trace()

    #Loop through napalm getters and output current running version.
    prYellow('Current IOS Running Versions:')
    for host in resultgetters:
        print(host, '>>', resultgetters[host][1].result['facts']['os_version'])

    #Perform a Diff between the pre and post nornir getter files we saved.
    for host in nr.inventory.hosts:
        #dont try to open files or compare if a host failed collection
        if host in resultconf.failed_hosts or host in resultgetters.failed_hosts:
            print('!', host, 'failed collection and Op State will not be compared\n')
            #TODO: log netmiko/nornir error to file. otherwise it should exist in nornir.log.
            continue
        else:
            #load facts in hosts pre and post folder and store to var. since were not using pyats native learn objects we must loop through files
            prGreen("vvv --- " + host + " --- Begin Comparison between Pre Upgrade and Post Upgrade operational values vvv")
            for filename in os.listdir(initial_facts_dir+host):
                with open(initial_facts_dir+host+'/'+filename, 'r') as f:
                    initialstate = json.load(f)
                with open(facts_dir+host+'/'+filename, 'r') as f:
                    poststate = json.load(f)
                compare = Diff(initialstate, poststate)
                compare.findDiff()
                print('#', filename, '#\n', compare)
            prGreen("^^^ --- " + host + " --- End Comparison between Pre Upgrade and Post Upgrade operational values ^^^\n")


            prGreen("vvv --- " + host + " --- Begin Comparison between Pre Upgrade and Post Upgrade configurations vvv")
            with open(initial_config_dir+host+'-running.cfg', 'r') as f:
                cfg = f.read()
            initialconfig = Config(cfg)
            initialconfig.tree()
            with open(config_dir+host+'-running.cfg', 'r') as f:
                cfg = f.read()
            postconfig = Config(cfg)
            postconfig.tree()
            compare = Diff(initialconfig, postconfig)
            compare.findDiff()
            prCyan("# " + os.path.basename(f.name) + " #")
            print(compare)
            #ipdb.set_trace()
            prGreen("^^^ --- " + host + " --- End Comparison between Pre Upgrade and Post Upgrade configurations ^^^\n")
예제 #9
0
def main():
    # Initialize Nornir object using default "SimpleInventory" plugin
    nr = InitNornir()
    nornir_set_creds(nr)
    result = nr.run(
        task=os_upgrade,
        num_workers=20,
    )
    std_print(result)

    # Filter to only a single device
    nr_ios = nr.filter(hostname="cisco1.domain.com")

    aggr_result = nr_ios.run(task=set_boot_var)

    # If setting the boot variable failed (assumes single device at this point)
    for hostname, val in aggr_result.items():
        if val[0].result is False:
            sys.exit("Setting the boot variable failed")

    # Verify the boot variable
    result = nr_ios.run(
        task=netmiko_send_command,
        command_string="show run | section boot",
        num_workers=20,
    )
    std_print(result)
    continue_func()

    # Save the config
    result = nr_ios.run(
        task=netmiko_send_command,
        command_string="write mem",
    )
    std_print(result)

    # Reload
    continue_func(msg="Do you want to reload the device (y/n)? ")
    result = nr_ios.run(
        task=netmiko_send_command,
        use_timing=True,
        command_string="reload",
    )

    # Confirm the reload (if 'confirm' is in the output)
    for device_name, multi_result in result.items():
        if 'confirm' in multi_result[0].result:
            result = nr_ios.run(
                task=netmiko_send_command,
                use_timing=True,
                command_string="y",
            )

    print("Devices reloaded")
예제 #10
0
def main():

    # Initialize Nornir object using hosts.yaml/groups.yaml/defaults.yaml
    norn = InitNornir(config_file="nornir.yaml")
    nornir_set_creds(norn)
    result = norn.run(
        netmiko_send_command,
        num_workers=20,
        command_string="show ip arp",
        # use_textfsm=True,
    )
    std_print(result)
예제 #11
0
def main():

    # Initialize Nornir object using hosts.yaml and groups.yaml
    brg = InitNornir(config_file="nornir.yml")
    nornir_set_creds(brg)

    print("Transferring files")
    result = brg.run(
        task=os_upgrade,
        num_workers=20,
    )
    std_print(result)

    # Filter to only a single device
    brg_ios = brg.filter(hostname="cisco1.twb-tech.com")

    # Verify the boot variable
    result = brg_ios.run(
        netmiko_send_command,
        command_string="show run | section boot",
        num_workers=20,
    )
    std_print(result)
    continue_func()

    # Save the config
    result = brg_ios.run(
        netmiko_send_command,
        command_string="write mem",
        num_workers=20,
    )
    std_print(result)

    # Reload
    continue_func(msg="Do you want to reload the device (y/n)? ")
    result = brg_ios.run(
        netmiko_send_command,
        use_timing=True,
        command_string="reload",
        num_workers=1,
    )

    # Confirm the reload (if 'confirm' is in the output)
    for device_name, multi_result in result.items():
        if 'confirm' in multi_result[0].result:
            result = brg_ios.run(
                netmiko_send_command,
                use_timing=True,
                command_string="y",
            )

    print("Devices reloaded")
예제 #12
0
def preval():
    """
        Prevalidate storage requirements and store baselines for running config and
        operational states prior to OS Staging and Upgrade

        Tasks:
        1) Validate sufficient disk space based on file defined in groups.yaml
        2) Collect and store running configurations to local machine.
        3) Collect and store napalm getters to local machine.
    """

    print(
        'Running prevalidation against the following Nornir inventory hosts:',
        nr.inventory.hosts.keys())
    # Ask for credentials at runtime instead of storing.
    nornir_set_creds(nr)

    #Connect to devices to check if there is sufficient disk space.
    storage_result = nr.run(task=validate_storage,
                            name='Validate Storage Requirements')
    print_result(
        storage_result
    )  #potentially print only True/False Result, logfile rest of output
    #Summarize Results of Storage validation
    print("vvv Storage Results Summary vvv")
    for host in storage_result:
        if storage_result[host][0].result == True:
            print("Success - Sufficient storage available on", host)
        elif storage_result[host][0].result == False:
            print("Error - There is not enough space to transfer the image on",
                  host)
        else:
            print("Error - An Error occured with", host)
    print("^^^ Storage Results Summary ^^^")
    #Connect to devices and store their running configurations to local folders.

    print(
        "\nCollecting running configurations and operational state from devices"
    )
    result = nr.run(task=collect_configs, directory=preconfig_dir)

    #Connect to devices and collect napalm getters, store to local folders.
    result = nr.run(task=collect_getters, directory=prefacts_dir)

    print(
        "Running configurations and operational state have been saved to local machine"
    )
예제 #13
0
def stage_firmware():
    """
        Transfer/Stage IOS image for router upgrade and validate MD5. Bootvars not changed.

        Tasks:
        1) Transfer image defined in groups.yaml from client to router using netmiko
        file transfer. Idempotent transfer.
        2) Check File exists and MD5 Hash on completion. if file exists valueerror.

        Requires user with privelge level 15 (without enable) and 'ip scp server enable'
    """

    print('Staging firmware against the following Nornir inventory hosts:',
          nr.inventory.hosts.keys())
    nornir_set_creds(nr)
    print("Starting Image Transfer")
    result = nr.run(task=os_staging)
    print_result(result)
예제 #14
0
def main():
    nr = InitNornir(config_file="config.yaml")
    #Filter devices to run against
    nr = nr.filter(F(groups__contains="iosv"))
    print(
        'Running prevalidation.py against the following Nornir inventory hosts:',
        nr.inventory.hosts.keys())

    # Ask for credentials at runtime instead of storing.
    nornir_set_creds(nr)

    #Connect to devices to check if there is sufficient disk space.
    storage_result = nr.run(task=validate_storage,
                            name='Validate Storage Requirements')
    print_result(
        storage_result
    )  #potentially print only True/False Result, logfile rest of output
    #Summarize Results of Storage validation
    print("vvv Storage Results Summary vvv")
    for host in storage_result:
        if storage_result[host][0].result == True:
            print("Success - Sufficient storage available on", host)
        elif storage_result[host][0].result == False:
            print("Error - There is not enough space to transfer the image on",
                  host)
        else:
            print("Error - An Error occured with", host)
    print("^^^ Storage Results Summary ^^^")
    #Connect to devices and store their running configurations to local folders.

    print(
        "\nCollecting running configurations and operational state from devices"
    )
    result = nr.run(task=collect_configs)
    #std_print(result)
    #Connect to devices and collect napalm getters, store to local folders.
    result = nr.run(task=collect_getters)
    #print_result(result)
    print(
        "Running configurations and operational state have been saved to local machine"
    )
예제 #15
0
from nornir.core import InitNornir
from nornir.plugins.tasks.networking import netmiko_file_transfer

from nornir_utilities import nornir_set_creds, std_print

# Initialize Nornir object using hosts.yaml and groups.yaml
norn = InitNornir(config_file="nornir.yml")
nornir_set_creds(norn)
test_file = 'test_file4.txt'

result = norn.run(
    task=netmiko_file_transfer,
    source_file=test_file,
    dest_file=test_file,
    direction='put',
    num_workers=20,
)
std_print(result)
예제 #16
0
def main():
    nr = InitNornir(config_file="config.yaml")
    #filter to one device
    #target_routers = nr.filter(hostname='10.83.46.1')
    target_routers = nr.filter(F(groups__contains="iosv"))
    print(
        'Running iosupgrade.py against the following Nornir inventory hosts:',
        target_routers.inventory.hosts.keys())

    # Ask for credentials at runtime instead of storing.
    nornir_set_creds(nr)

    aggr_result = target_routers.run(task=set_boot_image, num_workers=20)
    print_result(aggr_result)

    #Check result for set_boot_image. True or False.
    for hostname, val in aggr_result.items():
        if val[0].result is False:
            #sys.exit("Setting the boot variable failed for device:", hostname)
            prRed("\n!!!There was an error with " + hostname +
                  " so it will be removed from further processing\n")
            #Add device to failed inventory to prevent future processing in tasks... (poor design)
            target_routers.data.failed_hosts.add(hostname)
    #Post validation of boot statements
    result = target_routers.run(task=netmiko_send_command,
                                command_string="show run | section boot",
                                name="show run | section boot",
                                num_workers=20)
    prYellow(
        "\nReview the configured boot statements before proceeding with Write Mem\n"
    )
    print_result(result)
    #After reviewing boot statements - ask to continue
    continue_func()

    # Save the configuration
    result = target_routers.run(
        task=netmiko_send_command,
        command_string="write mem",
        name="Issue Write Mem",
    )
    print_result(result)

    # Reload
    continue_func(msg="Do you want to reload the device (y/n)? ")
    result = target_routers.run(
        task=netmiko_send_command,
        use_timing=True,
        command_string="reload",
        name="Issue Reload Command",
    )
    #import ipdb; ipdb.set_trace()
    # Handle reload confirmation if required
    for device_name, multi_result in result.items():
        if 'confirm' in multi_result[0].result:
            result = target_routers.run(
                task=netmiko_send_command,
                use_timing=True,
                command_string="y",
                name="Confirm Reload with Yes",
            )
    print_result(result)
    print("Devices reloaded")
예제 #17
0
from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_file_transfer

from nornir_utilities import nornir_set_creds, std_print


# Initialize Nornir object using default "SimpleInventory" plugin 
nr = InitNornir()
nornir_set_creds(nr)
test_file = 'test_file4.txt'

result = nr.run(
    task=netmiko_file_transfer,
    source_file=test_file,
    dest_file=test_file,
    direction='put',
    num_workers=20,
)
std_print(result)
예제 #18
0
from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_file_transfer

from nornir_utilities import nornir_set_creds, std_print

# Initialize Nornir object using default "SimpleInventory" plugin
nr = InitNornir()
nornir_set_creds(nr)
test_file = 'test_xfer.txt'

result = nr.run(
    task=netmiko_file_transfer,
    source_file=test_file,
    dest_file=test_file,
    direction='put',
    num_workers=20,
)
std_print(result)
예제 #19
0
    exit()

### insert data into hosts yaml file
host_file = open('hosts.yaml', 'w')
host_file.write(yaml_host)
host_file.close()
############################end extracting data => yaml file ##################

print("############################################")
print("########         Runing...         #########")
print("############################################")

#############################   Main program  ##################################
nr = InitNornir()  # Initialize Nornir object using hosts.yaml and groups.yaml

nornir_set_creds(nr, sys.argv[1], sys.argv[2])  # user & password function
run_cmd1 = nr.run(task=sh_vrrp_brief, num_workers=10)  # run sh vrrp brief
run_cmd2 = nr.run(task=sh_int_loopback, num_workers=10)  # run sh int loopback
run_cmd3 = nr.run(task=sh_policy_map, num_workers=10)  # sh policy-map
run_cmd4 = nr.run(task=sh_ip_route_static,
                  num_workers=10)  # sh ip route static
run_cmd5 = nr.run(task=sh_ip_wan, num_workers=10)  # sh ip wan

copyfile('input-output/output_files/output.csv',
         'tmp/copyoutput.csv')  # copy of output.csv

with open('tmp/copyoutput.csv', newline='') as tmpfile:  #read input file
    reader = csv.reader(tmpfile)
    next(reader)  # skip_lan1 header
    for line in reader:
        name_file = line[1]
예제 #20
0
def upgrade_ios():
    """
        Update bootvars and perform IOS Upgrade.

        Tasks:
        1) Check if current image is defined in bootvars so we can set as backup.
        2) Validate that both new and existing/backup image are on device.
        3) Set boot vars so new image is primary image, previous is backup.
        4) Output new Boot Config and Prompt for user review before continuuing
        5) Write Mem (Confirm)
        6) Reload Router (Confirm)

    """

    print(
        'Running iosupgrade.py against the following Nornir inventory hosts:',
        nr.inventory.hosts.keys())

    # Ask for credentials at runtime instead of storing.
    nornir_set_creds(nr)

    aggr_result = nr.run(task=set_boot_image, num_workers=20)
    print_result(aggr_result)

    #Check result for set_boot_image. True or False.
    for hostname, val in aggr_result.items():
        if val[0].result is False:
            #sys.exit("Setting the boot variable failed for device:", hostname)
            prRed("\n!!!There was an error with " + hostname +
                  " so it will be removed from further processing\n")
            #Add device to failed inventory to prevent future processing in tasks... (poor design)
            nr.data.failed_hosts.add(hostname)
    #Post validation of boot statements
    result = nr.run(task=netmiko_send_command,
                    command_string="show run | section boot",
                    name="show run | section boot",
                    num_workers=20)
    prYellow(
        "\nReview the configured boot statements before proceeding with Write Mem\n"
    )
    print_result(result)
    #After reviewing boot statements - ask to continue
    continue_func()

    # Save the configuration
    result = nr.run(
        task=netmiko_send_command,
        command_string="write mem",
        name="Issue Write Mem",
    )
    print_result(result)

    # Reload
    continue_func(msg="Do you want to reload the device (y/n)? ")
    result = nr.run(
        task=netmiko_send_command,
        use_timing=True,
        command_string="reload",
        name="Issue Reload Command",
    )
    #import ipdb; ipdb.set_trace()
    # Handle reload confirmation if required
    for device_name, multi_result in result.items():
        if 'confirm' in multi_result[0].result:
            result = nr.run(
                task=netmiko_send_command,
                use_timing=True,
                command_string="y",
                name="Confirm Reload with Yes",
            )
    print_result(result)
    print("Devices reloaded")