forked from CiscoDevNet/ncc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncc-locker.py
executable file
·110 lines (98 loc) · 3.99 KB
/
ncc-locker.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
#
# Copyright (c) 2018 Cisco and/or its affiliates
#
import sys
from argparse import ArgumentParser
from functools import partial
from ncclient import manager
from lxml import etree
import logging
import time
import os
if __name__ == '__main__':
parser = ArgumentParser(description='Select your simple poller parameters:')
# Input parameters
parser.add_argument('--host', type=str,
default=os.environ.get('NCC_HOST', '127.0.0.1'),
help="The IP address for the device to connect to "
"(default localhost)")
parser.add_argument('-u', '--username', type=str,
default=os.environ.get('NCC_USERNAME', 'cisco'),
help="Username to use for SSH authentication "
"(default 'cisco')")
parser.add_argument('-p', '--password', type=str,
default=os.environ.get('NCC_PASSWORD', 'cisco'),
help="Password to use for SSH authentication "
"(default 'cisco')")
parser.add_argument('--port', type=int,
default=os.environ.get('NCC_PORT', 830),
help="Specify this if you want a non-default port "
"(default 830)")
parser.add_argument('-v', '--verbose', action='store_true',
help="Do I really need to explain?")
#
# alternate operations
#
parser.add_argument('--unlock', action='store_true',
help="Instead of locking, unlock the target datastore")
# other options
parser.add_argument('--target', type=str, default='running',
help="Datastore to lock")
parser.add_argument('--blocking', action='store_true',
help="Do a blocking lock")
parser.add_argument('--time', type=int, default=10,
help="Time to lock for")
parser.add_argument('--retries', type=int, default=0,
help="Number of retries, 0 is infinite")
parser.add_argument('--interval', type=float, default=1.0,
help="Time between retries in seconds (float)")
parser.add_argument('--context', action='store_true', default=False,
help="Use context-style locking")
args = parser.parse_args()
if args.verbose:
handler = logging.StreamHandler()
for l in ['ncclient.transport.ssh', 'ncclient.transport.session', 'ncclient.operations.rpc']:
logger = logging.getLogger(l)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
#
# Connect
#
def unknown_host_cb(host, fingerprint):
return True
m = manager.connect(host=args.host,
port=args.port,
username=args.username,
password=args.password,
allow_agent=False,
look_for_keys=False,
hostkey_verify=False,
unknown_host_cb=unknown_host_cb)
#
# for testing, just try and unlock the target
#
if args.unlock:
m.unlock(target=args.target)
print('Unlocked {}'.format(args.target))
m.close_session()
sys.exit(0)
#
# lock and unlock
#
if not args.context:
m.lock(target=args.target, blocking=args.blocking, retries=args.retries, interval=1.0)
print('Locked {}'.format(args.target))
print('Sleeping with lock for {} seconds...'.format(args.time))
time.sleep(args.time)
m.unlock(target=args.target)
print('Unlocked {}'.format(args.target))
else:
with m.locked(args.target, blocking=args.blocking, retries=args.retries, interval=args.interval):
print('Locked {}'.format(args.target))
time.sleep(args.time)
print('Unlocked {}'.format(args.target))
#
# graceful shutdown
#
m.close_session()