Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_connect_return_node(self):
transport = Mock()
with patch.dict(pyeapi.client.TRANSPORTS, {'https': transport}):
conf = get_fixture('eapi.conf')
pyeapi.client.load_config(filename=conf)
node = pyeapi.client.connect(host='192.168.1.16', username='eapi',
password='password', port=None,
timeout=60, return_node=True)
kwargs = dict(host='192.168.1.16', username='eapi',
password='password', port=None, key_file=None,
cert_file=None, ca_file=None, timeout=60)
transport.assert_called_once_with(**kwargs)
self.assertIsNone(node._enablepwd)
def init(opts):
'''
Open the connection to the Arista switch over the eAPI.
'''
proxy_dict = opts.get('proxy', {})
conn_args = proxy_dict.copy()
conn_args.pop('proxytype', None)
opts['multiprocessing'] = conn_args.get('multiprocessing', True)
# This is not a SSH-based proxy, so it should be safe to enable
# multiprocessing.
try:
conn = pyeapi.client.connect(**conn_args)
node = pyeapi.client.Node(conn, enablepwd=conn_args.get('enablepwd'))
pyeapi_device['connection'] = node
pyeapi_device['initialized'] = True
pyeapi_device['up'] = True
except pyeapi.eapilib.ConnectionError as cerr:
log.error('Unable to connect to %s', conn_args['host'], exc_info=True)
return False
return True
my_devices = yaml_out["my_devices"]
eapi_devices = []
for device_name in my_devices:
device_dict = yaml_out[device_name]
device_dict["password"] = password
# Generate config from template
j2_vars = device_dict.pop("data")
template = env.get_template(template_file)
cfg_lines = template.render(**j2_vars)
cfg_lines = cfg_lines.strip()
cfg_lines = cfg_lines.splitlines()
# Establish eAP connection and push config
eapi_conn = pyeapi.client.connect(**device_dict)
device_obj = pyeapi.client.Node(eapi_conn)
eapi_devices.append(device_obj)
output = device_obj.config(cfg_lines)
print(output)
# Verify interfaces
for device_obj in eapi_devices:
output = device_obj.enable("show ip interface brief")
print()
print("-" * 50)
print(output[0]["result"]["output"].rstrip())
print("-" * 50)
print()
def main():
devices = yaml_load_devices()
password = os.getenv("PYNET_PASSWORD") if os.getenv("PYNET_PASSWORD") else getpass()
for name, device_dict in devices.items():
device_dict["password"] = password
connection = pyeapi.client.connect(**device_dict)
device = pyeapi.client.Node(connection)
output = device.enable("show ip arp")
arp_list = output[0]["result"]["ipV4Neighbors"]
output_printer(arp_list)
import os
import pyeapi
from getpass import getpass
from my_funcs import yaml_load_devices
if __name__ == "__main__":
password = os.getenv("PYNET_PASSWORD") if os.getenv("PYNET_PASSWORD") else getpass()
devices = yaml_load_devices()
for name, device_dict in devices.items():
device_dict["password"] = password
connection = pyeapi.client.connect(**device_dict)
device = pyeapi.client.Node(connection)
output = device.enable("show ip route")
routes = output[0]["result"]["vrfs"]["default"]["routes"]
print()
for prefix, route_dict in routes.items():
route_type = route_dict["routeType"]
print()
print(prefix)
print("-" * 12)
print(route_type)
print(">" * 6)
print(route_dict["vias"][0]["interface"])
if route_type == "static":
print(route_dict["vias"][0]["nexthopAddr"])
print("-" * 12)
def provision_eos(port, username, password):
connection = pyeapi.client.connect(
transport="https",
host="localhost",
username="vagrant",
password="vagrant",
port=port,
)
device = pyeapi.client.Node(connection)
commands = list()
commands.append("configure session")
commands.append("rollback clean-config")
with open("../eos/initial.conf", "r") as f:
lines = f.readlines()
for line in lines:
import pyeapi
from getpass import getpass
password = getpass()
arista8 = {
"transport": "https",
"host": "arista8.lasthop.io",
"username": "pyclass",
"password": password,
"port": 443,
}
connection = pyeapi.client.connect(**arista8)
device = pyeapi.client.Node(connection)
output = device.enable("show ip arp")
print()
print("-" * 40)
arp_list = output[0]["result"]["ipV4Neighbors"]
for arp_entry in arp_list:
mac_address = arp_entry["hwAddress"]
ip_address = arp_entry["address"]
print("{:^15}{:^5}{:^15}".format(ip_address, "-->", mac_address))
print("-" * 40)
print()
def open(self):
"""Implementation of NAPALM method open."""
try:
if self.transport in ('http', 'https'):
connection = pyeapi.client.connect(
transport=self.transport,
host=self.hostname,
username=self.username,
password=self.password,
port=self.port,
timeout=self.timeout
)
elif self.transport == 'socket':
connection = pyeapi.client.connect(transport=self.transport)
else:
raise ConnectionException("Unknown transport: {}".format(self.transport))
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(ce.message)