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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
#! /usr/bin/env python3
# Copyright (C) 2023 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import socket
import ipaddress
from vyos.utils.network import interface_list
from vyos.utils.network import vrf_list
from vyos.utils.process import call
options = {
'report-mode': {
'mtr': '{command} --report',
'type': 'noarg',
'help': 'This option puts mtr into report mode. When in this mode, mtr will run for the number of cycles specified by the -c option, and then print statistics and exit.'
},
'report-wide': {
'mtr': '{command} --report-wide',
'type': 'noarg',
'help': 'This option puts mtr into wide report mode. When in this mode, mtr will not cut hostnames in the report.'
},
'raw': {
'mtr': '{command} --raw',
'type': 'noarg',
'help': 'Use the raw output format. This format is better suited for archival of the measurement results.'
},
'json': {
'mtr': '{command} --json',
'type': 'noarg',
'help': 'Use this option to tell mtr to use the JSON output format.'
},
'split': {
'mtr': '{command} --split',
'type': 'noarg',
'help': 'Use this option to set mtr to spit out a format that is suitable for a split-user interface.'
},
'no-dns': {
'mtr': '{command} --no-dns',
'type': 'noarg',
'help': 'Use this option to force mtr to display numeric IP numbers and not try to resolve the host names.'
},
'show-ips': {
'mtr': '{command} --show-ips {value}',
'type': '<num>',
'help': 'Use this option to tell mtr to display both the host names and numeric IP numbers.'
},
'ipinfo': {
'mtr': '{command} --ipinfo {value}',
'type': '<num>',
'help': 'Displays information about each IP hop.'
},
'aslookup': {
'mtr': '{command} --aslookup',
'type': 'noarg',
'help': 'Displays the Autonomous System (AS) number alongside each hop. Equivalent to --ipinfo 0.'
},
'interval': {
'mtr': '{command} --interval {value}',
'type': '<num>',
'help': 'Use this option to specify the positive number of seconds between ICMP ECHO requests. The default value for this parameter is one second. The root user may choose values between zero and one.'
},
'report-cycles': {
'mtr': '{command} --report-cycles {value}',
'type': '<num>',
'help': 'Use this option to set the number of pings sent to determine both the machines on the network and the reliability of those machines. Each cycle lasts one second.'
},
'psize': {
'mtr': '{command} --psize {value}',
'type': '<num>',
'help': 'This option sets the packet size used for probing. It is in bytes, inclusive IP and ICMP headers. If set to a negative number, every iteration will use a different, random packet size up to that number.'
},
'bitpattern': {
'mtr': '{command} --bitpattern {value}',
'type': '<num>',
'help': 'Specifies bit pattern to use in payload. Should be within range 0 - 255. If NUM is greater than 255, a random pattern is used.'
},
'gracetime': {
'mtr': '{command} --gracetime {value}',
'type': '<num>',
'help': 'Use this option to specify the positive number of seconds to wait for responses after the final request. The default value is five seconds.'
},
'tos': {
'mtr': '{command} --tos {value}',
'type': '<tos>',
'help': 'Specifies value for type of service field in IP header. Should be within range 0 - 255.'
},
'mpls': {
'mtr': '{command} --mpls {value}',
'type': 'noarg',
'help': 'Use this option to tell mtr to display information from ICMP extensions for MPLS (RFC 4950) that are encoded in the response packets.'
},
'interface': {
'mtr': '{command} --interface {value}',
'type': '<interface>',
'helpfunction': interface_list,
'help': 'Use the network interface with a specific name for sending network probes. This can be useful when you have multiple network interfaces with routes to your destination, for example both wired Ethernet and WiFi, and wish to test a particular interface.'
},
'address': {
'mtr': '{command} --address {value}',
'type': '<x.x.x.x> <h:h:h:h:h:h:h:h>',
'help': 'Use this option to bind the outgoing socket to ADDRESS, so that all packets will be sent with ADDRESS as source address.'
},
'first-ttl': {
'mtr': '{command} --first-ttl {value}',
'type': '<num>',
'help': 'Specifies with what TTL to start. Defaults to 1.'
},
'max-ttl': {
'mtr': '{command} --max-ttl {value}',
'type': '<num>',
'help': 'Specifies the maximum number of hops or max time-to-live value mtr will probe. Default is 30.'
},
'max-unknown': {
'mtr': '{command} --max-unknown {value}',
'type': '<num>',
'help': 'Specifies the maximum unknown host. Default is 5.'
},
'udp': {
'mtr': '{command} --udp',
'type': 'noarg',
'help': 'Use UDP datagrams instead of ICMP ECHO.'
},
'tcp': {
'mtr': '{command} --tcp',
'type': 'noarg',
'help': ' Use TCP SYN packets instead of ICMP ECHO. PACKETSIZE is ignored, since SYN packets can not contain data.'
},
'sctp': {
'mtr': '{command} --sctp',
'type': 'noarg',
'help': 'Use Stream Control Transmission Protocol packets instead of ICMP ECHO.'
},
'port': {
'mtr': '{command} --port {value}',
'type': '<port>',
'help': 'The target port number for TCP/SCTP/UDP traces.'
},
'localport': {
'mtr': '{command} --localport {value}',
'type': '<port>',
'help': 'The source port number for UDP traces.'
},
'timeout': {
'mtr': '{command} --timeout {value}',
'type': '<num>',
'help': ' The number of seconds to keep probe sockets open before giving up on the connection.'
},
'mark': {
'mtr': '{command} --mark {value}',
'type': '<num>',
'help': ' Set the mark for each packet sent through this socket similar to the netfilter MARK target but socket-based. MARK is 32 unsigned integer.'
},
'vrf': {
'mtr': 'sudo ip vrf exec {value} {command}',
'type': '<vrf>',
'help': 'Use specified VRF table',
'helpfunction': vrf_list,
'dflt': 'default'
}
}
mtr = {
4: '/bin/mtr -4',
6: '/bin/mtr -6',
}
class List(list):
def first(self):
return self.pop(0) if self else ''
def last(self):
return self.pop() if self else ''
def prepend(self, value):
self.insert(0, value)
def completion_failure(option: str) -> None:
"""
Shows failure message after TAB when option is wrong
:param option: failure option
:type str:
"""
sys.stderr.write('\n\n Invalid option: {}\n\n'.format(option))
sys.stdout.write('<nocomps>')
sys.exit(1)
def expension_failure(option, completions):
reason = 'Ambiguous' if completions else 'Invalid'
sys.stderr.write(
'\n\n {} command: {} [{}]\n\n'.format(reason, ' '.join(sys.argv),
option))
if completions:
sys.stderr.write(' Possible completions:\n ')
sys.stderr.write('\n '.join(completions))
sys.stderr.write('\n')
sys.stdout.write('<nocomps>')
sys.exit(1)
def complete(prefix):
return [o for o in options if o.startswith(prefix)]
def convert(command, args):
to_json = False
while args:
shortname = args.first()
longnames = complete(shortname)
if len(longnames) != 1:
expension_failure(shortname, longnames)
longname = longnames[0]
if longname == 'json':
to_json = True
if options[longname]['type'] == 'noarg':
command = options[longname]['mtr'].format(
command=command, value='')
elif not args:
sys.exit(f'mtr: missing argument for {longname} option')
else:
command = options[longname]['mtr'].format(
command=command, value=args.first())
return command, to_json
if __name__ == '__main__':
args = List(sys.argv[1:])
host = args.first()
if not host:
sys.exit("mtr: Missing host")
if host == '--get-options' or host == '--get-options-nested':
if host == '--get-options-nested':
args.first() # pop monitor
args.first() # pop mtr | traceroute
args.first() # pop IP
usedoptionslist = []
while args:
option = args.first() # pop option
matched = complete(option) # get option parameters
usedoptionslist.append(option) # list of used options
# Select options
if not args:
# remove from Possible completions used options
for o in usedoptionslist:
if o in matched:
matched.remove(o)
sys.stdout.write(' '.join(matched))
sys.exit(0)
if len(matched) > 1:
sys.stdout.write(' '.join(matched))
sys.exit(0)
# If option doesn't have value
if matched:
if options[matched[0]]['type'] == 'noarg':
continue
else:
# Unexpected option
completion_failure(option)
value = args.first() # pop option's value
if not args:
matched = complete(option)
helplines = options[matched[0]]['type']
# Run helpfunction to get list of possible values
if 'helpfunction' in options[matched[0]]:
result = options[matched[0]]['helpfunction']()
if result:
helplines = '\n' + ' '.join(result)
sys.stdout.write(helplines)
sys.exit(0)
for name, option in options.items():
if 'dflt' in option and name not in args:
args.append(name)
args.append(option['dflt'])
try:
ip = socket.gethostbyname(host)
except UnicodeError:
sys.exit(f'mtr: Unknown host: {host}')
except socket.gaierror:
ip = host
try:
version = ipaddress.ip_address(ip).version
except ValueError:
sys.exit(f'mtr: Unknown host: {host}')
command, to_json = convert(mtr[version], args)
if to_json:
call(f'{command} {host}')
else:
call(f'{command} --curses --displaymode 0 {host}')
|