Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def open(self):
"""Open the connection with the device."""
try:
self.device.open()
except ConnectTimeoutError as cte:
raise ConnectionException(cte.msg)
self.device.timeout = self.timeout
self.device._conn._session.transport.set_keepalive(self.keepalive)
if hasattr(self.device, "cu"):
# make sure to remove the cu attr from previous session
# ValueError: requested attribute name cu already exists
del self.device.cu
self.device.bind(cu=Config)
if not self.lock_disable and self.session_config_lock:
self._lock()
def _process_optional_args(self, optional_args):
# Define locking method
self.lock_disable = optional_args.get("lock_disable", False)
self.enablepwd = optional_args.pop("enable_password", "")
self.eos_autoComplete = optional_args.pop("eos_autoComplete", None)
# eos_transport is there for backwards compatibility, transport is the preferred method
transport = optional_args.get(
"transport", optional_args.get("eos_transport", "https")
)
try:
self.transport_class = pyeapi.client.TRANSPORTS[transport]
except KeyError:
raise ConnectionException("Unknown transport: {}".format(self.transport))
init_args = inspect.getfullargspec(self.transport_class.__init__)[0]
init_args.pop(0) # Remove "self"
init_args.append("enforce_verification") # Not an arg for unknown reason
filter_args = ["host", "username", "password", "timeout", "lock_disable"]
self.eapi_kwargs = {
k: v
for k, v in optional_args.items()
if k in init_args and k not in filter_args
}
def open(self):
try:
self.device = NXOSDevice(
host=self.hostname,
username=self.username,
password=self.password,
timeout=self.timeout,
port=self.port,
transport=self.transport,
verify=self.ssl_verify,
api_format="jsonrpc",
)
self._send_command("show hostname")
except (NXAPIConnectionError, NXAPIAuthError):
# unable to open connection
raise ConnectionException("Cannot connect to {}".format(self.hostname))
password=self.password,
timeout=self.timeout,
**self.eapi_kwargs
)
if self.device is None:
self.device = pyeapi.client.Node(connection, enablepwd=self.enablepwd)
# does not raise an Exception if unusable
# let's try to run a very simple command
self.device.run_commands(["show clock"], encoding="text")
except ConnectionError as ce:
# and this is raised either if device not avaiable
# either if HTTP(S) agent is not enabled
# show management api http-commands
raise ConnectionException(str(ce))
def open(self):
try:
self.device.open()
except ConnectError as conn_err:
raise ConnectionException(conn_err.args[0])
def open(self):
try:
self.api = connect(
host=self.hostname,
username=self.username,
password=self.password,
timeout=self.timeout
)
except (TrapError, FatalError, ConnectionError, MultiTrapError) as exc:
raise ConnectionException(
'Could not connect to {}:{} - [{!r}]'.format(self.hostname, self.port, exc)
)
def _netmiko_open(self, device_type, netmiko_optional_args=None):
"""Standardized method of creating a Netmiko connection using napalm attributes."""
if netmiko_optional_args is None:
netmiko_optional_args = {}
try:
self._netmiko_device = ConnectHandler(
device_type=device_type,
host=self.hostname,
username=self.username,
password=self.password,
timeout=self.timeout,
**netmiko_optional_args
)
except NetMikoTimeoutException:
raise ConnectionException("Cannot connect to {}".format(self.hostname))
# ensure in enable mode
self._netmiko_device.enable()
return self._netmiko_device
def open(self):
self.device = ConnectHandler(device_type='vyos',
host=self.hostname,
username=self.username,
password=self.password,
**self.netmiko_optional_args)
try:
self._scp_client = SCPConn(self.device)
except: # noqa
raise ConnectionException("Failed to open connection ")