Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import json
from napalm import get_network_driver
driver = get_network_driver('ios')
iosvl2 = driver('192.168.3.50', 'yasser', 'cisco')
iosvl2.open()
ios_output = iosvl2.get_facts()
print (json.dumps(ios_output, indent=4))
ios_output = iosvl2.get_interfaces()
print (json.dumps(ios_output, sort_keys=True, indent=4))
ios_output = iosvl2.get_interfaces_counters()
print (json.dumps(ios_output, indent=4))
ios_output = iosvl2. get_interfaces_ip()
print (json.dumps(ios_output, indent=4))
password = module.params['password']
timeout = module.params['timeout']
args = module.params['args']
argument_check = {'hostname': hostname, 'username': username, 'dev_os': dev_os}
for key, val in argument_check.items():
if val is None:
module.fail_json(msg=str(key) + " is required")
if module.params['optional_args'] is None:
optional_args = {}
else:
optional_args = module.params['optional_args']
try:
network_driver = get_network_driver(dev_os)
except ModuleImportError as e:
module.fail_json(msg="Failed to import napalm driver: " + str(e))
try:
device = network_driver(hostname=hostname,
username=username,
password=password,
timeout=timeout,
optional_args=optional_args)
device.open()
except Exception as e:
module.fail_json(msg="cannot connect to device: " + str(e))
try:
cli_response = device.cli(**args)
except Exception as e:
#!/usr/bin/env python
import sys, os
import napalm
from jinja2 import FileSystemLoader, Environment
user = "antidote"
password = "antidotepassword"
vendor = "junos"
port = "22"
host = os.getenv("SYRINGE_TARGET_HOST")
template_file = "vqfx3.txt"
driver = napalm.get_network_driver(vendor)
with driver(hostname=host, username=user, password=password, optional_args={'port': port}) as device:
# Retrieve management IP address
# TODO(mierdin): this is junos-specific. Will need to maintain a list of management interfaces per vendor,
# or expose this as a field in the endpoint.
interface_ip_details = device.get_interfaces_ip()['em0.0']
for addr, prefix in interface_ip_details['ipv4'].items():
address_cidr = "%s/%s" % (addr, prefix['prefix_length'])
# Create template configuration with this address
loader = FileSystemLoader([os.path.dirname(os.path.realpath(__file__))]) # Use current directory
env = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True)
template_object = env.get_template(template_file)
rendered_config = template_object.render(mgmt_addr=address_cidr)
# Push rendered config to device
def get_napalm_device(self):
self.logger.debug('looking for napalm driver "%s"', self.platform)
try:
# Driver found, instanciate it
driver = napalm.get_network_driver(self.platform)
self.logger.debug('found napalm driver "%s"', self.platform)
return driver(
hostname=self.hostname,
username=self.napalm_username or settings.NAPALM_USERNAME,
password=self.napalm_password or settings.NAPALM_PASSWORD,
timeout=self.napalm_timeout or settings.NAPALM_TIMEOUT,
optional_args=self.napalm_args or settings.NAPALM_ARGS,
)
except napalm.base.exceptions.ModuleImportError:
# Unable to import proper driver from napalm
# Most probably due to a broken install
self.logger.error(
'no napalm driver "%s" found (not installed or does not exist)',
self.platform,
)
return None
self.optional_args = {}
if "ctl_secret" in self.config and isinstance(
self.config["ctl_secret"], str
):
secret = self.config["ctl_secret"]
else:
secret = getpass.getpass("Enable secret: ")
self.optional_args.update({"secret": secret})
if self.config["ctl_transport"]:
primary_transport = self.config["ctl_transport"][0]
self.optional_args.update({"transport": primary_transport})
if self.config["ssh_config_file"]:
self.optional_args.update(
{"ssh_config_file": self.config["ssh_config_file"]}
)
self.napalm_driver = napalm.get_network_driver(self.driver)
self.napalm_args = {
"hostname": self.hostname,
"username": self.username,
"password": self.password,
"timeout": self.timeout,
"optional_args": self.optional_args,
}
self.napalm = self.napalm_driver(**self.napalm_args)
self.log.info("===============================================")
self.log.info(
f"Opening connection to {self.hostname} via {primary_transport}..."
)
#self.log.debug(pprint.pprint(self.napalm_args))
try:
self.napalm.open()
except (socket.timeout, SSHException, ConnectionRefusedError):
def main():
"""
Connect to set of network devices using NAPALM (different platforms); print
out the facts.
"""
for a_device in device_list:
device_type = a_device.pop('device_type')
driver = get_network_driver(device_type)
device = driver(**a_device)
print()
print(">>>Device open")
device.open()
print("-" * 50)
device_facts = device.get_facts()
print("{hostname}: Model={model}".format(**device_facts))
print()
hostname = module.params['hostname']
username = module.params['username']
dev_os = module.params['dev_os']
password = module.params['password']
timeout = module.params['timeout']
argument_check = {'hostname': hostname, 'username': username, 'dev_os': dev_os}
for key, val in argument_check.items():
if val is None:
module.fail_json(msg=str(key) + " is required")
optional_args = module.params['optional_args'] or {}
try:
network_driver = get_network_driver(dev_os)
except ModuleImportError as e:
module.fail_json(msg="Failed to import napalm driver: " + str(e))
try:
device = network_driver(hostname=hostname,
username=username,
password=password,
timeout=timeout,
optional_args=optional_args)
device.open()
except Exception as err:
module.fail_json(msg="cannot connect to device: {0}".format(str(err)))
return device
if not napalm_found:
module.fail_json(msg="the python module napalm is required")
hostname = module.params['hostname']
username = module.params['username']
dev_os = module.params['dev_os']
password = module.params['password']
timeout = module.params['timeout']
if module.params['optional_args'] is None:
optional_args = {}
else:
optional_args = module.params['optional_args']
try:
network_driver = get_network_driver(dev_os)
device = network_driver(hostname=hostname,
username=username,
password=password,
timeout=timeout,
optional_args=optional_args)
device.open()
except:
module.fail_json(msg="cannot connect to device")
try:
facts = device.get_interfaces()
except:
module.fail_json(msg="cannot retrieve device data")
try:
device.close()