def open( self, hostname: Optional[str], username: Optional[str], password: Optional[str], port: Optional[int], platform: Optional[str], extras: Optional[Dict[str, Any]] = None, configuration: Optional[Config] = None, ) -> None: """ Open a scrapli connection to a device Args: hostname: hostname from nornir inventory username: username from nornir inventory/connection_options for scrapli password: password from nornir inventory/connection_options for scrapli port: port from nornir inventory/connection_options for scrapli platform: platform from nornir inventory/connection_options for scrapli; ignored with scrapli netconf extras: extras dict from connection_options for scrapli -- pass all other scrapli arguments here configuration: nornir configuration Returns: N/A # noqa: DAR202 Raises: N/A """ # platform is irrelevant for scrapli netconf for now _ = platform extras = extras or {} # 99.9% configuration will always be passed here... but to be consistent w/ the other # plugins we'll leave the function signature same/same as the others global_config = configuration.dict() if configuration else {} parameters: Dict[str, Any] = { "host": hostname, "auth_username": username or "", "auth_password": password or "", "port": port or 830, "ssh_config_file": global_config.get("ssh", {}).get("config_file", False), } # will override any of the configs from global nornir config (such as ssh config file) with # options from "extras" (connection options) parameters.update(extras) connection = NetconfScrape(**parameters) connection.open() self.connection = connection # pylint: disable=W0201
def open( self, hostname: Optional[str], username: Optional[str], password: Optional[str], port: Optional[int], platform: Optional[str], extras: Optional[Dict[str, Any]] = None, configuration: Optional[Config] = None, ) -> None: """ Open a scrapli connection to a device Args: hostname: hostname from nornir inventory username: username from nornir inventory/connection_options for scrapli password: password from nornir inventory/connection_options for scrapli port: port from nornir inventory/connection_options for scrapli platform: platform from nornir inventory/connection_options for scrapli; ignored with scrapli netconf extras: extras dict from connection_options for scrapli -- pass all other scrapli arguments here configuration: nornir configuration Returns: N/A # noqa: DAR202 Raises: N/A """ # for now not trying to get ssh config out of configuration, but should in the future... # platform is irrelevant for scrapli netconf for now _, _ = configuration, platform extras = extras or {} parameters: Dict[str, Any] = { "host": hostname, "auth_username": username or "", "auth_password": password or "", "port": port or 22, } parameters.update(extras) connection = NetconfScrape(**parameters) connection.open() self.connection = connection # pylint: disable=W0201
from scrapli_netconf.driver import NetconfScrape my_device = { "host": "sandbox-iosxe-latest-1.cisco.com", "auth_username": "******", "auth_password": "******", "auth_strict_key": False, "port": 830 } conn = NetconfScrape(**my_device) conn.open() ospf_xpath = '/ospf-oper-data/ospf-state/ospf-instance[af="address-family-ipv4" and router-id="235802126"]/ospf-area[area-id=599]/ospf-interface[name="Loopback14"]' response = conn.get(filter_=ospf_xpath, filter_type='xpath') print(response.result)
""" # conn = NetconfScrape(**my_device) # conn.open() # result = conn.edit_config(config=template, target="running") # print(result.result) filter_ = """ <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"> <interface> <name>GigabitEthernet3</name> </interface> </interfaces> """ conn = NetconfScrape(**my_device) conn.open() response = conn.get(filter_=filter_, filter_type="subtree") print(response.result) # template = f""" # <config> # <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"> # <interface> # <name>{name}</name> # <description>{descr}</description> # <type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type> # <enabled>true</enabled> # <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"> # <address operation="delete"> # <ip>{ip_addr}</ip>
def main(): # create scrapli_netconf connection just like with scrapli, open the connection conn = NetconfScrape(**IOSXR_DEVICE) conn.open() # lock the candidate config before starting because why not result = conn.lock(target="candidate") print(result.result) # get the whole config; just like scrapli the result is a `Response` object, in this case a # `NetconfResponse` object with some additional methods result = conn.get_config() # print xml text result print(result.result) # print xml element result print(result.xml_result) # get the whole config, but apply some filters (subtree filters) filters = [INTERFACE_ACTIVE_FILTER, NETCONF_YANG_FILTER] result = conn.get_config(filters=filters) print(result.result) # get something other than the config; note the `filter_` to not reuse builtins result = conn.get(filter_=PLATFORM_FILTER) print(result.result) # edit the candidate configuration result = conn.edit_config(config=EDIT_CDP, target="candidate") print(result.result) # commit config changes conn.commit() print(result.result) # stage a config we'll discard config = EDIT_BANNER result = conn.edit_config(config=config, target="candidate") print(result.result) # discard this config change result = conn.discard() print(result.result) # unlock the candidate now that we're done result = conn.unlock(target="candidate") print(result.result) # close the session conn.close()
def dummy_conn(): conn = NetconfScrape(host="localhost") return conn
def main(): # create scrapli_netconf connection just like with scrapli, open the connection conn = NetconfScrape(**JUNOS_DEVICE) conn.open() # lock the candidate config before starting because why not result = conn.lock(target="candidate") print(result.result) # get the whole config; just like scrapli the result is a `Response` object, in this case a # `NetconfResponse` object with some additional methods result = conn.get_config() # print xml text result print(result.result) # print xml element result print(result.xml_result) # get the whole config, but apply some filters (subtree filters) in the case of junos since its # just a config, not a model and we are filtering for things under `configuration` this has to # live in a single filter unlike iosxr result = conn.get_config(filters=CONFIG_FILTER) print(result.result) # get some operational data via "rpc" for juniper style rpc calls; note the `filter_` to # not reuse builtins result = conn.rpc(filter_=COMMIT_FILTER) print(result.result) # edit the candidate configuration result = conn.edit_config(config=EDIT_NETCONF, target="candidate") print(result.result) # commit config changes conn.commit() print(result.result) # edit multiple config elements result = conn.edit_config(config=EDIT_MULTIPLE, target="candidate") print(result.result) # discard this config change result = conn.discard() print(result.result) # unlock the candidate now that we're done result = conn.unlock(target="candidate") print(result.result) # close the session conn.close()
def test_init_invalid_transport(): with pytest.raises(TransportPluginError) as exc: NetconfScrape(host="localhost", transport="telnet") assert str( exc.value ) == "`NetconfScrape` is only supported using the `system` transport plugin"
def test_init(): conn = NetconfScrape(host="localhost") assert conn.transport_class == NetconfSystemSSHTransport
from scrapli_netconf.driver import NetconfScrape from mydevice import * import logging logging.basicConfig(level=logging.DEBUG) xpath_filter = ''' <get> <filter xmlns:t="http://cisco.com/ns/yang/Cisco-IOS-XE-ospf-oper" type="xpath" select="/ospf-oper-data/ospf-state/ospf-instance[af='address-family-ipv4' and router-id='168432911']" /> </get> ''' conn = NetconfScrape(**router) conn.open() response = conn.rpc(filter_=xpath_filter) print(response.result)
from scrapli_netconf.driver import NetconfScrape my_device = { "host": "sandbox-iosxe-latest-1.cisco.com", "auth_username": "******", "auth_password": "******", "auth_strict_key": False, "port": 830 } conn = NetconfScrape(**my_device) conn.open() ospf_filter = """ <ospf-oper-data xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ospf-oper"> <ospf-state> <ospf-instance> <af>address-family-ipv4</af> <router-id>235802126</router-id> <ospf-area> <area-id>599</area-id> <ospf-interface> <name>Loopback14</name> </ospf-interface> </ospf-area> </ospf-instance> </ospf-state> </ospf-oper-data> """ response = conn.get(
# test-scrap.py import logging from scrapli_netconf.driver import NetconfScrape logging.basicConfig(filename="scrapli.log", level=logging.INFO) logger = logging.getLogger("scrapli") DEVICE = { "host": "192.168.0.222", "port": 830, "auth_username": "******", "auth_password": "******", "auth_strict_key": False, } conn = NetconfScrape(**DEVICE) conn.open() result = conn.get_config() print(result.result) print(result.get_xml_elements())
from scrapli_netconf.driver import NetconfScrape my_device = { "host": "10.10.20.100", "auth_username": "******", "auth_password": "******", "auth_strict_key": False, "port": 830 } conn = NetconfScrape(**my_device) conn.open() rpc_filter = ''' <get> <filter xmlns:t="urn:ietf:params:xml:ns:yang:ietf-interfaces" type="xpath" select="/interfaces/interface[name='Vlan500']/description"/> </get> ''' response = conn.rpc(rpc_filter) print(response.result)
from scrapli_netconf.driver import NetconfScrape from mydevice import * import logging logging.basicConfig(level=logging.DEBUG) eigrp_filter = '/eigrp-oper-data/eigrp-instance/eigrp-topo/eigrp-network[afi="eigrp-af-ipv4" and ip-prefix="192.168.10.0/24"]/rd-vecmetric' ospf_filter = '/ospf-oper-data' conn = NetconfScrape(**router) conn.open() response = conn.get( filter_=ospf_filter, filter_type='xpath', ) print(response.result)
def main(): """Edit config example""" # create scrapli_netconf connection just like with scrapli, open the connection conn = NetconfScrape(**IOSXR_DEVICE) conn.open() # lock the candidate config before starting because why not result = conn.lock(target="candidate") print(result.result) config = EDIT_INTERFACE_G_0_0_0_0 result = conn.edit_config(config=config, target="candidate") print(result.result) # commit config changes conn.commit() print(result.result) # unlock the candidate now that we're done result = conn.unlock(target="candidate") print(result.result) # close the session conn.close()
from scrapli_netconf.driver import NetconfScrape my_device = { "host": "10.10.10.2", "auth_username": "******", "auth_password": "******", "auth_strict_key": False, "port": 830 } conn = NetconfScrape(**my_device) conn.open() response = conn.get( filter_='//eigrp-network[ip-prefix="192.168.10.0/24"]/rd-vecmetric', filter_type="xpath") print(response.result)