Giter VIP home page Giter VIP logo

ydk-py-samples's Introduction

Sample Apps for YDK-Py

This repository provides a collection of sample applications that use YDK-Py for network programmability. YDK-Py is the Python package for the Cisco YANG development kit (YDK) which provides model-driven APIs generated from a variety of YANG models.

A "hello, world" App

The hello-ydk.py script illustrates a minimalistic app that prints the uptime of a device running Cisco IOS XR. The script opens a NETCONF session to a device with address 10.0.0.1, reads the system time and prints the formatted uptime.

# import providers, services and models
from ydk.services import CRUDService
from ydk.providers import NetconfServiceProvider
from ydk.models.cisco_ios_xr import Cisco_IOS_XR_shellutil_oper \
    as xr_shellutil_oper
from datetime import timedelta


if __name__ == "__main__":
    """Main execution path"""

    # create NETCONF session
    provider = NetconfServiceProvider(address="10.0.0.1",
                                      port=830,
                                      username="admin",
                                      password="admin",
                                      protocol="ssh")
    # create CRUD service
    crud = CRUDService()

    # create system time object
    system_time = xr_shellutil_oper.SystemTime()

    # read system time from device
    system_time = crud.read(provider, system_time)

    # print system uptime
    print("System uptime is " +
          str(timedelta(seconds=system_time.uptime.uptime)))

    exit()

Sample output

$ ./hello-ydk.py
System uptime is 5 days, 3:52:08
$

Sample App Library

This repository includes a large number of basic sample apps. They focus on a single model and have no or minimal programming logic (conditionals, loops, etc). They should be your starting point if you don't have strong experience with models or programming. They are grouped by service and model.

Installation

The sample apps do not require any special installation. You can download the entire repository as a compressed file or you can clone it:

$ git clone https://github.com/CiscoDevNet/ydk-py-samples.git

Running an App

Instructions for running the basic apps can be found in their README file. Before you attempt to run any app, verify that your system supports Python 2.7 / 3.4 (or later):

$ python --version
Python 2.7.6
$
$ python3 --version
Python 3.5.2
$

Your system must also have YDK-Py installed, including the respective model bundle (e.g. Cisco IOS XR, Cisco IOS XE, OpenConfig or IETF):

$ pip list | grep ydk
ydk                     0.6.0        
ydk-models-cisco-ios-xe 16.6.1       
ydk-models-cisco-ios-xr 6.2.2        
ydk-models-ietf         0.1.3        
ydk-models-openconfig   0.1.3        
$

If you don't have YDK-Py installed, verify your system requirements and then follow the YDK-Py installation instructions.

The release versions of this repository match the release versions of YDK-Py used to test the sample apps. Note that using a different YDK-Py version may break some of the sample apps. For Cisco IOS XR models, you will need a device running at least Cisco IOS XR 6.0.0. For Cisco IOS XE models, you will need a Cisco IOS XE device running at least 16.5.1.

Vagrant Sandbox

You can instantiate a YDK-Py sandbox on your computer using Vagrant. The sandbox will provide an Ubuntu VM with YDK-Py pre-installed. Make sure you have these prerequisites installed on your computer:

To create a sandbox, issue the following command from the directory where the Vagrantfile resides:

$ vagrant up

To verify the status of your sandbox use:

$ vagrant status

Once your sandbox is running, you can connect to it using:

$ vagrant ssh

Note that the samples and projects directories are shared between your host and your Vagrant box. Any changes to those directories are seen in both environments. Any other data in your Vagrant box is isolated from your host and will be lost if you destroy your Vagrant box.

You can suspend and resume your sandbox using:

$ vagrant suspend
$ vagrant resume

To destroy your sandbox, issue the following command:

$ vagrant destroy

If you previously instantiated a YDK-Py sandbox, you can check for updates using:

$ vagrant box outdated

If a newer version is available, you can update your sandbox automatically using:

$ vagrant box update

Documentation and Support

Read the API documentation for details on how to use the API and specific models. For support, join the YDK community to connect with other users and with the makers of YDK

ydk-py-samples's People

Contributors

111pontes avatar dasomeom avatar djsmith20171 avatar fmontoy avatar jabelk avatar jtorella avatar krishna426426 avatar maccioni avatar netwrkr95 avatar scadora avatar shgandhi avatar vosipchu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ydk-py-samples's Issues

vagrant problem

Hi -

been trying to spin up a vagrant box (a few times), it is not working properly:

ALZVEREV-M-X2U9:ydk-py-samples alzverev$ vagrant up
Bringing machine 'ydk-py-ubuntu' up with 'virtualbox' provider...
==> ydk-py-ubuntu: Box 'ydk-py-ubuntu' could not be found. Attempting to find and install...
ydk-py-ubuntu: Box Provider: virtualbox
ydk-py-ubuntu: Box Version: >= 0
==> ydk-py-ubuntu: Box file was not detected as metadata. Adding it directly...
==> ydk-py-ubuntu: Adding box 'ydk-py-ubuntu' (v0) for provider: virtualbox
ydk-py-ubuntu: Downloading: http://ydk.cisco.com/vagrant/ydk-py-ubuntu/
An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try
again.

Failed to connect to ydk.cisco.com port 80: Operation timed out

How to establish NETCONF sessions using SSH authentication in AWS ?

I'm running docker Yang Development Kit for python ydk-py in my remote server Linux Ubuntu. I would like to Establish a connection using with the remote server and my AWS EC2 instance that runs CSR 1000v (SSH authentication)

I used to access my router using the following ssh command:

ssh -i "ssh-key.pem" [email protected]
Where ec2-xx-xx-xx-xxx.us-west-2.compute.amazonaws.com is the hostname, ec2-user is the username and the ssh key ssh-key.pem is for authentification.

As the first step, I want to run the given example in here ydk-py samples

This is the creation of NETCONF session in the given example:

    provider = NetconfServiceProvider(address="10.0.0.1",
                                      port=830,
                                      username="admin",
                                      password="admin",
                                      protocol="ssh")

I have tried this

provider = NetconfServiceProvider(address="ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com", 
username= "ec2-user", 
 public_key_path="mykey.pem")

I have got this error

Traceback (most recent call last):
  File "hello-ydk.py", line 18, in <module>
    private_key_path="mykey.pem")
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. ydk_.providers.NetconfServiceProvider(repo: ydk_.path.Repository, address: unicode, username: unicode, password: unicode, port: int=830L, protocol: unicode=u'ssh', on_demand: bool=True, timeout: int=-1L)
    2. ydk_.providers.NetconfServiceProvider(address: unicode, username: unicode, password: unicode, port: int=830L, protocol: unicode=u'ssh', on_demand: bool=True, common_cache: bool=False, timeout: int=-1L)
    3. ydk_.providers.NetconfServiceProvider(repo: ydk_.path.Repository, address: unicode, username: unicode, private_key_path: unicode, public_key_path: unicode, port: int=830L, on_demand: bool=True, timeout: int=-1L)
    4. ydk_.providers.NetconfServiceProvider(address: unicode, username: unicode, private_key_path: unicode, public_key_path: unicode, port: int=830L, on_demand: bool=True, common_cache: bool=False, timeout: int=-1L)

Invoked with: 'ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com', 'ec2-user'; kwargs: repo=None, public_key_path='mykey.pem'

I kept going through all possibility where I found in the README file in here Read me the running a sample app is the following:

Unless specified by the app, all basic apps take two command line arguments. An optional argument (-v | --verbose) to enable logging and a mandatory argument in URL format that describes the connection details to the networking device (ssh://user:password@device:port):

$ ./nc-read-xr-ip-ntp-oper-10-ydk.py ssh://admin:[email protected]
So in my case, it should be like this, right?

$ ./hello-ydk.py ssh://ec2-user:[email protected]

But still did not work, and here's what I got

from: can't read /var/mail/ydk.services
from: can't read /var/mail/ydk.providers
from: can't read /var/mail/ydk.models.cisco_ios_xr
from: can't read /var/mail/datetime
./hello-ydk.py: 13: ./hello-ydk.py: Syntax error: "(" unexpected (expecting "then")

BGP sample nc-create-config-bgp-10-ydk.py

https://github.com/CiscoDevNet/ydk-py-samples/blob/master/samples/basic/crud/ydk/models/bgp/nc-create-config-bgp-10-ydk.py

Hi

I get this when running the sample:

python nc-create-config-bgp-10-ydk.py
Traceback (most recent call last):
File "nc-create-config-bgp-10-ydk.py", line 36, in
from ydk.models.bgp import bgp as oc_bgp
ImportError: No module named bgp

I then tried:

from ydk.models.openconfig import bgp as oc_bgp

But:

from ydk.models.openconfig import bgp as oc_bgp

ImportError: No module named openconfig

Regards

Nathan

afi-safi-name fields not getting populated in the BGP sample apps

I'm trying to use and modify the openconfig-bgp/nc-create-oc-bgp-40-ydk.py script and I get an "expected element is missing" for the afi-safi-name:

<?xml version="1.0"?>
<rpc-reply message-id="urn:uuid:77f89d28-6859-4261-a20f-7eee433bde93" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <rpc-error>
  <error-type>protocol</error-type>
  <error-tag>missing-element</error-tag>
  <error-severity>error</error-severity>
  <error-message>An expected element is missing.</error-message>
  <error-info>
   <bad-element>afi-safi-name</bad-element>
  </error-info>
 </rpc-error>
</rpc-reply>

I can see in the verbose output that the field is not getting set in the request:

###########  ---snip-----############
  <config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
      <bgp xmlns="http://openconfig.net/yang/bgp">
        <global>
          <afi-safis>
            <afi-safi>
              <afi-safi-name></afi-safi-name>
              <config>
                <afi-safi-name></afi-safi-name>
                <enabled>true</enabled>
              </config>
            </afi-safi>
          </afi-safis>
          <config>
            <as>65001</as>

###########  ---snip-----############

In the code, the field is being set as shown below:

###########  ---snip-----############
def config_bgp(bgp):
    """Add config data to bgp object."""
    # global configuration
    bgp.global_.config.as_ = 65001
    v4_afi_safi = bgp.global_.afi_safis.AfiSafi()
    v4_afi_safi.afi_safi_name = "ipv4-unicast"
    v4_afi_safi.config.afi_safi_name = "ipv4-unicast"
    v4_afi_safi.config.enabled = True
    bgp.global_.afi_safis.afi_safi.append(v4_afi_safi)

    # configure IBGP peer group
    ibgp_pg = bgp.peer_groups.PeerGroup()

###########  ---snip-----############

RuntimeError: YClientError: Could not connect to x.x.x.x: Session status: Undefined

(test_venv) root@8ff5eb066f2e:/root/container/test_venv# python t
ry1.py
Trace: Unable to connect to 172.18.87.54:830 over IPv4 (Connection refused).
Connection error occurred: Unable to connect to 172.18.87.54:830.
NetconfSSHClient: NC session status: -1
Could not connect to 172.18.87.54: Session status: Undefined
Traceback (most recent call last):
File "try1.py", line 28, in
protocol="ssh")
File "/root/container/test_venv/lib/python3.6/site-packages/ydk/providers/netconf_provider.py", line 48, in init
protocol, on_demand, common_cache, timeout)
RuntimeError: YClientError: Could not connect to 172.18.87.54: Session status: Undefined

I am getting this error. i have followed the steps here : https://community.cisco.com/t5/yang-development-kit-ydk/netconfserviceprovider-is-not-able-to-establish-ssh-tunnel/td-p/3585193 but was not able to am still getting this error

i am able to login to the router via cli using : ssh -2 -s [email protected] netconf -p 830

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.