def run(vendor, hostname, user, password, strategy, optional_args, config_file, dry_run): logger.debug('Getting driver for OS "{driver}"'.format(driver=vendor)) driver = get_network_driver(vendor) optional_args = parse_optional_args(optional_args) logger.debug('Connecting to device "{}" with user "{}" and optional_args={}'.format( hostname, user, optional_args)) with driver(hostname, user, password, optional_args=optional_args) as device: logger.debug('Strategy for loading configuration is "{strategy}"'.format(strategy=strategy)) if strategy == 'replace': strategy_method = device.load_replace_candidate elif strategy == 'merge': strategy_method = device.load_merge_candidate logger.debug('Loading configuration file "{config}"'.format(config=config_file)) strategy_method(filename=config_file) logger.debug('Comparing configuration') diff = device.compare_config() if dry_run: logger.debug('Dry-run. Discarding configuration.') else: logger.debug('Committing configuration') device.commit_config() logger.debug('Closing session') return diff
def main(): warning() args = build_help(validate=True) configure_logging(logger, args.debug) logger.debug('Getting driver for OS "{driver}"'.format(driver=args.vendor)) driver = get_network_driver(args.vendor) optional_args = parse_optional_args(args.optional_args) logger.debug('Connecting to device "{}" with user "{}" and optional_args={}'.format( args.hostname, args.user, optional_args)) with driver(args.hostname, args.user, args.password, optional_args=optional_args) as device: logger.debug('Generating compliance report') print(json.dumps(device.compliance_report(args.validation_file), indent=4)) logger.debug('Closing session') sys.exit(0)
def main(): warning() args = build_help(connect_test=True) configure_logging(logger, args.debug) logger.debug('Getting driver for OS "{driver}"'.format(driver=args.vendor)) driver = get_network_driver(args.vendor) optional_args = parse_optional_args(args.optional_args) logger.debug('Connecting to device "{}" with user "{}" and optional_args={}'.format( args.hostname, args.user, optional_args)) with driver(args.hostname, args.user, args.password, optional_args=optional_args) as device: logger.debug('Successfully connected to the device: {}'.format(device.hostname)) print('Successfully connected to the device') sys.exit(0)
def main(): #fetch list of network nodes from the Node CSV ournodes = readcsvfile(workingpath + nodefilename) #If there are any nodes to process ... if len(ournodes) > 0: deployednodes = deployloopbacks(ournodes) #initialise NAPALM for IOS devices driver = get_network_driver('ios') #loop through the list of nodes which successfully deployed for node in deployednodes: try: #connect to the node over the OOB connection uname = node['username'] if ( node['username'] != "*default*") else defaultusername pword = node['password'] if ( node['password'] != "*default*") else defaultpassword sec = node['secret'] if ( node['secret'] != "*default*") else defaultsecret device = driver(hostname=node['OOB address'], username=uname, password=pword, optional_args={'secret': sec}) device.open() except: print("Cannot connect to", node['Hostname']) else: #pull the list of configured IP interfaces ints = device.get_interfaces_ip() device.close() #and pretty print them print(node['Hostname']) print(json.dumps(ints, sort_keys=True, indent=4))
def call_get_network_driver(vendor): return get_network_driver(vendor)
import json from napalm.base import get_network_driver import pprint optional_args = {'transport': 'https'} driver = get_network_driver('eos') dev = driver(hostname='192.168.1.2', username='******', password='******', optional_args=optional_args) dev.open() a = dev.get_facts() print(a['hostname']) print(a['os_version']) with dev as eos: pprint.pprint(eos.compliance_report("validate-eos.yml")) dev.close()
# Python3 support from __future__ import print_function from __future__ import unicode_literals # NAPALM base from napalm.base import get_network_driver import napalm.base.exceptions from napalm.base.utils import py23_compat import pytest import os BASE_PATH = os.path.dirname(__file__) driver = get_network_driver("mock") optional_args = { "path": os.path.join(BASE_PATH, "test_mock_driver"), "profile": ["eos"], } fail_args = { "path": os.path.join(BASE_PATH, "test_mock_driver"), "profile": ["eos"], "fail_on_open": True, } class TestMockDriver(object): """Test Mock Driver.""" def test_basic(self): d = driver("blah", "bleh", "blih", optional_args=optional_args)
def test_get_network_driver(self, driver): """Check that we can get the desired driver and is instance of NetworkDriver.""" self.assertTrue(issubclass(get_network_driver(driver), NetworkDriver))
if __name__ == '__main__': # Setup environ sys.path.append(os.getcwd()) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "viconf.settings") # Setup django import django django.setup() from nodes.models import Node, Interface nodes = Node.objects.all() for node in nodes: if node.driver != "none": driver = get_network_driver(node.driver) username = node.group.username password = node.group.password optional_args = {'enable_password': node.group.enable_password} try: target = socket.gethostbyname(node.hostname) except socket.error: if node.ipv4 is not None: target = node.ipv4 else: pass with driver(target, username, password, optional_args=optional_args) as device: print("Connecting to device {}".format(target)) interfaces = device.get_interfaces()
# NAPALM base from napalm.base import get_network_driver import napalm.base.exceptions from napalm.base.utils import py23_compat import pytest import os BASE_PATH = os.path.dirname(__file__) driver = get_network_driver("mock") optional_args = { "path": os.path.join(BASE_PATH, "test_mock_driver"), "profile": ["eos"], } fail_args = { "path": os.path.join(BASE_PATH, "test_mock_driver"), "profile": ["eos"], "fail_on_open": True, } class TestMockDriver(object): """Test Mock Driver.""" def test_basic(self):
#in this script i'm printing the used masks/values of IPv4 unicast indirectly-connected routes: # once you get the value of masks/values, you can trigger email to monitor tcam table #below is the output of command 'show platform tcam utilization | i IPv4 unicast indirectly-connected routes' # IPv4 unicast indirectly-connected routes: 2048/2048 34/34 import json from napalm.base import get_network_driver optional_args = {'port': '443'} driver = get_network_driver('ios') dev = driver(hostname='142.225.213.132', username='******', password='******') dev.open() return_dictionary = dev.cli([ 'show platform tcam utilization | i IPv4 unicast indirectly-connected routes', ]) a = (return_dictionary[ 'show platform tcam utilization | i IPv4 unicast indirectly-connected routes'] ) output = a.split("2048/2048", 1)[1] nums = [int(n) for n in output.split('/')] print('used Masks/values is ' + str(nums[0]) + str('/') + str(nums[0])) dev.close()