root / examples / misc.py @ bf394ae4
History | View | Annotate | Download (2.2 kB)
1 |
#import logging
|
---|---|
2 |
#logging.basicConfig(level=logging.DEBUG)
|
3 |
|
4 |
from ncclient import manager |
5 |
from ncclient.operations import RPCError |
6 |
|
7 |
m = manager.connect('broccoli', 22, username='sbhushan') |
8 |
|
9 |
# add user
|
10 |
print 'Add user:', |
11 |
config = """<config>
|
12 |
<aaa xmlns="http://tail-f.com/ns/aaa/1.1">
|
13 |
<authentication>
|
14 |
<users>
|
15 |
<user>
|
16 |
<name>testtailf</name>
|
17 |
<uid>9099</uid>
|
18 |
<gid>1</gid>
|
19 |
<password>testtailf</password>
|
20 |
<ssh_keydir/>
|
21 |
<homedir/>
|
22 |
</user>
|
23 |
</users>
|
24 |
</authentication>
|
25 |
</aaa>
|
26 |
</config>"""
|
27 |
try:
|
28 |
m.edit_config(target='candidate', config=config)
|
29 |
except RPCError as e: |
30 |
print 'error:', e |
31 |
else:
|
32 |
print 'OK' |
33 |
|
34 |
# get using xpath filter
|
35 |
print 'Get using XPath filter:', |
36 |
expr = "aaa/authentication/users/user[name='testtailf']"
|
37 |
try:
|
38 |
reply = m.get_config(source='candidate', filter=("xpath", expr)) |
39 |
except RPCError as e: |
40 |
print 'error:', e |
41 |
else:
|
42 |
print reply.data_xml
|
43 |
|
44 |
# get using subtree filter
|
45 |
print 'Get using subtree filter:', |
46 |
criteria = """<aaa xmlns="http://tail-f.com/ns/aaa/1.1">
|
47 |
<authentication>
|
48 |
<users>
|
49 |
<user><name>testtailf</name></user>
|
50 |
</users>
|
51 |
</authentication>
|
52 |
</aaa>"""
|
53 |
try:
|
54 |
reply = m.get_config(source='candidate', filter=("subtree", criteria)) |
55 |
except RPCError as e: |
56 |
print 'error:', e |
57 |
else:
|
58 |
print reply.data_xml
|
59 |
|
60 |
# modify user
|
61 |
print 'Modify user:', |
62 |
config = """<config>
|
63 |
<aaa xmlns="http://tail-f.com/ns/aaa/1.1">
|
64 |
<authentication>
|
65 |
<users>
|
66 |
<user>
|
67 |
<name>testtailf</name>
|
68 |
<uid>9011</uid>
|
69 |
<homedir>abc123</homedir>
|
70 |
</user>
|
71 |
</users>
|
72 |
</authentication>
|
73 |
</aaa>
|
74 |
</config>"""
|
75 |
try:
|
76 |
reply = m.edit_config(target='candidate', config=config)
|
77 |
except RPCError as e: |
78 |
print 'error:', e |
79 |
else:
|
80 |
print 'OK' |
81 |
|
82 |
print 'Deleting user:', |
83 |
config = """<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
|
84 |
<aaa xmlns="http://tail-f.com/ns/aaa/1.1">
|
85 |
<authentication>
|
86 |
<users>
|
87 |
<user xc:operation="delete">
|
88 |
<name>testtailf</name>
|
89 |
</user>
|
90 |
</users>
|
91 |
</authentication>
|
92 |
</aaa>
|
93 |
</config>"""
|
94 |
try:
|
95 |
m.edit_config(target='candidate', config=config)
|
96 |
except RPCError as e: |
97 |
print 'error: ', e |
98 |
else:
|
99 |
print 'OK' |
100 |
|
101 |
print 'Closing session:', |
102 |
try:
|
103 |
m.close_session() |
104 |
except RPCError as e: |
105 |
print 'error', e |
106 |
else:
|
107 |
print 'OK' |