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
# 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)
Exemple #3
0
from pyntc import ntc_device as NTC

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

SW.backup_running_config('S1_config.cfg')
SW.close()
Exemple #4
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()
Exemple #5
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

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

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

iosvl2.config_list(['hostname R100'])
Exemple #7
0
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.backup_running_config('R100.cfg')
Exemple #8
0
import json
from pyntc import ntc_device as NTC

#Define parameters to establish SSH session
iosvl2 = NTC(host='192.168.122.2',
             username='******',
             password='******',
             device_type='cisco_ios_ssh')

#Open SSH session
iosvl2.open()

#Gather uptime, interfaces etc. from device
ios_output = iosvl2.facts
#Utilise json to format output to more readable format
print(json.dumps(ios_output, indent=4))

#Apply configuration commands in list
iosvl2.config_list(['hostname s1_python'])
iosvl2.close()
Exemple #9
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()
Exemple #10
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
        
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()

from pyntc import ntc_device as NTC

#Define device list
device_list = [
    '192.168.122.2',
    '192.168.122.226',
]
#Loop for each device
for device in device_list:
    iosvl2 = NTC(host=device,
                 username='******',
                 password='******',
                 device_type='cisco_ios_ssh')
    print('Accessing device ' + str(device))
    #Open SSH session to device and backup config to file
    iosvl2.open()
    ios_run = iosvl2.backup_running_config(str(device) + '.cfg')

    iosvl2.close()
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')

########################################################
Exemple #14
0
#/python3

import scapy

import json
from pyntc import ntc_device as NTC

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

# print(json.dumps(SW.facts, indent=4))

SW.config('hostname SWITCH1')
SW.config_list(['int lo3', 'ip add 4.4.4.4 255.255.255.255'])
SW.close()
# 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'])
Exemple #16
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_run = iosvl2.backup_running_config("iosvl2-1.cfg")
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()

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_output = iosvl2.facts
print(json.dumps(ios_output, indent=4))

iosvl2.config_list([
    'hostname R100', 'router ospf 100', 'network 192.168.1.0 0.0.0.255 area 0'
])
Exemple #19
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']
#!/usr/bin/env python

import json
from pyntc import ntc_device as NTC

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

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

rtr.config_list(['router ospf 1', 
                 'no network 0.0.0.0 255.255.255.255 area 0', 
                 'network 10.2.2.1 255.255.255.255 area 0', 
                 'network 192.168.122.0 255.255.255.0 area 0',
                 'network 10.3.3.1 255.255.255.255 area 0'])
                 
rtr.close()
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))
#pyntc is an open source multi-vendor Python library that establishes a common framework for working with different network APIs & device types (including IOS devices)
#
#It's main purpose to is to simplify the execution of common tasks including:
#
#Executing commands
#Copying files
#Upgrading devices
#Rebooting devices
#Saving / Backing Up Configs
#pyntc currently supports four device types:
#
#cisco_ios_ssh
#cisco_nxos_nxapi
#arista_eos_eapi
#juniper_junos_netconf
#https://github.com/networktocode/pyntc

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

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

rtr.config_list([' hostname mgmt_python'])
Exemple #23
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()
Exemple #24
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"
])
##################################
#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()
Exemple #26
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






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)
#this script captures the running config and saves the file

#two methods of writing code to get the running config:
#1st method

#!/usr/bin/env python

import json
from pyntc import ntc_device as NTC

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

ios_run = rtr.running_config
#comment out the below line if you don't want output seen on the screen while running script.
print(ios_run)

HOST = '192.168.122.26'
saveoutput = open('Running-CFG' + HOST, 'w')
saveoutput.write(ios_run)
saveoutput.close

rtr.close()

#2nd method-less code, 5 lines of code!

#!/usr/bin/env python