Exemple #1
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
Exemple #2
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))
# 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'])