Revision c9a39aac

/dev/null
1
Very... ad-hoc examples here! More concrete, well-written examples... forthcoming.
/dev/null
1
# importing shows some warning because the Crypto library uses deprecated modules, but that's ok...
2
>>> from ncclient import manager
3
/home/sbhushan/local/lib/python2.6/site-packages/Crypto/Hash/SHA.py:6: DeprecationWarning: the sha module is deprecated; use the hashlib module instead
4
  from sha import *
5
/home/sbhushan/local/lib/python2.6/site-packages/Crypto/Hash/MD5.py:6: DeprecationWarning: the md5 module is deprecated; use hashlib instead
6
  from md5 import *
7

  
8
# i use agent forwarding from my machine to meat; and SSHSession looks for agent's keys unless told not to -- so i need not specify a password
9
>>> m = manager.connect('broccoli', 22, username='sbhushan')
10

  
11
# a copy config
12
>>> reply = m.copy_config(source='running', target='candidate')
13
>>> reply.ok
14
True
15

  
16
# try to make a checkpoint
17
>>> reply = m.copy_config(source='candidate', target='file://checkpoint.conf')
18
Traceback (most recent call last):
19
  File "<stdin>", line 1, in <module>
20
  File "ncclient/manager.py", line 90, in <lambda>
21
    copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds)
22
  File "ncclient/manager.py", line 73, in do
23
    raise reply.error
24
ncclient.operations.rpc.RPCError: syntax error in url element
25
# ok, now fixed by putting another "/" after url scheme
26
>>> reply = m.copy_config(source='candidate', target='file:///checkpoint.conf')
27
>>> reply.ok
28
True
29

  
30
# validation
31
>>> reply = m.validate(source='<config>not a valid config</config>')
32
Traceback (most recent call last):
33
  File "<stdin>", line 1, in <module>
34
  File "ncclient/manager.py", line 93, in <lambda>
35
    validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds)
36
  File "ncclient/manager.py", line 73, in do
37
    raise reply.error
38
ncclient.operations.rpc.RPCError: Unexpected content: 'not a valid config'
39

  
40
# discard and commit...
41
>>> m.discard_changes()
42
<?xml version="1.0" encoding="UTF-8"?>
43
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:11b42210-43bc-11de-b876-00e0812e961e"><ok/></rpc-reply>
44
>>> m.commit().ok
45
True
46

  
47
# locking
48
>>> m.lock('candidate')
49
<?xml version="1.0" encoding="UTF-8"?>
50
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:2caf54cc-43bc-11de-b876-00e0812e961e"><ok/></rpc-reply>
51
>>> m.unlock('candidate')
52
<?xml version="1.0" encoding="UTF-8"?>
53
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:30146760-43bc-11de-b876-00e0812e961e"><ok/></rpc-reply>
54

  
55
# lets try a fake operation, since its a new op we can't use manager
56

  
57
>>> from ncclient.operations import RPC
58
>>> class FakeOp(RPC):
59
...     def request(self):
60
...     return self._request({'tag': 'fake-op'})
61
...
62
>>> reply = FakeOp(m.session).request()
63
>>> reply.error
64
{'error-type': 'rpc', 'error-severity': 'error', 'error-tag': 'unknown-element', 'error-path': '/rpc', 'error-info': '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<netconf:error-info xmlns:netconf="urn:ietf:params:xml:ns:netconf:base:1.0"><netconf:bad-element>fake-op</netconf:bad-element>\n</netconf:error-info>\n'}
65
>>> reply.error.type
66
'rpc'
67
>>> reply.error.severity
68
'error'
69
>>> reply.error.tag
70
'unknown-element'
71
# and so on..
/dev/null
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'
/dev/null
1
from ncclient import manager
2

  
3
import logging
4
logging.basicConfig(level=logging.DEBUG)
5

  
6
with manager.connect('broccoli', 22, username='sbhushan') as m:
7
    with m.locked('candidate'):
8
        reply = m.copy_config(source='running', target='candidate')
9

  
10
print reply
/dev/null
1
from ncclient.transport import SSHSession
2
from ncclient.operations import CloseSession
3
from ncclient.capabilities import CAPABILITIES
4
from ncclient import operations
5

  
6
import logging
7
logging.basicConfig(level=logging.DEBUG)
8

  
9
from ncclient.operations.rpc import RPC
10
class FakeOp(RPC):
11
    def request(self):
12
        return self._request({'tag': 'fake-operation'})
13

  
14
s = SSHSession(CAPABILITIES)
15
#s.add_listener(PrintListener())
16
s.load_known_hosts()
17
s.connect('broccoli', 22, username='sbhushan')
18

  
19
fo = FakeOp(s)
20
fo_reply = fo.request()
21
if not fo_reply.ok:
22
    print 'error dictionary: %r' % fo_reply.error
23
else:
24
    print 'fake op went ok?!'
25
    print fo_reply
26

  
27
go = operations.Get(s)
28
go.request()
29
print 'GET_REPLY', go.reply.data_xml
30

  
31
cs = CloseSession(s)
32
cs_reply = cs.request()
33
print 'closesession ok:', cs_reply.ok

Also available in: Unified diff