cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Announcements
583
Views
11
Helpful
5
Replies
Joshua Leatham
Cisco Employee

XE examples?

I see a lot of python based XR samples in github but no XE.  Not even a hello world that I can find.

The XR hello world also uses a systemTime call that doesn't exist in XE... Are these examples posted anywhere?

Thanks

5 REPLIES 5
Joe Clarke
Hall of Fame Cisco Employee

Sounds like you've seen my blog on VLANs. If you have something else you'd like to see an example of, let me know and I'll try and cook it up.

PGP Key : http://www.marcuscom.com/pgp.asc

Oh hey! Nice blog post! I think a nice IOS-XE hello world for routers would be nice. Maybe something simple like print out the config and/or set an interface IP/description .

Thanks!

Sent from phone, please excuse any errors

Josh Leatham

M: 919 889 5631

einarnn
Cisco Employee

Joshua,

We don't yet have as many published samples for XE as we have for XR, but here is a simple one to get you going. Shows how to query for interfaces and set the description, using the IOS-XE "native" model.

from ydk.types import Empty

from ydk.errors import YPYError

from ydk.services import CRUDService

from ydk.providers import NetconfServiceProvider

from ydk.models.cisco_ios_xe import Cisco_IOS_XE_native as n

from ydk.models.cisco_ios_xe import Cisco_IOS_XE_environment_oper as e

def query_and_print_gi(service, session):

'''query for gigabit ethernet interfaces, print name & description'''

q_intf = n.Native.Interface()

intf = service.read(session, q_intf)

for i in intf.gigabitethernet:

print(i.name, i.description)

def query_for_gi_names(service, session):

'''query for gigabit ethernet interface names'''

q_intf = n.Native.Interface()

intf = service.read(session, q_intf)

return

def set_gi_descr(service, session, intf_name, new_descr):

w_intf = n.Native.Interface.Gigabitethernet()

w_intf.name = intf_name

w_intf.description = new_descr

intf = n.Native.Interface()

intf.gigabitethernet.append(w_intf)

r = service.create(session, intf)

  1. create the provider

session = NetconfServiceProvider(

address='127.0.0.1',

port=2223,

username='vagrant',

password='vagrant',

protocol='ssh')

  1. Initialize a CRUD service

c = CRUDService()

names = query_for_gi_names(c, session)

if len(names)>0:

set_gi_descr(c, session, names[0], 'first gi interface')

if len(names)>1:

set_gi_descr(c, session, names[-1], 'last gi interface')

query_and_print_gi(c, session)

Cheers,

Einar

Thanks!

I had to change a few things so that it would work in Python3.  I also added some stuff to make sure I understood how it all worked.  Helped a ton to get started.

from ydk.types import Empty

from ydk.errors import YPYError

from ydk.services import CRUDService

from ydk.providers import NetconfServiceProvider

#Import native, view here: http://ydk.cisco.com/py/docs/gen_doc_0fe171c66ddd61f15cb095cc5be949d69c0fa46e.html

from ydk.models.cisco_ios_xe import Cisco_IOS_XE_native as n

#Import environment, http://ydk.cisco.com/py/docs/gen_doc_07621ffc0807a582e7d392cc16b68a5bb54fb216.html

from ydk.models.cisco_ios_xe import Cisco_IOS_XE_environment_oper as e

def query_and_print_gi(service, session):

   '''query for gigabit ethernet interfaces, print name & description'''

  q_intf = n.Native.Interface()

  intf = service.read(session, q_intf)

   for i in intf.gigabitethernet:

  print(i.name, i.description, i.ip.address.primary.address)


def query_for_gi_names(service, session):

   '''query for gigabit ethernet interface names'''

  q_intf = n.Native.Interface()

  intf = service.read(session, q_intf)

   #return intf object which contains: Cisco_IOS_XE_native.Native.Interface values

   #We are interested in the ...Interface.gigabitethernet data here:

   #http://ydk.cisco.com/py/docs/gen_doc_69015643701774b0b231b639fe7b70281eb6130b.html

   #now that we have this object(containing all gig interfaces), we can iterate the data

   #out via for loops

   return intf

def set_gi_descr(service, session, intf_name, new_descr):

  w_intf = n.Native.Interface.Gigabitethernet()

  w_intf.name = intf_name

  w_intf.description = new_descr

  intf = n.Native.Interface()

  intf.gigabitethernet.append(w_intf)

  print("Setting gig"+intf_name+ " to: "+new_descr)

  r = service.create(session, intf)

def get_sensor_data(service, session):

  q_sensor = e.EnvironmentSensors.EnvironmentSensor()

  sensor = service.read(session, q_sensor)

  print("Sensor data:")

   for i in sensor:

  print(i.name + " = "+ str(i.current_reading) + " " + str(i.sensor_units.name))


#create the provider

session = NetconfServiceProvider(

  address='10.88.77.240',

  port=830, #netconf TCP, must enable on device

  username='cisco',

  password='cisco',

  protocol='ssh')

#Initialize a CRUD service

c = CRUDService()

names = query_for_gi_names(c, session)


#Find 1st gig interface and add description if empty

if names.gigabitethernet[0].description == None:

  set_gi_descr(c, session, names.gigabitethernet[0].name, 'first gi interface')

else:

  print ("gig"+names.gigabitethernet[0].name + " description: "+ names.gigabitethernet[0].description)


#Find last gig interface and add description if empty

if names.gigabitethernet[-1].description == None:

  set_gi_descr(c, session, names.gigabitethernet[-1].name, 'last gi interface')

else:

  print ("gig"+names.gigabitethernet[-1].name + " description: "+ names.gigabitethernet[-1].description)


query_and_print_gi(c, session)

print("")

get_sensor_data(c, session)

Great! ?

Create
Recognize Your Peers
Content for Community-Ad