From b1bee9e29ed8f6464e14b577d79a23b5c128ac8a Mon Sep 17 00:00:00 2001 From: bbabich Date: Sat, 4 Nov 2023 22:23:17 +1100 Subject: op-mode: T5658: add VRF support for "monitor traceroute" (cherry picked from commit 07ecc0c33fb32878cac25ec84f2f3a977588f0dd) --- src/op_mode/mtr.py | 342 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 342 insertions(+) create mode 100644 src/op_mode/mtr.py (limited to 'src/op_mode') diff --git a/src/op_mode/mtr.py b/src/op_mode/mtr.py new file mode 100644 index 000000000..759f7078c --- /dev/null +++ b/src/op_mode/mtr.py @@ -0,0 +1,342 @@ +#! /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 . + +import os +import sys +import socket +import ipaddress +from vyos.utils.network import get_all_vrfs +from vyos.ifconfig import Section + + +def interface_list() -> list: + """ + Get list of interfaces in system + :rtype: list + """ + return Section.interfaces() + + +def vrf_list() -> list: + """ + Get list of VRFs in system + :rtype: list + """ + return list(get_all_vrfs().keys()) + + +options = { + 'report': { + '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.' + }, + 'xml': { + 'mtr': '{command} --xml', + 'type': 'noarg', + 'help': 'Use this option to tell mtr to use the xml output format. This format is better suited for automated processing of the measurement results.' + }, + 'curses': { + 'mtr': '{command} --curses', + 'type': 'noarg', + 'help': 'Use this option to force mtr to use the curses based terminal interface (if available). In case the list of hops exceeds the height of your terminal, you can use the + and - keys to scroll up and down half a page.' + }, + 'displaymode': { + 'mtr': '{command} --displaymode', + 'type': '', + 'help': 'Use this option to select the initial display mode: 0 (default) selects statistics, 1 selects the stripchart without latency information, and 2 selects the stripchart with latency information.' + }, + 'raw': { + 'mtr': '{command} --raw', + 'type': 'noarg', + 'help': 'Use the raw output format. This format is better suited for archival of the measurement results.' + }, + 'csv': { + 'mtr': '{command} --csv', + 'type': 'noarg', + 'help': 'Use the Comma-Separated-Value (CSV) output format.' + }, + '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': '', + 'help': 'Use this option to tell mtr to display both the host names and numeric IP numbers.' + }, + 'ipinfo': { + 'mtr': '{command} --ipinfo {value}', + 'type': '', + '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': '', + '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': '', + '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': '', + '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': '', + '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': '', + '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': '', + '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': '', + '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': ' ', + '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': '', + 'help': 'Specifies with what TTL to start. Defaults to 1.' + }, + 'max-ttl': { + 'mtr': '{command} --max-ttl {value}', + 'type': '', + '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': '', + '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': '', + 'help': 'The target port number for TCP/SCTP/UDP traces.' + }, + 'localport': { + 'mtr': '{command} --localport {value}', + 'type': '', + 'help': 'The source port number for UDP traces.' + }, + 'timeout': { + 'mtr': '{command} --timeout {value}', + 'type': '', + 'help': ' The number of seconds to keep probe sockets open before giving up on the connection.' + }, + 'mark': { + 'mtr': '{command} --mark {value}', + 'type': '', + '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': '', + '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('') + 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('') + sys.exit(1) + + +def complete(prefix): + return [o for o in options if o.startswith(prefix)] + + +def convert(command, args): + while args: + shortname = args.first() + longnames = complete(shortname) + if len(longnames) != 1: + expension_failure(shortname, longnames) + longname = longnames[0] + 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 + + +if __name__ == '__main__': + args = List(sys.argv[1:]) + host = args.first() + + if not host: + sys.exit("mtr: Missing host") + + if host == '--get-options': + args.first() # pop ping + 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 = convert(mtr[version], args) + + # print(f'{command} {host}') + os.system(f'{command} {host}') -- cgit v1.2.3 From e769d0d4b504e71ea152a31af28e09dd7bc1e43e Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sun, 12 Nov 2023 10:03:25 +0100 Subject: T5658: add common methods interface_list() and vrf_list() to vyos.utils.network Reduce amount of duplicated (3 times) code in op-mode scripts for ping, traceroute and mtr. (cherry picked from commit 7b27a20c8664460482301cc8d7554048f152485e) --- python/vyos/utils/network.py | 15 +++++++++++++++ src/op_mode/mtr.py | 27 ++++----------------------- src/op_mode/ping.py | 28 +++++----------------------- src/op_mode/traceroute.py | 26 ++++---------------------- 4 files changed, 28 insertions(+), 68 deletions(-) (limited to 'src/op_mode') diff --git a/python/vyos/utils/network.py b/python/vyos/utils/network.py index 157530cc1..3b5e146e1 100644 --- a/python/vyos/utils/network.py +++ b/python/vyos/utils/network.py @@ -194,6 +194,21 @@ def get_all_vrfs(): data[name] = entry return data +def interface_list() -> list: + """ + Get list of interfaces in system + :rtype: list + """ + return Section.interfaces() + + +def vrf_list() -> list: + """ + Get list of VRFs in system + :rtype: list + """ + return list(get_all_vrfs().keys()) + def mac2eui64(mac, prefix=None): """ Convert a MAC address to a EUI64 address or, with prefix provided, a full diff --git a/src/op_mode/mtr.py b/src/op_mode/mtr.py index 759f7078c..57aab5389 100644 --- a/src/op_mode/mtr.py +++ b/src/op_mode/mtr.py @@ -14,29 +14,13 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import os import sys import socket import ipaddress -from vyos.utils.network import get_all_vrfs -from vyos.ifconfig import Section - - -def interface_list() -> list: - """ - Get list of interfaces in system - :rtype: list - """ - return Section.interfaces() - - -def vrf_list() -> list: - """ - Get list of VRFs in system - :rtype: list - """ - return list(get_all_vrfs().keys()) +from vyos.utils.network import interface_list +from vyos.utils.network import vrf_list +from vyos.utils.process import call options = { 'report': { @@ -214,7 +198,6 @@ mtr = { 6: '/bin/mtr -6', } - class List(list): def first(self): return self.pop(0) if self else '' @@ -337,6 +320,4 @@ if __name__ == '__main__': sys.exit(f'mtr: Unknown host: {host}') command = convert(mtr[version], args) - - # print(f'{command} {host}') - os.system(f'{command} {host}') + call(f'{command} --curses --displaymode 0 {host}') diff --git a/src/op_mode/ping.py b/src/op_mode/ping.py index f1d87a118..583d8792c 100755 --- a/src/op_mode/ping.py +++ b/src/op_mode/ping.py @@ -1,6 +1,6 @@ #! /usr/bin/env python3 -# Copyright (C) 2020 VyOS maintainers and contributors +# 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 @@ -14,29 +14,13 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import os import sys import socket import ipaddress -from vyos.utils.network import get_all_vrfs -from vyos.ifconfig import Section - - -def interface_list() -> list: - """ - Get list of interfaces in system - :rtype: list - """ - return Section.interfaces() - - -def vrf_list() -> list: - """ - Get list of VRFs in system - :rtype: list - """ - return list(get_all_vrfs().keys()) +from vyos.utils.network import interface_list +from vyos.utils.network import vrf_list +from vyos.utils.process import call options = { 'audible': { @@ -295,6 +279,4 @@ if __name__ == '__main__': sys.exit(f'ping: Unknown host: {host}') command = convert(ping[version], args) - - # print(f'{command} {host}') - os.system(f'{command} {host}') + call(f'{command} {host}') diff --git a/src/op_mode/traceroute.py b/src/op_mode/traceroute.py index 2f0edf53a..d2bac3f7c 100755 --- a/src/op_mode/traceroute.py +++ b/src/op_mode/traceroute.py @@ -14,29 +14,13 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import os import sys import socket import ipaddress -from vyos.utils.network import get_all_vrfs -from vyos.ifconfig import Section - - -def interface_list() -> list: - """ - Get list of interfaces in system - :rtype: list - """ - return Section.interfaces() - - -def vrf_list() -> list: - """ - Get list of VRFs in system - :rtype: list - """ - return list(get_all_vrfs().keys()) +from vyos.utils.network import interface_list +from vyos.utils.network import vrf_list +from vyos.utils.process import call options = { 'backward-hops': { @@ -251,6 +235,4 @@ if __name__ == '__main__': sys.exit(f'traceroute: Unknown host: {host}') command = convert(traceroute[version], args) - - # print(f'{command} {host}') - os.system(f'{command} {host}') + call(f'{command} {host}') -- cgit v1.2.3 From cc1df006731050ca68bb1ea1619864f85cee9cdb Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sun, 12 Nov 2023 10:06:04 +0100 Subject: op-mode: T5658: reduce amount of exposed options Example: we should focus on JSON output and not expose XML and CSV. (cherry picked from commit b8e9daf12eaef46747e7379042f8acd575e5b1d6) --- src/op_mode/mtr.py | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) (limited to 'src/op_mode') diff --git a/src/op_mode/mtr.py b/src/op_mode/mtr.py index 57aab5389..efb627e51 100644 --- a/src/op_mode/mtr.py +++ b/src/op_mode/mtr.py @@ -33,31 +33,11 @@ options = { 'type': 'noarg', 'help': 'This option puts mtr into wide report mode. When in this mode, mtr will not cut hostnames in the report.' }, - 'xml': { - 'mtr': '{command} --xml', - 'type': 'noarg', - 'help': 'Use this option to tell mtr to use the xml output format. This format is better suited for automated processing of the measurement results.' - }, - 'curses': { - 'mtr': '{command} --curses', - 'type': 'noarg', - 'help': 'Use this option to force mtr to use the curses based terminal interface (if available). In case the list of hops exceeds the height of your terminal, you can use the + and - keys to scroll up and down half a page.' - }, - 'displaymode': { - 'mtr': '{command} --displaymode', - 'type': '', - 'help': 'Use this option to select the initial display mode: 0 (default) selects statistics, 1 selects the stripchart without latency information, and 2 selects the stripchart with latency information.' - }, 'raw': { 'mtr': '{command} --raw', 'type': 'noarg', 'help': 'Use the raw output format. This format is better suited for archival of the measurement results.' }, - 'csv': { - 'mtr': '{command} --csv', - 'type': 'noarg', - 'help': 'Use the Comma-Separated-Value (CSV) output format.' - }, 'json': { 'mtr': '{command} --json', 'type': 'noarg', @@ -86,7 +66,7 @@ options = { 'aslookup': { 'mtr': '{command} --aslookup', 'type': 'noarg', - 'help': 'Displays the Autonomous System (AS) number alongside each hop. Equivalent to --ipinfo 0.' + 'help': 'Displays the Autonomous System (AS) number alongside each hop. Equivalent to --ipinfo 0.' }, 'interval': { 'mtr': '{command} --interval {value}', @@ -101,12 +81,12 @@ options = { 'psize': { 'mtr': '{command} --psize {value}', 'type': '', - '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.' + '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': '', - '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.' + '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}', @@ -116,7 +96,7 @@ options = { 'tos': { 'mtr': '{command} --tos {value}', 'type': '', - 'help': 'Specifies value for type of service field in IP header. Should be within range 0 - 255.' + 'help': 'Specifies value for type of service field in IP header. Should be within range 0 - 255.' }, 'mpls': { 'mtr': '{command} --mpls {value}', @@ -182,7 +162,7 @@ options = { 'mark': { 'mtr': '{command} --mark {value}', 'type': '', - '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.' + '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}', -- cgit v1.2.3 From 21e4fddeebe541856e25a398018854fb0cf6fbd9 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sun, 12 Nov 2023 10:10:01 +0100 Subject: op-mode: T5658: fix "monitor traceroute" completion helper (cherry picked from commit c0de93d37354ec89f44dde7f1b5a4c8af550a019) --- op-mode-definitions/monitor-traceroute.xml.in | 30 --------------------------- op-mode-definitions/mtr.xml.in | 26 ++++++++++++++++++++++- src/op_mode/mtr.py | 7 +++++-- 3 files changed, 30 insertions(+), 33 deletions(-) delete mode 100644 op-mode-definitions/monitor-traceroute.xml.in (limited to 'src/op_mode') diff --git a/op-mode-definitions/monitor-traceroute.xml.in b/op-mode-definitions/monitor-traceroute.xml.in deleted file mode 100644 index 7697228b3..000000000 --- a/op-mode-definitions/monitor-traceroute.xml.in +++ /dev/null @@ -1,30 +0,0 @@ - - - - - Monitor route with mtr - - - - - Traceroute route with mtr - - <hostname> <x.x.x.x> <h:h:h:h:h:h:h:h> - - - ${vyos_op_scripts_dir}/mtr.py ${@:3} - - - - mtr options - - - - - ${vyos_op_scripts_dir}/mtr.py ${@:3} - - - - - - diff --git a/op-mode-definitions/mtr.xml.in b/op-mode-definitions/mtr.xml.in index acc4ee29d..8239aec4c 100644 --- a/op-mode-definitions/mtr.xml.in +++ b/op-mode-definitions/mtr.xml.in @@ -1,8 +1,32 @@ + + + + + Monitor Traceroute and ping path to target + + <hostname> <x.x.x.x> <h:h:h:h:h:h:h:h> + + + ${vyos_op_scripts_dir}/mtr.py ${@:3} + + + + mtr options + + + + + ${vyos_op_scripts_dir}/mtr.py ${@:3} + + + + + - Trace network path to node with mtr + Monitor Traceroute and ping path to target <hostname> <x.x.x.x> <h:h:h:h:h:h:h:h> diff --git a/src/op_mode/mtr.py b/src/op_mode/mtr.py index efb627e51..de139f2fa 100644 --- a/src/op_mode/mtr.py +++ b/src/op_mode/mtr.py @@ -242,8 +242,11 @@ if __name__ == '__main__': if not host: sys.exit("mtr: Missing host") - if host == '--get-options': - args.first() # pop ping + + 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: -- cgit v1.2.3