File: session.ncclient.py

package info (click to toggle)
yuma123 2.14-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,436 kB
  • sloc: ansic: 185,144; cpp: 10,968; python: 7,990; sh: 2,676; makefile: 1,175; xml: 807; exp: 776; perl: 70
file content (55 lines) | stat: -rw-r--r-- 1,559 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
from ncclient import manager
from ncclient.xml_ import *
from ncclient.operations.rpc import RPCError
import time
import sys, os

def main():
	print("""
#Description: Demonstrate that edit-config works.
#Procedure:
#1 - Create interface "foo" and commit. Verify commit succeeds.
#2 - Create interface "bar" and commit. Verify commit fails.
""")

	conn = manager.connect(host="127.0.0.1", port=830, username=os.getenv('USER'), password='admin', look_for_keys=True, timeout=10, device_params = {'name':'junos'}, hostkey_verify=False)
	print("Connected ...")

	edit_rpc = """
<edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <target>
  <candidate/>
 </target>
 <default-operation>merge</default-operation>
 <test-option>set</test-option>
 <config>
  <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
   <interface xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">
     <name>%(interface)s</name>
     <type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
   </interface>
  </interfaces>
 </config>
</edit-config>
"""
	print("edit-config - create 'foo' ...")
	result = conn.rpc(edit_rpc%{'interface':"foo"})
	ok = result.xpath('//ok')
	assert(len(ok)==1)

	commit_rpc = """
<commit/>
"""
	print("commit ...")
	result = conn.rpc(commit_rpc)

	print("edit-config - create 'bar' ...")
	result = conn.rpc(edit_rpc%{'interface':"bar"})

	print("commit ...")
	result = conn.rpc(commit_rpc)
	ok = result.xpath('//ok')
	assert(len(ok)!=1)

sys.exit(main())