Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
cli_cmd)
if not config_operation:
for cmd in cli_cmd:
cmd = cmd.strip()
cli_output[cmd] = (net_connect.send_command(cmd))
self.logger.info('successfully executed cli %s', cmd)
else:
if device_type == 'brocade_netiron':
net_connect.enable()
cli_output['output'] = (net_connect.send_config_set(cli_cmd))
self.logger.info('successfully executed config cli %s', cli_cmd)
self.logger.info('closing connection to %s after executions cli cmds -- all done!',
self.host)
return cli_output
except (NetMikoTimeoutException, NetMikoAuthenticationException,
) as e:
reason = e.message
self.logger.error('Failed to execute cli on %s due to %s', mgmt_ip, reason)
sys.exit(-1)
except SSHException as e:
reason = e.message
self.logger.error('Failed to execute cli on %s due to %s', mgmt_ip, reason)
sys.exit(-1)
except Exception as e:
reason = e.message
# This is in case of I/O Error, which could be due to
# connectivity issue or due to pushing commands faster than what
# the switch can handle
self.logger.error('Failed to execute cli on %s due to %s', mgmt_ip, reason)
sys.exit(-1)
finally:
def send_command_to_devices(devices, command):
data = {}
with ThreadPoolExecutor(max_workers=2) as executor:
future_ssh = [
executor.submit(send_show, device, command) for device in devices
]
for f in as_completed(future_ssh):
try:
result = f.result()
except NetMikoAuthenticationException as e:
print(e)
else:
data.update(result)
return data
def send_command_to_devices(devices, command):
data = {}
with ProcessPoolExecutor(max_workers=2) as executor:
future_ssh = [
executor.submit(send_show, device, command) for device in devices
]
for f in as_completed(future_ssh):
try:
result = f.result()
except NetMikoAuthenticationException as e:
print(e)
else:
data.update(result)
return data
Open a connection to an IOS-XR device.
Connects to the device using SSH and drops into XML mode.
"""
try:
self.device = ConnectHandler(device_type='cisco_xr',
ip=self.hostname,
port=self.port,
username=self.username,
password=self.password,
**self.netmiko_kwargs)
self.device.timeout = self.timeout
self._xml_agent_alive = True # successfully open thus alive
except NetMikoTimeoutException as t_err:
raise ConnectError(t_err.args[0])
except NetMikoAuthenticationException as au_err:
raise ConnectError(au_err.args[0])
self._cli_prompt = self.device.find_prompt() # get the prompt
self._enter_xml_mode()
opts['multiprocessing'] = proxy_dict.get('multiprocessing', False)
netmiko_connection_args = proxy_dict.copy()
netmiko_connection_args.pop('proxytype', None)
netmiko_device['always_alive'] = netmiko_connection_args.pop('always_alive',
opts.get('proxy_always_alive', True))
try:
connection = ConnectHandler(**netmiko_connection_args)
netmiko_device['connection'] = connection
netmiko_device['initialized'] = True
netmiko_device['args'] = netmiko_connection_args
netmiko_device['up'] = True
if not netmiko_device['always_alive']:
netmiko_device['connection'].disconnect()
except NetMikoTimeoutException as t_err:
log.error('Unable to setup the netmiko connection', exc_info=True)
except NetMikoAuthenticationException as au_err:
log.error('Unable to setup the netmiko connection', exc_info=True)
return True
return_msg += output
# Check if proper data received
if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
alt_prompt_terminator, output, flags=re.M
):
return return_msg
self.write_channel(self.TELNET_RETURN)
time.sleep(0.5 * delay_factor)
i += 1
except EOFError:
self.remote_conn.close()
msg = "Login failed: {}".format(self.host)
raise NetMikoAuthenticationException(msg)
# Last try to see if we already logged in
self.write_channel(self.TELNET_RETURN)
time.sleep(0.5 * delay_factor)
output = self.read_channel()
return_msg += output
if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
alt_prompt_terminator, output, flags=re.M
):
return return_msg
self.remote_conn.close()
msg = "Login failed: {}".format(self.host)
raise NetMikoAuthenticationException(msg)
'password': network_password,
'secret': secret,
'port': open_proto['port'],
'verbose': False
}
try:
session = netmiko.ConnectHandler(**device)
if session:
session.enable()
return session
except netmiko.ssh_exception.NetMikoAuthenticationException:
# If my creds are rejected, try the generic telnet password
device['password'] = telnet_password
try:
return netmiko.ConnectHandler(**device)
except netmiko.ssh_exception.NetMikoAuthenticationException:
# If we still can't log in, nothing more to try
raise
except:
raise
)
raise NetMikoAuthenticationException(msg)
# Check if proper data received
if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
alt_prompt_terminator, output, flags=re.M
):
return return_msg
self.write_channel(self.TELNET_RETURN)
time.sleep(0.5 * delay_factor)
i += 1
except EOFError:
self.remote_conn.close()
msg = "Login failed: {}".format(self.host)
raise NetMikoAuthenticationException(msg)
# Last try to see if we already logged in
self.write_channel(self.TELNET_RETURN)
time.sleep(0.5 * delay_factor)
output = self.read_channel()
return_msg += output
if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
alt_prompt_terminator, output, flags=re.M
):
return return_msg
self.remote_conn.close()
msg = "Login failed: {}".format(self.host)
raise NetMikoAuthenticationException(msg)
username=self.username,
password=self.password)
logging.debug('Connection to %s successful!' % self.hostname)
self.prompt = self.sock.find_prompt()
if self.prompt:
logging.debug('Prompt found: %s' % self.prompt)
# Send commands after login that won't be parsed
if self.precommand_list:
for precommand in self.precommand_list:
self.sock.send_command(precommand)
else:
logging.debug('No prompt found')
except ssh_exception.NetMikoAuthenticationException:
logging.error('Authentication error, username was %s' % self.username)
return False
except:
print("Unexpected error:", sys.exc_info()[0])
raise
return True