09-03-2020 02:01 AM
Is there a way to retrieve information from one spesific interface, rather than looping through all the interfaces and choosing the correct one?
I find the latter approach to be very inefficient in terms of the time it takes to loop through all available interfaces, and am looking for a better and faster way.
Here is my current code:
from ydk.models.cisco_ios_xe.Cisco_IOS_XE_interfaces_oper import Interfaces
from ydk.services.netconf_service import NetconfService
from ydk.providers.netconf_provider import NetconfServiceProvider service = NetconfService() provider = NetconfServiceProvider(address=address, username=username, password=password)
model = Interfaces()
interfaces = service.get(provider=provider, read_filter=model)
for interface in interfaces.interface:
if interface.name == "GigabitEthernet0/0/7":
return "some data"
Solved! Go to Solution.
09-03-2020 11:07 AM
Try this script (change interface name and device credentials):
import logging
import ydk.models.cisco_ios_xe.Cisco_IOS_XE_interfaces_oper as ifc_oper
from ydk.services.netconf_service import NetconfService
from ydk.providers.netconf_provider import NetconfServiceProvider
def enable_logging(level):
log = logging.getLogger('ydk')
log.setLevel(level)
handler = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
log.addHandler(handler)
if __name__ == "__main__":
enable_logging(logging.INFO)
service = NetconfService()
provider = NetconfServiceProvider(address="ios-xe-mgmt-latest.cisco.com",
port=10000,
username="developer",
password="C1sco12345")
model = ifc_oper.Interfaces()
ifc = ifc_oper.Interfaces.Interface()
ifc.name = "GigabitEthernet3"
model.interface.append(ifc)
interface = service.get(provider=provider, read_filter=ifc)
I got this from XE sandbox:
2020-09-03 11:02:01,620 - ydk - INFO - ============= Sending RPC to device =============
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><get xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<filter><interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper">
<interface>
<name>GigabitEthernet3</name>
</interface>
</interfaces></filter>
</get>
</rpc>
2020-09-03 11:02:01,947 - ydk - INFO - ============= Received RPC from device =============
<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2">
<data>
<interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper">
<interface>
<name>GigabitEthernet3</name>
<interface-type>iana-iftype-ethernet-csmacd</interface-type>
<admin-status>if-state-up</admin-status>
<oper-status>if-oper-state-ready</oper-status>
<last-change>2020-09-03T11:31:33.000402+00:00</last-change>
<if-index>3</if-index>
<phys-address>00:50:56:bb:eb:1e</phys-address>
<speed>1024000000</speed>
<statistics>
<discontinuity-time>2020-09-02T01:36:47.000631+00:00</discontinuity-time>
<in-octets>2180</in-octets>
<in-unicast-pkts>32</in-unicast-pkts>
<in-broadcast-pkts>0</in-broadcast-pkts>
<in-multicast-pkts>0</in-multicast-pkts>
<in-discards>0</in-discards>
<in-errors>0</in-errors>
<in-unknown-protos>0</in-unknown-protos>
<out-octets>0</out-octets>
<out-unicast-pkts>0</out-unicast-pkts>
<out-broadcast-pkts>0</out-broadcast-pkts>
<out-multicast-pkts>0</out-multicast-pkts>
<out-discards>0</out-discards>
<out-errors>0</out-errors>
<rx-pps>0</rx-pps>
<rx-kbps>0</rx-kbps>
<tx-pps>0</tx-pps>
<tx-kbps>0</tx-kbps>
<num-flaps>0</num-flaps>
<in-crc-errors>0</in-crc-errors>
</statistics>
<vrf/>
<description>Network Interface</description>
<mtu>1500</mtu>
<input-security-acl/>
<output-security-acl/>
<v4-protocol-stats>
<in-pkts>0</in-pkts>
<in-octets>0</in-octets>
<in-error-pkts>0</in-error-pkts>
<in-forwarded-pkts>0</in-forwarded-pkts>
<in-forwarded-octets>0</in-forwarded-octets>
<in-discarded-pkts>0</in-discarded-pkts>
<out-pkts>0</out-pkts>
<out-octets>0</out-octets>
<out-error-pkts>0</out-error-pkts>
<out-forwarded-pkts>0</out-forwarded-pkts>
<out-forwarded-octets>0</out-forwarded-octets>
<out-discarded-pkts>0</out-discarded-pkts>
</v4-protocol-stats>
<v6-protocol-stats>
<in-pkts>0</in-pkts>
<in-octets>0</in-octets>
<in-error-pkts>0</in-error-pkts>
<in-forwarded-pkts>0</in-forwarded-pkts>
<in-forwarded-octets>0</in-forwarded-octets>
<in-discarded-pkts>0</in-discarded-pkts>
<out-pkts>0</out-pkts>
<out-octets>0</out-octets>
<out-error-pkts>0</out-error-pkts>
<out-forwarded-pkts>0</out-forwarded-pkts>
<out-forwarded-octets>0</out-forwarded-octets>
<out-discarded-pkts>0</out-discarded-pkts>
</v6-protocol-stats>
<bia-address>00:50:56:bb:eb:1e</bia-address>
<ipv4-tcp-adjust-mss>0</ipv4-tcp-adjust-mss>
<ipv6-tcp-adjust-mss>0</ipv6-tcp-adjust-mss>
<ether-state>
<negotiated-duplex-mode>full-duplex</negotiated-duplex-mode>
<negotiated-port-speed>speed-1gb</negotiated-port-speed>
<auto-negotiate>true</auto-negotiate>
<enable-flow-control>false</enable-flow-control>
</ether-state>
<ether-stats>
<in-mac-control-frames>0</in-mac-control-frames>
<in-mac-pause-frames>0</in-mac-pause-frames>
<in-oversize-frames>0</in-oversize-frames>
<in-jabber-frames>0</in-jabber-frames>
<in-fragment-frames>0</in-fragment-frames>
<in-8021q-frames>0</in-8021q-frames>
<out-mac-control-frames>0</out-mac-control-frames>
<out-mac-pause-frames>0</out-mac-pause-frames>
<out-8021q-frames>0</out-8021q-frames>
</ether-stats>
</interface>
</interfaces>
</data>
</rpc-reply>
09-03-2020 07:23 AM - edited 09-03-2020 07:24 AM
Hi,
Seems like there is no alternative method get a specific Interface entity through YDK's Cisco_IOS_XE_interfaces_oper
http://ydk.cisco.com/py/docs/gen_doc_d3c0b5efefaadaf38699df0c6c6e43bd0355e31c.html
Maybe you could loop through all the interface once, and store it as a python dictionary. So that you can get the Interface entity directly in next time.
Here is an example, please verify / fix the code if there is any syntax error.
interfaces = service.get(provider=provider, read_filter=model) interfaces_dict = {}
for interface in interfaces.interface:
tmp = {interface.name : interface}
interfaces_dict.update(tmp)
print(interfaces_dict['GigabitEthernet0/0/7'].if_index)
09-03-2020 11:07 AM
Try this script (change interface name and device credentials):
import logging
import ydk.models.cisco_ios_xe.Cisco_IOS_XE_interfaces_oper as ifc_oper
from ydk.services.netconf_service import NetconfService
from ydk.providers.netconf_provider import NetconfServiceProvider
def enable_logging(level):
log = logging.getLogger('ydk')
log.setLevel(level)
handler = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
log.addHandler(handler)
if __name__ == "__main__":
enable_logging(logging.INFO)
service = NetconfService()
provider = NetconfServiceProvider(address="ios-xe-mgmt-latest.cisco.com",
port=10000,
username="developer",
password="C1sco12345")
model = ifc_oper.Interfaces()
ifc = ifc_oper.Interfaces.Interface()
ifc.name = "GigabitEthernet3"
model.interface.append(ifc)
interface = service.get(provider=provider, read_filter=ifc)
I got this from XE sandbox:
2020-09-03 11:02:01,620 - ydk - INFO - ============= Sending RPC to device =============
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><get xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<filter><interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper">
<interface>
<name>GigabitEthernet3</name>
</interface>
</interfaces></filter>
</get>
</rpc>
2020-09-03 11:02:01,947 - ydk - INFO - ============= Received RPC from device =============
<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2">
<data>
<interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper">
<interface>
<name>GigabitEthernet3</name>
<interface-type>iana-iftype-ethernet-csmacd</interface-type>
<admin-status>if-state-up</admin-status>
<oper-status>if-oper-state-ready</oper-status>
<last-change>2020-09-03T11:31:33.000402+00:00</last-change>
<if-index>3</if-index>
<phys-address>00:50:56:bb:eb:1e</phys-address>
<speed>1024000000</speed>
<statistics>
<discontinuity-time>2020-09-02T01:36:47.000631+00:00</discontinuity-time>
<in-octets>2180</in-octets>
<in-unicast-pkts>32</in-unicast-pkts>
<in-broadcast-pkts>0</in-broadcast-pkts>
<in-multicast-pkts>0</in-multicast-pkts>
<in-discards>0</in-discards>
<in-errors>0</in-errors>
<in-unknown-protos>0</in-unknown-protos>
<out-octets>0</out-octets>
<out-unicast-pkts>0</out-unicast-pkts>
<out-broadcast-pkts>0</out-broadcast-pkts>
<out-multicast-pkts>0</out-multicast-pkts>
<out-discards>0</out-discards>
<out-errors>0</out-errors>
<rx-pps>0</rx-pps>
<rx-kbps>0</rx-kbps>
<tx-pps>0</tx-pps>
<tx-kbps>0</tx-kbps>
<num-flaps>0</num-flaps>
<in-crc-errors>0</in-crc-errors>
</statistics>
<vrf/>
<description>Network Interface</description>
<mtu>1500</mtu>
<input-security-acl/>
<output-security-acl/>
<v4-protocol-stats>
<in-pkts>0</in-pkts>
<in-octets>0</in-octets>
<in-error-pkts>0</in-error-pkts>
<in-forwarded-pkts>0</in-forwarded-pkts>
<in-forwarded-octets>0</in-forwarded-octets>
<in-discarded-pkts>0</in-discarded-pkts>
<out-pkts>0</out-pkts>
<out-octets>0</out-octets>
<out-error-pkts>0</out-error-pkts>
<out-forwarded-pkts>0</out-forwarded-pkts>
<out-forwarded-octets>0</out-forwarded-octets>
<out-discarded-pkts>0</out-discarded-pkts>
</v4-protocol-stats>
<v6-protocol-stats>
<in-pkts>0</in-pkts>
<in-octets>0</in-octets>
<in-error-pkts>0</in-error-pkts>
<in-forwarded-pkts>0</in-forwarded-pkts>
<in-forwarded-octets>0</in-forwarded-octets>
<in-discarded-pkts>0</in-discarded-pkts>
<out-pkts>0</out-pkts>
<out-octets>0</out-octets>
<out-error-pkts>0</out-error-pkts>
<out-forwarded-pkts>0</out-forwarded-pkts>
<out-forwarded-octets>0</out-forwarded-octets>
<out-discarded-pkts>0</out-discarded-pkts>
</v6-protocol-stats>
<bia-address>00:50:56:bb:eb:1e</bia-address>
<ipv4-tcp-adjust-mss>0</ipv4-tcp-adjust-mss>
<ipv6-tcp-adjust-mss>0</ipv6-tcp-adjust-mss>
<ether-state>
<negotiated-duplex-mode>full-duplex</negotiated-duplex-mode>
<negotiated-port-speed>speed-1gb</negotiated-port-speed>
<auto-negotiate>true</auto-negotiate>
<enable-flow-control>false</enable-flow-control>
</ether-state>
<ether-stats>
<in-mac-control-frames>0</in-mac-control-frames>
<in-mac-pause-frames>0</in-mac-pause-frames>
<in-oversize-frames>0</in-oversize-frames>
<in-jabber-frames>0</in-jabber-frames>
<in-fragment-frames>0</in-fragment-frames>
<in-8021q-frames>0</in-8021q-frames>
<out-mac-control-frames>0</out-mac-control-frames>
<out-mac-pause-frames>0</out-mac-pause-frames>
<out-8021q-frames>0</out-8021q-frames>
</ether-stats>
</interface>
</interfaces>
</data>
</rpc-reply>
09-03-2020 05:10 PM
Good Job.
Thanks
09-04-2020 05:09 AM
Thanks!
This was very helpful :)
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide