Ejemplo n.º 1
0
    def staging_process(self):

        print('starting get running config job')
        self._attributes = self.get_job_details()
        self.device = self._attributes['device']
        self.log("Updated job details: {}".format(self._attributes))
        self.status = "CONNECTING"
        self._pyntc = None

        try:
            self._pyntc = NTC(host=self.device,
                              username=self.username,
                              password=self.password,
                              device_type="cisco_ios_ssh")
            device = self._pyntc

        except Exception:
            self.status = "FAILED CONNECT"

        self.status = "Getting Configuration"

        output = device.native.send_command('show running-config')
        self.running_config_url = self.logbin(output)
        if len(output) > 0:
            self.get_running_config_status = "success"
            self.status = "SUCCESS"
        else:
            self.get_running_config_status = "warn"
            self.status = "WARNING"
        # print messages are displayed in worker console
        print('staging thread for {} exiting...'.format(self.device))
Ejemplo n.º 2
0
    def staging_process(self):
        print('starting staging job')
        self._attributes = self.get_job_details()
        self.device = self._attributes['device']
        self.register_custom_tasks()
        self.log("Updated job details: {}".format(self._attributes))
        self.status = "CONNECTING"
        self._pyntc = None

        try:
            self._pyntc = NTC(host=self.device,
                              username=self.username,
                              password=self.password,
                              device_type="cisco_ios_ssh")
            device = self._pyntc
            hostname = self._pyntc.facts['hostname']

        except Exception:
            self.status = "FAILED CONNECT"
        try:
            regions = requests.get(self.regions_url,
                                   headers=self.request_headers).json()
            regional_fs = regions[hostname[:2].upper()]['regional_fs']
            print("Using server {}".format(regional_fs))
        except KeyError:
            print("Unable to determine regional server")

        self.status = "IDENTIFY PLATFORM"
        sup_type, sup_output = self.identify_platform()

        print("Supervisor identified as {}".format(sup_type))

        self.status = "LOCATING IMAGE"
        images = requests.get(self.images_url,
                              headers=self.request_headers).json()
        image = images[sup_type]['filename']

        print("Using image {}".format(image))
        self.target_image = image

        self.status = "TRANSFERRING IOS"
        print("Initatiating file transfer...")
        url = "tftp://{}/{}".format(regional_fs, image)
        ios_transfer, ios_transfer_output = generic.copy_remote_image(
            device, url)
        logbin_url = self.logbin(ios_transfer_output,
                                 description="file transfer log for {}".format(
                                     self.device))
        self.code_upload_log_url = logbin_url
        if ios_transfer:
            print('File Transfer Suceeded')
            self.code_upload_status = "success"
        else:
            print('File Transfer Failed')
            self.code_upload_status = "danger"
            exit()

        self.status = "CODE STAGING SUCCESSFUL"

        print('staging thread for {} exiting...'.format(self.device))
Ejemplo n.º 3
0
    def custom_verification_1(self):
        print "verify fpga upgrades"
        self.custom_verification_1_name = "Verify 4748 FPGA upgrades"
        output = ""
        try:
            device = NTC(host=self.device,
                         username=self.username,
                         password=self.password,
                         device_type="cisco_ios_ssh")

            hostname = device.facts['hostname']

        except Exception:
            self.status = "FAILED CONNECT"

        print("Verify FPGA")
        self.status = "VERIFYING FPGA UPGRADE"
        fpga_status, fpga_output = self._verify_fpga(device)
        logbin_url = self.logbin(
            fpga_output,
            description="fpga upgrade verification for {}".format(self.device))
        self.custom_verification_1_status_log_url = logbin_url

        if fpga_status:
            self.custom_verification_1_status = "success"
            return True

        else:
            self.custom_verification_1_status = "danger"
            return False
Ejemplo n.º 4
0
def createSwitch(hostname, username, password, Switchtype):
    #types:
    #IOS: cisco_ios_ssh
    #NEXUS: cisco_nxos_nxapi
    tmpSwitch = NTC(host=hostname,
                    username=username,
                    password=password,
                    device_type=Switchtype)
    switchList.append(
        tmpSwitch.facts['hostname']
    )  #adds the hostname of the switch to our master list of hostnames
    return tmpSwitch
Ejemplo n.º 5
0
    def _ensure_queues_are_enabled(self):
        output = ""
        try:
            device = NTC(host=self.device,
                         username=self.username,
                         password=self.password,
                         device_type="cisco_ios_ssh")

            hostname = device.facts['hostname']

        except Exception:
            self.status = "FAILED CONNECT"

        output = device.show('show module')

        output += "Idenfitied chassis as "

        chassis_type = device.show('sho mod | inc Chassis').strip().split(
            ':')[1]
        output += chassis_type

        if '4507' in chassis_type:
            sup_ints = ['t3/1', 't3/2', 't4/1', 't4/2']
        elif '4510' in chassis_type:
            sup_ints = ['t5/1', 't5/2', 't5/1', 't5/2']
        else:
            sup_ints = []

        for int in sup_ints:
            output += "\n\nChecking Interface {}\n\n".format(int)
            output += "=" * 20 + "\n"
            int_output = device.show(
                'show platform hardware interface {} tx-queue'.format(int))
            output += int_output
            if 'Disabled' in int_output:
                output += "Bouncing {}\n".format(int)
                shut_noshut = [
                    'interface {}'.format(int), 'shutdown', 'no shutdown'
                ]
                bounce = device.native.send_config_set(shut_noshut)
                output += bounce

            else:
                output += "{} queues are okay".format(int)

        return True, output
# Author: Anubhavsingh Sawdagur
# Created Date: 12 Febuary 2019

import os
import json
from pyntc import ntc_device as NTC

IP_LIST = [
    "192.168.44.110", "192.168.44.111", "192.168.44.121", "192.168.44.122"
]
# IP_LIST = ["192.168.44.110"]

for IP in IP_LIST:

    DEVICE = NTC(host=str(IP),
                 username='******',
                 password='******',
                 device_type='cisco_ios_ssh')
    DEVICE.open()

    print("Connected to " + str(IP))

    # ------------------------------------
    print("Facts - " + str(IP))
    OUTPUT = DEVICE.facts
    OUTPUT = json.dumps(OUTPUT, indent=4)
    print(OUTPUT)

    # ------------------------------------
    print("Running Config - " + str(IP))
    OUTPUT = DEVICE.running_config
    print(OUTPUT)
Ejemplo n.º 7
0
else:
    print('No changes required.')
    iosv_l2.discard_config()


########################################################################################################################
### pyntc
########################################################################################################################

pip install pyntc

##
import json
from pyntc import ntc_device as NTC

iosv_l2 = NTC(host='192.168.1.1', username='******', password='******',device_type='cisco_ios_ssh')
iosv_l2.open()
ios_output = iosvl2.facts
print(json.dumps(ios_output, ident=4))
iosvl2.config_list(['hostname cisco_test'])
ios_output = iosvl2.running_config

# бэкап
ios_output = iosvl2.backup_running_config






Ejemplo n.º 8
0
import json
from pyntc import ntc_device as NTC
site2 = NTC(host='102.1.1.100', username='******', password='******', device_type='cisco_ios_ssh')
site2.open()
site2_output = site2.facts
print (json.dumps(site2_output, indent=4))
site2.config_list(['crypto isakmp policy 1',
                   'authentication pre-share',
                   'encryption aes',
                   'hash sha',
                   'group 5',
                   'lifetime 1800',
                   'exit',
                   'crypto isakmp key shiva address 101.1.1.100',
                   'crypto ipsec transform-set t-set esp-aes esp-sha-hmac',
                   'mode tunnel',
                   'exit',
                   'crypto ipsec security-association lifetime seconds 1800',
                   'access-list 101 permit ip 192.168.102.0 0.0.0.255 192.168.122.0 0.0.0.255', 
                   'crypto map test 10 ipsec-isakmp',
                   'set transform-set t-set',
                   'set peer 101.1.1.100',
                   'match address 101',
                   'int g0/0',
                   'crypto map test',
                   'ip access-list extended natacl',
                   '1 deny ip 192.168.102.0 0.0.0.255 192.168.122.0 0.0.0.255',
                   'exit'])
site2.close()
Ejemplo n.º 9
0
    
    IPs = raw_input("Please Enter name of the file that contains IP addresses : ")
    
    with open(IPs) as f:
        myIPs = f.read().splitlines()
    
    for IP in myIPs:
        print "Establishing Connection to . .  " + IP
        ip = IP
        ios_device = {
            "device_type": "cisco_ios",
            "ip": ip,
            "username": username,
            "password": password
        }
        ios_device = NTC(host=ip, username=username, password=password,device_type="cisco_ios_ssh")
        try:
            ios_device.backup_running_config("Device_IP_"+ ip +".cfg")
                
        except (AuthenticationException):
            print "Authentication failed for:  " + ip
            continue
        
        except (NetMikoTimeoutException):
            print "Time out to device: " + ip
            continue

        except (EOFError):
            print "End of file while attempting device: " + ip
            continue
        
Ejemplo n.º 10
0
    def staging_process(self):
        print('starting staging job')
        self._attributes = self.get_job_details()
        self.device = self._attributes['device']
        self.register_custom_tasks()
        self.log("Updated job details: {}".format(self._attributes))
        self.status = "CONNECTING"
        self._pyntc = None

        try:
            self._pyntc = NTC(host=self.device,
                              username=self.username,
                              password=self.password,
                              device_type="cisco_ios_ssh")
            device = self._pyntc
            hostname = self._pyntc.facts['hostname']

        except Exception:
            self.status = "FAILED CONNECT"
        try:
            regions = requests.get(self.regions_url,
                                   headers=self.request_headers).json()
            regional_fs = regions[hostname[:2].upper()]['regional_fs']
            print("Using server {}".format(regional_fs))
        except KeyError:
            print("Unable to determine regional server")

        self.status = "IDENTIFY PLATFORM"
        sup_type, sup_output = self.identify_platform()

        print("Supervisor identified as {}".format(sup_type))

        self.status = "LOCATING IMAGE"
        images = requests.get(self.images_url,
                              headers=self.request_headers).json()
        image = images[sup_type]['filename']
        print("Using image {}".format(image))
        self.target_image = image

        self.status = "TRANSFERRING"
        print("Initatiating file transfer...")
        url = "tftp://{}/{}".format(regional_fs, image)
        transfer, transfer_output = generic.copy_remote_image(device, url)
        logbin_url = self.logbin(transfer_output,
                                 description="file transfer log for {}".format(
                                     self.device))
        self.code_upload_log_url = logbin_url
        if transfer:
            print('File Transfer Suceeded')
            self.code_upload_status = "success"
        else:
            print('File Transfer Failed')
            self.code_upload_status = "danger"
            exit()

        # determine whether there is a sup redundancy
        self.status = "VERIFY_SUP_REDUNDANCY"
        result = generic.verify_sup_redundancy(device)
        sup_redundancy, sup_redundancy_output = result
        self.sup_redundancy_log_url = self.logbin(
            sup_redundancy_output,
            description="sup redundancy verification for {}".format(
                self.device))
        if sup_redundancy:
            print('Redundant Supervisors detected\n')
            self.sup_redundancy_status = "success"

            self.status = "SYNCHRONIZING IMAGE"
            result = generic.copy_image_to_slave(device, image)
            slave_copy, slave_copy_output = result
            self.copy_code_to_slave_log_url = self.logbin(
                slave_copy_output,
                description="file transfer log for {}".format(self.device))
            if slave_copy:
                print('File Transfer Suceeded')
                self.copy_code_to_slave_status = "success"
            else:
                self.copy_code_to_slave_status = "danger"
        else:
            print('Sups not redundant')
            self.copy_code_to_slave_status = "warning"

        self.status = "CODE STAGING SUCCESSFUL"

        print('staging thread for {} exiting...'.format(self.device))
Ejemplo n.º 11
0
import json
from pyntc import ntc_device as NTC
iosvl2 = NTC(host='10.0.0.2', username='******', password='******', device_type='cisco_ios_ssh')
iosvl2.open()

ios_run = iosvl2.backup_running_config('S1.cfg')

iosvl2.close()

Ejemplo n.º 12
0
    def upgrade_process(self):
        print('starting staging job')
        self._attributes = self.get_job_details()
        self.device = self._attributes['device']
        reloaded = False

        # Connect to device
        try:
            connected = NTC(host=self.device,
                            username=self.username,
                            password=self.password,
                            device_type="cisco_ios_ssh")
            device = connected
        except NetMikoTimeoutException:
            connected = None

        # Proceed with upgrade
        self.status = "CONNECTING"
        if connected:
            hostname = connected.facts['hostname']
        else:
            hostname = None


        # Verify upgrade
        self.status = "VERIFYING UPGRADE"
        online = NTC(host=self.device,
                     username=self.username,
                     password=self.password,
                     device_type="cisco_ios_ssh")


        # Capture post verification commands
        if self.verification_commands:
            post_output = generic.capture_commands(online, self.verification_commands)
            if post_output:
                self.post_verification_commands_status = "success"
                descr = "post upgrade verification commands for {}".format(self.device)
                self.post_verification_commands_url = self.logbin(post_output,
                                                                  description=descr)
            else:
                self.status = "FAILED - COULD NOT GATHER POST VERIFICATION COMMANDS"
                exit(1)

        self.status = "Getting Configuration"

        output = device.native.send_command('show running-config')
        self.post_running_config_url = self.logbin(output)
        if len(output) > 0:
            self.get_post_running_config_status = "success"
            self.status = "SUCCESS"
        else:
            self.get_post_running_config_status = "warn"
            self.status = "WARNING"


        #Get Version
        self.status = "Getting Version"
        output = device.native.send_command('show ver | i Cisco | i Version')
        self.post_version_url = self.logbin(output)
        if binary_diff(self.pre_version_url, self.post_version_url) == True:
            self.get_post_version_status = "success"
            self.status = "SUCCESS"
        else:
            self.get_post_version_status = "danger"
            self.status = "DANGER"


        #Validate Bootvar
        self.status = "Getting Bootvar"
        output = device.native.send_command('show bootvar | i BOOT variable')
        self.post_bootvar_url = self.logbin(output)
        if binary_diff(self.pre_bootvar_url, self.post_bootvar_url) == True:
            self.post_bootvar_status = "success"
            self.status = "SUCCESS"
        else:
            self.post_bootvar_status = "danger"
            self.status = "DANGER"



        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
        for key, value in self._attributes.iteritems():
            if "status" in key:
                if value.lower() == 'success' or value.lower() == 'default' or value.lower() == 'info':
                    print("Success")
                    print(key, value)
                    self.status = "{} Post Upgrade SNAPSHOT Complete".format(hostname)
                    self.status_light = "success"
                else:
                    print("Not")
                    print(key,value)
                    self.status = "{} Post Upgrade SNAPSHOT Complete - WARNING".format(hostname)
                    self.status_light = "danger"
                    break
            print("\n")
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")

        # if all([online]):
        #     self.status = "{} Post Upgrade Snapshot Successful".format(hostname)
        #     print("Post Upgrade Snapshot Successful")
        # else:
        #     self.status = "Post Upgrade Snapshot Failed"
        #     print("Post Upgrade Snapshot Failed")

        end = datetime.datetime.now()
Ejemplo n.º 13
0
    def staging_process(self):
        print('starting snapshot job')
        self._attributes = self.get_job_details()
        self.device = self._attributes['device']
        self.register_custom_tasks()
        self.log("Updated job details: {}".format(self._attributes))
        self.status = "CONNECTING"
        self._pyntc = None
        self.status_light = "default"


        try:
            self._pyntc = NTC(host=self.device,
                              username=self.username,
                              password=self.password,
                              device_type="cisco_ios_ssh")
            device = self._pyntc
            connected = device
            hostname = connected.facts['hostname']
            self.hostname = hostname
        except Exception:
            self.status = "FAILED CONNECT"
            connected = self.status


        # Capture pre verification commands
        if self.verification_commands:
            pre_output = generic.capture_commands(connected, self.verification_commands)
            if pre_output:
                self.pre_verification_commands_status = "success"
                self.pre_verification_commands_url = self.logbin(pre_output, description="upgrade pre-verification commands for {}".format(self.device))
            else:
                self.status = "FAILED - COULD NOT GATHER VERIFICATION COMMANDS"
                exit(1)

        # Backup Running Config
        self.status = "Getting Configuration"
        output = device.native.send_command('show running-config')
        self.running_config_url = self.logbin(output)
        if len(output) > 0:
            self.get_running_config_status = "success"
            self.status = "SUCCESS"
        else:
            self.get_running_config_status = "warn"
            self.status = "WARNING"

        #Get Version
        self.status = "Getting Version"
        output = device.native.send_command('show ver | i Cisco | i Version')
        self.pre_version_url = self.logbin(output)
        if len(output) > 0:
            self.get_pre_version_status = "success"
            self.status = "SUCCESS"
        else:
            self.get_pre_version_status = "warn"
            self.status = "WARNING"

        #Get Bootvar
        self.status = "Getting Bootvar"
        output = device.native.send_command('show bootvar | i BOOT variable')
        self.pre_bootvar_url = self.logbin(output)
        if len(output) > 0:
            self.pre_bootvar_status = "success"
            self.status = "SUCCESS"
        else:
            self.pre_bootvar_status = "warn"
            self.status = "WARNING"

        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
        self.status_light = "info"

        for key, value in self._attributes.iteritems():
            if "status" in key:
                if value.lower() == 'success' or value.lower() == 'default' or value.lower() == 'info':
                    print("Success")
                    print(key, value)
                    self.status = "{} Pre-Upgrade SNAPSHOT SUCCESSFUL".format(hostname)
                else:
                    print("Not")
                    print(key,value)
                    self.status = "{} Pre-Upgrade SNAPSHOT Detects a Problem".format(hostname)
                    self.status_light = "warning"
                    break
            print("\n")
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")

        print('staging thread for {} exiting...'.format(self.device))
Ejemplo n.º 14
0
from pyntc import ntc_device as NTC
import getpass

f = open('iplist.txt', 'r')

hosts = f.read()
hosts = hosts.split()

pw = getpass.getpass()
for each in hosts:
	try:
		x = NTC(host=each, username='******', password=pw, device_type='cisco_ios_ssh')
		file = open(each+".txt", "w+")
		print("\n", each, "-- writing backup locally")
		backup = x.running_config
		file.write(backup)
	except:
		print(each, " CONNECTION FAILED")
Ejemplo n.º 15
0
import json
from pyntc import ntc_device as NTC

SW = NTC(host='192.168.2.11',
         username='******',
         password='******',
         device_type='cisco_ios_ssh')
SW.open()

#print (json.dumps(SW.facts, indent=4))
SW.config('hostname pyntc_S1')
SW.config_list(['router ospf 1', 'network 192.168.2.0 0.0.0.255 area 0'])

SW.close()
Ejemplo n.º 16
0
import json
from pyntc import ntc_device as NTC

iosv_l2 = NTC(host='192.168.122.242',username='******',password='******',device_$
iosv_l2.open()

iosv_output = iosv_l2.facts
print json.dumps(iosv_output, indent=4)

iosv_l2.close()

Ejemplo n.º 17
0
import json
from pyntc import ntc_device as NTC
site1 = NTC(host='192.168.122.56',
            username='******',
            password='******',
            device_type='cisco_ios_ssh')
site1.open()
site1_output = site1.facts
print(json.dumps(site1_output, indent=4))
site1.config_list([
    'crypto isakmp policy 1', 'authentication pre-share', 'encryption aes',
    'hash sha', 'group 5', 'lifetime 1800', 'exit',
    'crypto isakmp key shiva address 102.1.1.100',
    'crypto ipsec transform-set t-set esp-aes esp-sha-hmac', 'mode tunnel',
    'exit', 'crypto ipsec security-association lifetime seconds 1800',
    'access-list 101 permit ip 192.168.122.0 0.0.0.255 192.168.102.0 0.0.0.255',
    'crypto map test 10 ipsec-isakmp', 'set transform-set t-set',
    'set peer 102.1.1.100', 'match address 101', 'int g0/0', 'crypto map test',
    'ip access-list extended natacl',
    '1 deny ip 192.168.122.0 0.0.0.255 192.168.102.0 0.0.0.255', 'exit'
])
site1.close()
Ejemplo n.º 18
0
    def staging_process(self):

        print('starting verification job')
        self._attributes = self.get_job_details()
        self.device = self._attributes['device']
        self.log("Updated job details: {}".format(self._attributes))
        self.status = "CONNECTING"
        self._pyntc = None

        try:
            self._pyntc = NTC(host=self.device,
                              username=self.username,
                              password=self.password,
                              device_type="cisco_ios_ssh")
            device = self._pyntc
            hostname = self._pyntc.facts['hostname']

        except Exception:
            self.status = "FAILED CONNECT"

        self.status = "Verifying Interfaces"
        badlist = list()
        if 'NX-OS' in device.native.send_command('show version'):
            output = device.native.send_command('show interface status')
            interfaces = [
                l.split() for l in output.split('\n') if l.startswith('Eth')
            ]
            for i in interfaces[4:]:
                if (i[2] == 'connected') and (i[1] == '--'):
                    badlist.append(i)

            output = ""
            for i in badlist:
                output += "{} is {} and has no description\n".format(
                    i[0], i[2])

        else:
            output = device.native.send_command('show interface description')

            interfaces = [l.split() for l in output.split('\n')]

            for i in interfaces:
                # [u'Gi1/1', u'up', u'up', u'LAB_GW_OUT']
                # [u'Fa1', u'down', u'down']
                if (i[1] == 'up') and (i[2] == 'up'):
                    try:
                        descr = i[3]
                    except:
                        badlist.append(i)
                        pass

            output = ""
            for i in badlist:
                output += "{} is {}/{} and has no description\n".format(
                    i[0], i[1], i[2])

        self.int_check_log_url = self.logbin(output)

        if len(badlist) > 0:
            self.int_check_status = "warning"
        else:
            self.int_check_status = "success"
        self.status = "Completed"
        # print messages are displayed in worker console
        print('staging thread for {} exiting...'.format(self.device))
Ejemplo n.º 19
0
# https://github.com/networktocode/pyntc
# this is amazing
import json
from pyntc import ntc_device as NTC


router6 = NTC(host='192.168.122.60',username='******',password='******',device_type='cisco_ios_ssh')

router6.open()

print(json.dumps(router6.facts,indent=2))



print(router6.show('show ip arp'))

print(router6.show_list(['show ip inte bri','sho clock','show ip arp']))


print(router6.running_config)







router6.config('hostname test-Router6')
router6.config_list(['interface loop 111','ip address 111.11.11.11 255.255.255.255','description test loopback','no shut'])
import json
from pyntc import ntc_device as NTC

R1 = {
    'host': 'R1',
    'device_type':'cisco_ios_ssh',
    'username': '******',
    'password': '******',
}

switch1 = {
    'host': 'Switch1',
    'device_type': 'cisco_ios_ssh',
    'username': '******',
    'password': '******',
}

all_devices = [R1,switch1]
for devices in all_devices:
    connect = NTC(**devices)
    connect.open()
    output = connect.running_config
    print (json.dumps(output, indent=4))
Ejemplo n.º 21
0
from pyntc import ntc_device as NTC

iosvl3 = NTC(host='172.31.33.201',
             username='******',
             password='******',
             device_type='cisco_ios_ssh')
iosvl3.open()

iosvl3.config_list['hostname Router1', 'router ospf 1',
                   'network 10.0.0.0 255.255.255.0 area 0.0.0.0']
Ejemplo n.º 22
0
    def upgrade_process(self):
        print('starting staging job')
        self._attributes = self.get_job_details()
        self.device = self._attributes['device']
        reloaded = False

        # Connect to device
        try:
            connected = NTC(host=self.device,
                            username=self.username,
                            password=self.password,
                            device_type="cisco_ios_ssh")

        except NetMikoTimeoutException:
            connected = None

        # Proceed with upgrade
        self.status = "CONNECTING"

        if connected:
            hostname = connected.facts['hostname']
            start = datetime.datetime.now()
            print("Upgrade for {} started at {}".format(hostname, start))

        else:
            self.status = "FAILED - COULD NOT CONNECT TO DEVICE"
            exit()

        # Capture pre verification commands
        if self.verification_commands:
            pre_output = generic.capture_commands(connected,
                                                  self.verification_commands)
            if pre_output:
                self.pre_verification_commands_status = "success"
                self.pre_verification_commands_url = self.logbin(
                    pre_output,
                    description="upgrade pre-verification commands for {}".
                    format(self.device))
            else:
                self.status = "FAILED - COULD NOT GATHER VERIFICATION COMMANDS"
                exit(1)

        # Backup Running Config
        self.status = "BACKING UP RUNNING CONFIG"
        output = connected.show('show running-config')
        if output:
            self.backup_running_config_status = "success"
            logbin_url = self.logbin(
                output,
                description="backup running config for {}".format(self.device))
            self.backup_running_config_log_url = logbin_url
        else:
            self.status = "FAILED - COULD NOT BACKUP RUNNING CONFIG"
            exit()

        # Change bootvar
        self.status = "SETTING BOOT VARIABLE"
        result = generic.set_bootvar(connected, image=self.target_image)
        bootvar_result, bootvar_output = result
        if bootvar_output:
            logbin_url = self.logbin(
                bootvar_output,
                description="setting boot variable for {}".format(self.device))
            self.set_bootvar_status_log_url = logbin_url
            self.set_bootvar_status = "success"
        else:
            self.status = "FAILED - COULD NOT SET BOOT VARIABLE"
            exit()

        # Verify bootvar
        self.status = "VERIFY BOOT VARIABLE"
        result = generic.verify_bootvar(connected, self.target_image)
        valid_bootvar, valid_bootvar_output = result

        if valid_bootvar:
            logbin_url = self.logbin(
                valid_bootvar_output,
                description="verify boot variable for {}".format(self.device))
            self.verify_bootvar_status_log_url = logbin_url
            self.set_bootvar_status = "success"
            self.verify_bootvar_status = "success"
            time.sleep(10)
        else:
            self.status = "FAILED - COULD NOT VERIFY BOOT VARIABLE"
            exit()

        # Reload
        self.status = "RELOADING"
        reload_output = generic.reload_device(connected,
                                              command=self.reload_command)
        logbin_url = self.logbin("{}".format(reload_output),
                                 description="reload output for {}".format(
                                     self.device))
        self.reload_status_log_url = logbin_url

        reloaded = True
        if reloaded:
            self.reload_status = "success"

        else:
            self.status = "FAILED"
            exit()

        # wait for device to come line
        if reloaded and generic.wait_for_reboot(self.device):
            self.status = "BACK ONLINE, WAITING FOR BOOTUP"
            # linecards may still be booting/upgrading
            time.sleep(300)

        else:
            self.status = "FAILED"
            exit()

        # Verify upgrade
        self.status = "VERIFYING UPGRADE"
        online = NTC(host=self.device,
                     username=self.username,
                     password=self.password,
                     device_type="cisco_ios_ssh")

        # here we are formatting the output that will be pushed to the logbin
        # so that it will be able to be viewed as an iframe
        image_output = '\nDetecting image name with `sho ver | inc image`\n'
        image_output += online.show('sho ver | inc image')
        image_output += '\nDetecting uptime with `show ver | inc ptime`\n'
        image_output += online.show('sho ver | inc ptime')

        # some platforms may have limitation in how many chars of the boot image display
        # so we'll do our part to shorten our image name
        match_pattern = self.target_image.split('.bin')[0]
        upgraded = match_pattern in image_output
        image_output += "\nChecking if {} is present in the output...".format(
            match_pattern)
        if upgraded:
            print("\nFound {} in command output".format(self.target_image))
            image_output += "it is"
            self.verify_upgrade = "success"

        else:
            print("\nCould not find {} in command output\n".format(
                self.target_image))
            image_output += "rur roh"
            self.verify_upgrade = "danger"

        # ship the log file and move on
        print image_output
        logbin_url = self.logbin(image_output,
                                 description="verify upgrade for {}".format(
                                     self.device))
        self.verify_upgrade_log_url = logbin_url

        custom_1 = self.custom_verification_1()
        custom_2 = self.custom_verification_2()

        # Capture post verification commands
        if self.verification_commands:
            post_output = generic.capture_commands(online,
                                                   self.verification_commands)
            if post_output:
                self.post_verification_commands_status = "success"
                descr = "post upgrade verification commands for {}".format(
                    self.device)
                self.post_verification_commands_url = self.logbin(
                    post_output, description=descr)
            else:
                self.status = "FAILED - COULD NOT GATHER POST VERIFICATION COMMANDS"
                exit(1)

        if all([online, upgraded, custom_1, custom_2]):
            self.status = "UPGRADE SUCCESSFUL"
            print("Upgrade was successful")
        else:
            self.status = "UPGRADE FAILED"
            print("Unable to verify image load was successful")

        end = datetime.datetime.now()
        print("Upgrade for {} ended at {}".format(hostname, end))
import json
from pyntc import ntc_device as NTC

device_list = [
    '192.168.122.2',
    '192.168.122.226',
]

for device in device_list:
    iosvl2 = NTC(host=device,
                 username='******',
                 password='******',
                 device_type='cisco_ios_ssh')

    print('Accessing device ' + str(device))
    iosvl2.open()
    ios_run = iosvl2.running_config

    print('Running configuration from device ' + str(device))
    print(ios_run)

    print('Saving device configuration to file')
    saveoutput = open('device' + str(device), 'w')
    saveoutput.write(ios_run)
    saveoutput.close

    iosvl2.close()
Ejemplo n.º 24
0
#################################################
#Backup switch config to a file
import json
from pyntc import ntc_device as NTC
iosvl2 = NTC(host='192.168.122.72',
             username='******',
             password='******',
             device_type='cisco_ios_ssh')
iosvl2.open()

ios_run = iosvl2.backup_running_config('iosvl2-1.cfg')

iosvl2.close()
Ejemplo n.º 25
0
import json
from pyntc import ntc_device as NTC
iosvl2 = NTC(host='192.168.122.226',
             username='******',
             password='******',
             device_type='cisco_ios_ssh')
iosvl2.open()

ios_output = iosvl2.facts
print(json.dumps(ios_output, indent=4))

iosvl2.config_list([
    'hostname S1', 'router ospf 1', 'network 0.0.0.0 255.255.255.255 area 0',
    'network 192.168.122.0 0.0.0.255 area 0',
    'network 10.1.20.0 0.0.0.255 area 1'
])

iosvl2.close()
Ejemplo n.º 26
0
#Using pyntc get facts

import json
from pyntc import ntc_device as NTC
iosvl2 = NTC(host='192.168.x.x',
             username='',
             password='******',
             device_type='cisco_ios_ssh')
iosvl2.open()

ios_output = iosvl2.facts
print(json.dumps(ios_output, indent=4))

iosvl2.close()
Ejemplo n.º 27
0
##################################
#Get(retrive) configuration
import json
from pyntc import ntc_device as NTC
iosvl2 = NTC(host='XXXXXX',
             username='******',
             password='******',
             device_type='cisco_ios_ssh')
iosvl2.open()

ios_run = iosvl2.running_config
print ios_run

iosvl2.close()
Ejemplo n.º 28
0
import json
from pyntc import ntc_device as NTC
iosvl2 = NTC(host="192.168.122.82",
             username="******",
             password="******",
             device_type="cisco_ios_ssh")
iosvl2.open()

ios_output = iosvl2.facts
print(json.dumps(ios_output, indent=4))

iosvl2.config_list([
    "hostname s1", "router ospf 1",
    "no network 0.0.0.0 255.255.255.255 area 0",
    "network 10.1.1.0 0.0.0.255 area 0", "network 10.1.2.0 0.0.0.255 area 1"
])
import json
from pyntc import ntc_device as NTC

iosvl2 = NTC(host='192.168.1.50',
             username='******',
             password='******',
             device_type='cisco_ios_ssh')
iosvl2.open()

ios_run = iosvl2.running_config
print(ios_run)
from pyntc import ntc_device as NTC

host = '172.31.33.201'

iosvl3 = NTC(host=host,
             username='******',
             password='******',
             device_type='cisco_ios_ssh')
iosvl3.open()

########################################################

running_config = iosvl3.running_config

saveoutput = open('Router' + host, 'w')
saveoutput.write(running_config)
saveoutput.close

########################################################
# OR
########################################################

running_config = iosvl3.backup_running_config('iosvl3.cfg')

########################################################