Revision 91c5e202
b/examples/nc01.py | ||
---|---|---|
1 |
#! /usr/bin/env python2.6 |
|
2 |
# |
|
3 |
# Connect to the NETCONF server passed on the command line and |
|
4 |
# display their capabilities. This script and the following scripts |
|
5 |
# all assume that the user calling the script is known by the server |
|
6 |
# and that suitable SSH keys are in place. For brevity and clarity |
|
7 |
# of the examples, we omit proper exception handling. |
|
8 |
# |
|
9 |
# $ ./nc01.py broccoli |
|
10 |
|
|
11 |
import sys, os, warnings |
|
12 |
warnings.simplefilter("ignore", DeprecationWarning) |
|
13 |
from ncclient import manager |
|
14 |
|
|
15 |
def demo(host, user): |
|
16 |
with manager.connect(host=host, port=22, username=user) as m: |
|
17 |
for c in m.server_capabilities: |
|
18 |
print c |
|
19 |
|
|
20 |
if __name__ == '__main__': |
|
21 |
demo(sys.argv[1], os.getenv("USER")) |
b/examples/nc02.py | ||
---|---|---|
1 |
#! /usr/bin/env python2.6 |
|
2 |
# |
|
3 |
# Retrieve the running config from the NETCONF server passed on the |
|
4 |
# command line using get-config and write the XML configs to files. |
|
5 |
# |
|
6 |
# $ ./nc02.py broccoli |
|
7 |
|
|
8 |
import sys, os, warnings |
|
9 |
warnings.simplefilter("ignore", DeprecationWarning) |
|
10 |
from ncclient import manager |
|
11 |
|
|
12 |
def demo(host, user): |
|
13 |
with manager.connect(host=host, port=22, username=user) as m: |
|
14 |
c = m.get_config(source='running').data_xml |
|
15 |
with open("%s.xml" % host, 'w') as f: |
|
16 |
f.write(c) |
|
17 |
|
|
18 |
if __name__ == '__main__': |
|
19 |
demo(sys.argv[1], os.getenv("USER")) |
b/examples/nc03.py | ||
---|---|---|
1 |
#! /usr/bin/env python2.6 |
|
2 |
# |
|
3 |
# Retrieve a portion selected by an XPATH expression from the running |
|
4 |
# config from the NETCONF server passed on the command line using |
|
5 |
# get-config and write the XML configs to files. |
|
6 |
# |
|
7 |
# $ ./nc03.py broccoli "aaa/authentication/users/user[name='schoenw']" |
|
8 |
|
|
9 |
import sys, os, warnings |
|
10 |
warnings.simplefilter("ignore", DeprecationWarning) |
|
11 |
from ncclient import manager |
|
12 |
|
|
13 |
def demo(host, user, expr): |
|
14 |
with manager.connect(host=host, port=22, username=user) as m: |
|
15 |
assert(":xpath" in m.server_capabilities) |
|
16 |
c = m.get_config(source='running', filter=('xpath', expr)).data_xml |
|
17 |
with open("%s.xml" % host, 'w') as f: |
|
18 |
f.write(c) |
|
19 |
|
|
20 |
if __name__ == '__main__': |
|
21 |
demo(sys.argv[1], os.getenv("USER"), sys.argv[2]) |
b/examples/nc04.py | ||
---|---|---|
1 |
#! /usr/bin/env python2.6 |
|
2 |
# |
|
3 |
# Create a new user to the running configuration using edit-config |
|
4 |
# and the test-option provided by the :validate capability. |
|
5 |
# |
|
6 |
# $ ./nc04.py broccoli bob 42 42 |
|
7 |
|
|
8 |
import sys, os, warnings |
|
9 |
warnings.simplefilter("ignore", DeprecationWarning) |
|
10 |
from ncclient import manager |
|
11 |
|
|
12 |
def demo(host, user, name, uid, gid): |
|
13 |
snippet = """<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
|
14 |
<aaa xmlns="http://tail-f.com/ns/aaa/1.1"> |
|
15 |
<authentication> <users> <user xc:operation="create"> |
|
16 |
<name>%s</name> <uid>%s</uid> <gid>%s</gid> |
|
17 |
<password>*</password> <ssh_keydir/> <homedir/> |
|
18 |
</user></users></authentication></aaa></config>""" % (name, uid, gid) |
|
19 |
|
|
20 |
with manager.connect(host=host, port=22, username=user) as m: |
|
21 |
assert(":validate" in m.server_capabilities) |
|
22 |
m.edit_config(target='running', config=snippet, |
|
23 |
test_option='test-then-set') |
|
24 |
|
|
25 |
if __name__ == '__main__': |
|
26 |
demo(sys.argv[1], os.getenv("USER"), sys.argv[2], sys.argv[3], sys.argv[4]) |
b/examples/nc05.py | ||
---|---|---|
1 |
#! /usr/bin/env python2.6 |
|
2 |
# |
|
3 |
# Delete an existing user from the running configuration using |
|
4 |
# edit-config and the test-option provided by the :validate |
|
5 |
# capability. |
|
6 |
# |
|
7 |
# $ ./nc05.py broccoli bob |
|
8 |
|
|
9 |
import sys, os, warnings |
|
10 |
warnings.simplefilter("ignore", DeprecationWarning) |
|
11 |
from ncclient import manager |
|
12 |
|
|
13 |
def demo(host, user, name): |
|
14 |
snippet = """<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
|
15 |
<aaa xmlns="http://tail-f.com/ns/aaa/1.1"> |
|
16 |
<authentication> <users> <user xc:operation="delete"> |
|
17 |
<name>%s</name> |
|
18 |
</user></users></authentication></aaa></config>""" % name |
|
19 |
|
|
20 |
with manager.connect(host=host, port=22, username=user) as m: |
|
21 |
assert(":validate" in m.server_capabilities) |
|
22 |
m.edit_config(target='running', config=snippet, |
|
23 |
test_option='test-then-set') |
|
24 |
|
|
25 |
if __name__ == '__main__': |
|
26 |
demo(sys.argv[1], os.getenv("USER"), sys.argv[2]) |
b/examples/nc06.py | ||
---|---|---|
1 |
#! /usr/bin/env python2.6 |
|
2 |
# |
|
3 |
# Delete a list of existing users from the running configuration using |
|
4 |
# edit-config; protect the transaction using a lock. |
|
5 |
# |
|
6 |
# $ ./nc06.py broccoli bob alice |
|
7 |
|
|
8 |
import sys, os, warnings |
|
9 |
warnings.simplefilter("ignore", DeprecationWarning) |
|
10 |
from ncclient import manager |
|
11 |
|
|
12 |
template = """<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
|
13 |
<aaa xmlns="http://tail-f.com/ns/aaa/1.1"> |
|
14 |
<authentication> <users> <user xc:operation="delete"> |
|
15 |
<name>%s</name> </user></users></authentication></aaa></config>""" |
|
16 |
|
|
17 |
def demo(host, user, names): |
|
18 |
with manager.connect(host=host, port=22, username=user) as m: |
|
19 |
with m.locked(target='running'): |
|
20 |
for n in names: |
|
21 |
m.edit_config(target='running', config=template % n) |
|
22 |
|
|
23 |
if __name__ == '__main__': |
|
24 |
demo(sys.argv[1], os.getenv("USER"), sys.argv[2:]) |
b/examples/nc07.py | ||
---|---|---|
1 |
#! /usr/bin/env python2.6 |
|
2 |
# |
|
3 |
# Delete a list of existing users from the running configuration using |
|
4 |
# edit-config and the candidate datastore protected by a lock. |
|
5 |
# |
|
6 |
# $ ./nc07.py broccoli bob alice |
|
7 |
|
|
8 |
import sys, os, warnings |
|
9 |
warnings.simplefilter("ignore", DeprecationWarning) |
|
10 |
from ncclient import manager |
|
11 |
|
|
12 |
template = """<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
|
13 |
<aaa xmlns="http://tail-f.com/ns/aaa/1.1"> |
|
14 |
<authentication> <users> <user xc:operation="delete"> |
|
15 |
<name>%s</name> </user></users></authentication></aaa></config>""" |
|
16 |
|
|
17 |
def demo(host, user, names): |
|
18 |
with manager.connect(host=host, port=22, username=user) as m: |
|
19 |
assert(":candidate" in m.server_capabilities) |
|
20 |
with m.locked(target='candidate'): |
|
21 |
m.discard_changes() |
|
22 |
for n in names: |
|
23 |
m.edit_config(target='candidate', config=template % n) |
|
24 |
m.commit() |
|
25 |
|
|
26 |
if __name__ == '__main__': |
|
27 |
demo(sys.argv[1], os.getenv("USER"), sys.argv[2:]) |
Also available in: Unified diff