forked from nokia/pygnmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygnmi.py
executable file
·154 lines (129 loc) · 7.73 KB
/
pygnmi.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python
##############################################################################
# #
# pygnmi.py #
# #
# History Change Log: #
# #
# 1.0 [JGC] 2018/06/26 first version #
# #
# Objective: #
# #
# Testing tool for the gNMI (GRPC Network Management Interface) in Python #
# #
# Features supported: #
# #
# - gNMI Capabilities #
# - gNMI Subscribe (Based on Nokia SR OS release 16 feature-set) #
# - secure and insecure mode #
# - multiple subscriptions paths #
# #
# Not yet supported: #
# #
# - Disable server name verification against TLS cert (opt: noHostCheck) #
# - Disable cert validation against root certificate (InsecureSkipVerify) #
# - gNMI Get #
# - gNMI Set #
# #
# License: #
# #
# Licensed under the MIT license #
# See LICENSE.md delivered with this project for more information. #
# #
# Author: #
# #
# Sven Wisotzky [SW] #
# mail: sven.wisotzky(at)nokia.com #
# #
# James Cumming [JGC] #
# mail: james.cumming(at)nokia.com #
# #
##############################################################################
"""
gNMI tools in Python Version 1.0
Copyright (C) 2018 Nokia. All Rights Reserved.
"""
__title__ = "pygnmi"
__version__ = "1.0"
__status__ = "dev"
__author__ = "James Cumming"
__date__ = "2018 June 26th"
##############################################################################
import argparse
import re
import sys
import os
import time
import grpc_support
##############################################################################
def get_options():
prog = os.path.splitext(os.path.basename(sys.argv[0]))[0]
parser = argparse.ArgumentParser()
parser.add_argument('--version', action='version', version=prog+' '+__version__)
group = parser.add_mutually_exclusive_group()
group.add_argument('-q', '--quiet', action='store_true', help='disable logging')
group.add_argument('-v', '--verbose', action='count', help='enhanced logging')
group = parser.add_argument_group()
group.add_argument('--server', default='localhost:57400', help='server/port (default: localhost:57400)')
group.add_argument('--username', default='admin', help='username (default: admin)')
group.add_argument('--password', default='admin', help='password (default: admin)')
group.add_argument('--cert', metavar='<filename>', help='CA root certificate')
group.add_argument('--tls', action='store_true', help='enable TLS security')
group.add_argument('--ciphers', help='override environment "GRPC_SSL_CIPHER_SUITES"')
group.add_argument('--altName', help='subjectAltName/CN override for server host validation')
group.add_argument('--noHostCheck', action='store_true', help='disable server host validation')
group = parser.add_argument_group()
group.add_argument('--logfile', metavar='<filename>', type=argparse.FileType('wb', 0), default='-', help='Specify the logfile (default: <stdout>)')
group.add_argument('--stats', action='store_true', help='collect stats')
group.add_argument('--logstash', action='store_true', help='Change subscription output format to be supported by logstash')
group.add_argument('--output', default='raw', help='Output format [raw, xpath]')
group = parser.add_argument_group()
group.add_argument('--service', default='capabilities', help='[capabilities, get, set, subscribe]')
group = parser.add_argument_group()
group.add_argument('--interval', default=10, type=int, help='sample interval (default: 10s)')
group.add_argument('--timeout', type=int, help='subscription duration in seconds (default: none)')
group.add_argument('--heartbeat', type=int, help='heartbeat interval (default: none)')
group.add_argument('--aggregate', action='store_true', help='allow aggregation')
group.add_argument('--suppress', action='store_true', help='suppress redundant')
group.add_argument('--submode', default=2, type=int, help='subscription mode [TARGET_DEFINED, ON_CHANGE, SAMPLE]')
group.add_argument('--mode', default=0, type=int, help='[STREAM, ONCE, POLL]')
group.add_argument('--encoding', default=0, type=int, help='[JSON, BYTES, PROTO, ASCII, JSON_IETF]')
group.add_argument('--qos', default=0, type=int, help='[JSON, BYTES, PROTO, ASCII, JSON_IETF]')
group.add_argument('--use_alias', action='store_true', help='use alias')
group.add_argument('--prefix', default='', help='gRPC path prefix (default: none)')
group.add_argument('xpaths', nargs=argparse.REMAINDER, help='path(s) to subscriber (default: /)')
options = parser.parse_args()
if len(options.xpaths)==0:
options.xpaths=['/']
if options.ciphers:
os.environ["GRPC_SSL_CIPHER_SUITES"] = options.ciphers
return(options,prog)
if __name__ == '__main__':
(options,prog) = get_options()
log = grpc_support.setup_log(options,prog)
channel = grpc_support.create_channel(options,log)
if options.service == "capabilities":
try:
import gNMI_Capabilities
output = gNMI_Capabilities.get_capabilities(channel, options, log)
except Exception as err:
log.error(str(err))
quit()
if options.service == "subscribe":
try:
import gNMI_Subscribe
output = gNMI_Subscribe.subscribe(channel, options, log, prog)
except Exception as err:
log.error(str(err))
quit()
if options.service == "get":
try:
import gNMI_Get
output = gNMI_Get.get(channel, options, log, prog)
if options.output == "xpath":
for n in output.notification:
output = grpc_support.xpath_output(n)
except Exception as err:
log.error(str(err))
quit()
print output