Testing configuration with ncclient (netconf)

I wanted to extend the previous post regarding using netmiko to get the configuration, well since my router supports netconf, I turned on netconf and did a test on getting configuration using get-config.

Now i am testing edit-config to try assigning ip address to an interface and turn the interface on.

Let’s see my R3, which I have default the ethernet0/0 and shut it down.

R3#sh run int e0/0
Building configuration...

Current configuration : 54 bytes
!
interface Ethernet0/0
no ip address
shutdown
end

On ncclient I only need to prepare field for the edit_config() to send to the router.
I can set the target to candidate, running or startup, i have not tried candidate yet since Cisco never has candidate before lol…
I tried the running instead.

So here’s my IOS command config which i want to pass to router using netconf with ncclient.

CREATE_INTERFACE_IP = """
    <config>
        <cli-config-data>
            <cmd>interface %s</cmd>
            <cmd>ip address %s %s</cmd>
            <cmd>no shutdown</cmd>
        </cli-config-data>
    </config>
"""

So here’s the code which i am testing..

from ncclient import manager
import logging

CREATE_INTERFACE_IP = """
    <config>
        <cli-config-data>
            <cmd>interface %s</cmd>
            <cmd>ip address %s %s</cmd>
            <cmd>no shutdown</cmd>
        </cli-config-data>
    </config>
"""


def create_interface_ip(conn, interface, ip, mask):
    try:
        config_str = CREATE_INTERFACE_IP % (interface, ip, mask)
        rpc_sent = conn.edit_config(target='running', config=config_str)
    except Exception:
        print('Exception occurs while creating interface %s' % interface) 


#netconf connection to router
def r_connect(host,port,user,password,):
        return manager.connect(host=host,
                               port=port,
                               username=user,
                               password=password,
                               hostkey_verify=False)

# Test the code here
with r_connect('192.168.1.46','22','cisco','cisco') as m:
        logging.basicConfig(level=logging.DEBUG)
        create_interface_ip(m, "Ethernet0/0", '10.0.0.2', '255.255.255.252')

My exception handling is too general hence i could not get any useful information, at first my code hit exception.. in the end because i put in a broadcast address into the command inadvertently -_-”

the logging is the python debugging in fact not much useful information as it only returns python related logs…

So from Cisco it receives my xml configuration as below, seems like ncclient helped me send the hello response:

*Sep  1 21:51:46.468: GSI: netconf app _s_ssh.[0xC4DD7840]: <?xml version="1.0" encoding="UTF-8"?><nc:hello xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:capabilities><nc:capability>urn:ietf:params:netconf:capability:rollback-on-error:1.0</nc:capability><nc:capability>urn:ietf:params:netconf:capability:confirmed-commit:1.0</nc:capability><nc:capability>urn:ietf:params:netconf:capability:xpath:1.0</nc:capability><nc:capability>urn:ietf:params:netconf:capability:validate:1.0</nc:capability><nc:capability>urn:ietf:params:netconf:base:1.1</nc:capability><nc:capability>urn:ietf:params:netconf:capability:interleave:1.0</nc:capability><nc:capability>urn:ietf:params:netconf:capability:writable-running:1.0</nc:capability><nc:capability>urn:ietf:params:netconf:base:1.0</nc:capability><nc:capability>urn:liberouter:params:netconf:capability:power-control:1.0</nc:capability><nc:capability>urn:ietf:params:netconf:capability:startup:1.0</nc:capability><nc:capability>urn:ietf:params:netconf:capability:notification:1.0</nc:capability><nc:capability>urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file,https,sftp</nc:capability><nc:capability>urn:ietf:params:netconf:capability:candidate:1.0</nc:capability></nc:capabilities></nc:hello>
*Sep  1 21:51:46.468: NETCONF: _namn.54.mlc.don=C5CCF4B8
*Sep  1 21:51:46.468: NETCONF: _narc.54.rcv.ok=C5D134DC
*Sep  1 21:51:46.468: NETCONF: netconf_xml_interpret.54.msg_len=1195 bytes
*Sep  1 21:51:46.468: NETCONF: netconf_xml_interpret.54.ok
*Sep  1 21:51:46.468: NETCONF: _nem.54.msg_type=1
*Sep  1 21:51:46.468: NETCONF: netconf hello#23.count=0
*Sep  1 21:51:46.468: NETCONF: _nem.54.nre_type=3
*Sep  1 21:51:46.468: NETCONF: _nmfr.54.now=C5E727D0
*Sep  1 21:51:46.468: NETCONF: _nefhm.now=C5E727D0
*Sep  1 21:51:46.468: NETCONF: _nefrp.now=C5E727D0
*Sep  1 21:51:46.568: GSI: netconf app _s_ssh.[0xC4DD7840]: <?xml version="1.0" encoding="UTF-8"?><nc:rpc xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:6403fff2-1d0b-434c-b1c7-b09f0edc8aa6"><nc:edit-config><nc:target><nc:running/></nc:target><config>
        <cli-config-data>
            <cmd>interface Ethernet0/0</cmd>
            <cmd>ip address 10.0.0.2 255.255.255.252</cmd>
            <cmd>no shutdown</cmd>
        </cli-config-data>
    </config></nc:edit-config></nc:rpc>
*Sep  1 21:51:46.568: NETCONF: _namn.55.mlc.don=C5CD2140
*Sep  1 21:51:46.568: NETCONF: _narc.55.rcv.ok=C5D134DC
*Sep  1 21:51:46.568: NETCONF: netconf_xml_interpret.55.msg_len=448 bytes
*Sep  1 21:51:46.568: NETCONF: netconf_xml_interpret.55.ok
*Sep  1 21:51:46.568: NETCONF: _nem.55.msg_type=2
*Sep  1 21:51:46.568: NETCONF: _nsv.31.vty=C5794710 usr=cisco priv=15 cvw=0
*Sep  1 21:51:46.568: NETCONF:cli.12.1=[interface Ethernet0/0] prv=PARSE_ERROR_NOERR prc=(SUCCESS) oLen=0 cLen=0.
*Sep  1 21:51:46.579: NETCONF:cli.12.2=[ip address 10.0.0.2 255.255.255.252] prv=PARSE_ERROR_NOERR prc=(SUCCESS) oLen=0 cLen=0.
*Sep  1 21:51:46.579: NETCONF:cli.12.3=[no shutdown] prv=PARSE_ERROR_NOERR prc=(SUCCESS) oLen=0 cLen=0.
*Sep  1 21:51:46.579: NETCONF: _nssd.56.snd.now<?xml version="1.0" encoding="UTF-8"?><rpc-reply message-id="urn:uuid:6403fff2-1d0b-434c-b1c7-b09f0edc8aa6" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><ok /></rpc-reply>
*Sep  1 21:51:46.579: NETCONF: _nssd.56.snd.don=C5D134DC msg=C5D133D4
*Sep  1 21:51:46.579: NETCONF: ne_send.sess=0xC5D134DC
*Sep  1 21:51:46.579: NETCONF: _nem.55.nre_type=3
*Sep  1 21:51:46.579: NETCONF: _nmfr.55.now=C4851C90
*Sep  1 21:51:46.579: NETCONF: _nefom.now=C4851C90
*Sep  1 21:51:46.579: NETCONF: _nefrp.now=C4851C90
*Sep  1 21:51:46.682: GSI: netconf app _s_ssh.[0xC4DD7840]: <?xml version="1.0" encoding="UTF-8"?><nc:rpc xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:617e6a2a-9c54-4b2e-a868-3bc9e1218f31"><nc:close-session/></nc:rpc>

Now to test the code:
My router’s logging console:

--More--
*Sep 1 22:09:03.324: %LINK-3-UPDOWN: Interface Ethernet0/0, changed state to up
*Sep 1 22:09:04.333: %LINEPROTO-5-UPDOWN: Line protocol on Interface Ethernet0/0, changed state to up

See the interface ip address now:

R3#sh ip int brief | in Ethernet0/0
Ethernet0/0 10.0.0.2 YES TFTP up up
R3#

Advertisements
This entry was posted in Python, Scripting and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s