本文整理汇总了Python中napalm.get_network_driver方法的典型用法代码示例。如果您正苦于以下问题:Python napalm.get_network_driver方法的具体用法?Python napalm.get_network_driver怎么用?Python napalm.get_network_driver使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类napalm
的用法示例。
在下文中一共展示了napalm.get_network_driver方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def __init__(self):
"""Initialise new instance."""
if not isinstance(self.driver_class, NetworkDriver):
if type(self).name:
self.driver_class = napalm.get_network_driver(type(self).name)
else:
raise ValueError
示例2: napalm_cli
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def napalm_cli(task, commands, timeout=60, optional_args=None):
"""
Run commands on remote devices using napalm
Arguments:
commands (list): list of commands to execute on the device
timeout (int, optional): defaults to 60
optional_args (dict, optional): defaults to ``{"port": task.host["napalm_port"]}``
Returns:
:obj:`brigade.core.task.Result`:
* result (``dict``): dictionary with the result of the commands
"""
parameters = {
"hostname": task.host.host,
"username": task.host.username,
"password": task.host.password,
"timeout": timeout,
"optional_args": optional_args or {},
}
if "port" not in parameters["optional_args"] and task.host.network_api_port:
parameters["optional_args"]["port"] = task.host.network_api_port
network_driver = get_network_driver(task.host.nos)
with network_driver(**parameters) as device:
result = device.cli(commands)
return Result(host=task.host, result=result)
示例3: napalm_configure
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def napalm_configure(task, configuration, replace=False, timeout=60, optional_args=None):
"""
Loads configuration into a network devices using napalm
Arguments:
configuration (str): configuration to load into the device
replace (bool): whether to replace or merge the configuration
timeout (int, optional): defaults to 60
optional_args (dict, optional): defaults to ``{"port": task.host["napalm_port"]}``
Returns:
:obj:`brigade.core.task.Result`:
* changed (``bool``): whether if the task is changing the system or not
* diff (``string``): change in the system
"""
parameters = {
"hostname": task.host.host,
"username": task.host.username,
"password": task.host.password,
"timeout": timeout,
"optional_args": optional_args or {},
}
if "port" not in parameters["optional_args"] and task.host.network_api_port:
parameters["optional_args"]["port"] = task.host.network_api_port
network_driver = get_network_driver(task.host.nos)
with network_driver(**parameters) as device:
if replace:
device.load_replace_candidate(config=configuration)
else:
device.load_merge_candidate(config=configuration)
diff = device.compare_config()
if task.dry_run:
device.discard_config()
else:
device.commit_config()
return Result(host=task.host, diff=diff, changed=len(diff) > 0)
示例4: napalm_get
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def napalm_get(task, getters, timeout=60, optional_args=None):
"""
Gather information from network devices using napalm
Arguments:
getters (list of str): getters to use
hostname (string, optional): defaults to ``brigade_ip``
username (string, optional): defaults to ``brigade_username``
password (string, optional): defaults to ``brigade_password``
driver (string, optional): defaults to ``nos``
timeout (int, optional): defaults to 60
optional_args (dict, optional): defaults to ``{"port": task.host["napalm_port"]}``
Returns:
:obj:`brigade.core.task.Result`:
* result (``dict``): dictionary with the result of the getter
"""
parameters = {
"hostname": task.host.host,
"username": task.host.username,
"password": task.host.password,
"timeout": timeout,
"optional_args": optional_args or {},
}
if "port" not in parameters["optional_args"] and task.host.network_api_port:
parameters["optional_args"]["port"] = task.host.network_api_port
network_driver = get_network_driver(task.host.nos)
if not isinstance(getters, list):
getters = [getters]
with network_driver(**parameters) as device:
result = {}
for g in getters:
if not g.startswith("get_"):
getter = "get_{}".format(g)
method = getattr(device, getter)
result[g] = method()
return Result(host=task.host, result=result)
示例5: get_napalm_device
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def get_napalm_device(self):
try:
# Driver found, instanciate it
driver = napalm.get_network_driver(self.platform)
return driver(hostname=self.hostname,
username=settings.NAPALM_USERNAME,
password=settings.NAPALM_PASSWORD,
timeout=settings.NAPALM_TIMEOUT,
optional_args=settings.NAPALM_ARGS)
except napalm.base.exceptions.ModuleImportError:
# Unable to import proper driver from napalm
# Most probably due to a broken install
return None
示例6: __init__
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def __init__(self, hostname, os, ipaddress, username, password):
self.hostname = hostname
self.username = username
self.password = password
self.ipaddress = ipaddress
self.os = os
self.driver = napalm.get_network_driver(self.os)
self.device = self.driver(hostname=ipaddress, username=username, password=password)
示例7: bgp_neighbors
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def bgp_neighbors(self):
"""Get BGP session info using napalm."""
try:
driver_name = self.device_driver
except AttributeError:
driver_name = settings.DEFAULT_DRIVER
driver = napalm.get_network_driver(driver_name)
with driver(hostname=self.hostname, username=settings.NAPALM_USERNAME,
password=settings.NAPALM_PASSWORD) as device:
neighbors = device.get_bgp_neighbors()
return neighbors
示例8: _get_devices
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def _get_devices(self):
"""Generate a dictionary of devices
This used to be a fancy dictionary comprehension, but the way that most
NAPALM drivers handle optional_args, this was difficult to maintain.
We're doing it this way now so that optional_args is only provided when
we are explicitly setting "port". Otherwise, we don't want to pass in
"optional_args", so NAPALM can use its default value for port.
"""
devices = {}
for device in self._config['devices']:
port = self._get_port(device)
if port:
devices[device['hostname']] = get_network_driver(device['driver'])(
hostname=device['hostname'],
username=self._get_creds(device['hostname'])['username'],
password=self._get_creds(device['hostname'])['password'],
optional_args={
'port': self._get_port(device)
}
)
else:
devices[device['hostname']] = get_network_driver(device['driver'])(
hostname=device['hostname'],
username=self._get_creds(device['hostname'])['username'],
password=self._get_creds(device['hostname'])['password']
)
return devices
示例9: get_driver
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def get_driver(self, **std_kwargs):
"""Centralizes some of the setup logic for each action
This will allow each action to more or less focus solely on the logic specific
to its task
"""
# Hostname is required
hostname = std_kwargs['hostname']
# These are not required, so we can default to None
credentials = std_kwargs.get('credentials')
driver = std_kwargs.get('driver')
port = std_kwargs.get('port')
htmlout = std_kwargs.get('htmlout', False)
# Look up the driver and if it's not given from the configuration file
# Also overides the hostname since we might have a partial host i.e. from
# syslog such as host1 instead of host1.example.com
found_device = self.find_device_from_config(hostname, driver, credentials, port)
login = self.get_credentials(found_device['credentials'])
if not found_device['port']:
optional_args = None
else:
optional_args = {'port': int(found_device['port'])}
# Some actions like to use these params in log messages, or commands, etc.
# So we tie to instance for easy lookup
self.hostname = found_device['hostname']
self.driver = found_device['driver']
self.htmlout = htmlout
return get_network_driver(self.driver)(
hostname=str(found_device['hostname']),
username=login['username'],
password=login['password'],
optional_args=optional_args
)
示例10: parse_from_device
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def parse_from_device(module, os_choices):
update_module_provider_data(module)
hostname = module.params['hostname']
username = module.params['username']
password = module.params['password']
timeout = module.params['timeout']
models = module.params['models']
mode = module.params['mode']
profiles = module.params['profiles']
dev_os = module.params['dev_os']
argument_check = {'hostname': hostname, 'username': username,
'dev_os': dev_os, 'password': password}
for key, val in argument_check.items():
if val is None:
module.fail_json(msg=str(key) + " is required")
# use checks outside of ansible defined checks, since params come can come
# from provider
dev_os = module.params['dev_os']
if dev_os not in os_choices:
module.fail_json(msg="dev_os is not set to " + str(os_choices))
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 Exception as e:
module.fail_json(msg="cannot connect to device: {}".format(e))
root = get_root_object(models)
if mode in ["config", "both"]:
root.parse_config(device=device, profile=profiles or device.profile)
if mode in ["state", "both"]:
root.parse_state(device=device, profile=profiles or device.profile)
# close device connection
try:
device.close()
except Exception as e:
module.fail_json(msg="cannot close device connection: {}".format(e))
return root
示例11: get_device_instance
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def get_device_instance(module, os_choices):
provider = module.params['provider'] or {}
no_log = ['password', 'secret']
for param in no_log:
if provider.get(param):
module.no_log_values.update(return_values(provider[param]))
if provider.get('optional_args') and provider['optional_args'].get(param):
module.no_log_values.update(return_values(provider['optional_args'].get(param)))
if module.params.get('optional_args') and module.params['optional_args'].get(param):
module.no_log_values.update(return_values(module.params['optional_args'].get(param)))
# allow host or hostname
provider['hostname'] = provider.get('hostname', None) \
or provider.get('host', None)
# allow local params to override provider
for param, pvalue in provider.items():
if module.params.get(param) is not False:
module.params[param] = module.params.get(param) or pvalue
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, 'password': password}
for key, val in argument_check.items():
if val is None:
module.fail_json(msg=str(key) + " is required")
# use checks outside of ansible defined checks,
# since params come can come from provider
if dev_os not in os_choices:
module.fail_json(msg="dev_os is not set to " + str(os_choices))
optional_args = module.params['optional_args'] or {}
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 Exception as err:
module.fail_json(msg="cannot connect to device: {0}".format(str(err)))
return device
示例12: testOpen
# 需要导入模块: import napalm [as 别名]
# 或者: from napalm import get_network_driver [as 别名]
def testOpen(self):
hostname = '172.16.123.3'
username = 'tog'
password = 'tog'
#optional_args = {'port': 830, }
driver = get_network_driver('ios')
self.device = driver(hostname,username,password)
self.device.open()