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
|
#!/usr/bin/env python3
#
# Copyright (C) 2022 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/>.
#
# Purpose:
# Displays routing table information.
# Used by the "run <ip|ipv6> route *" commands.
import re
import sys
import typing
from jinja2 import Template
import vyos.opmode
frr_command_template = Template("""
{% if family == "inet" %}
show ip route
{% else %}
show ipv6 route
{% endif %}
{% if table %}
table {{table}}
{% endif %}
{% if vrf %}
vrf {{table}}
{% endif %}
{% if tag %}
tag {{tag}}
{% elif net %}
{{net}}
{% elif protocol %}
{{protocol}}
{% endif %}
{% if raw %}
json
{% endif %}
""")
def show_summary(raw: bool, family: str, table: typing.Optional[int]):
from vyos.util import cmd
if family == 'inet':
family_cmd = 'ip'
elif family == 'inet6':
family_cmd = 'ipv6'
else:
raise ValueError(f"Usupported address family {family}")
# Replace with Jinja if it ever starts growing
if table:
table_command = f"table {table}"
else:
table_command = ""
if raw:
from json import loads
output = cmd(f"vtysh -c 'show {family_cmd} route summary {table_command} json'").strip()
# If there are no routes in a table, its "JSON" output is an empty string,
# as of FRR 8.4.1
if output:
return loads(output)
else:
return {}
else:
output = cmd(f"vtysh -c 'show {family_cmd} route summary {table_command}'")
return output
def show(raw: bool,
family: str,
net: typing.Optional[str],
table: typing.Optional[int],
protocol: typing.Optional[str],
vrf: typing.Optional[str],
tag: typing.Optional[str]):
if net and protocol:
raise ValueError("net and protocol are mutually exclusive")
elif table and vrf:
raise ValueError("table and vrf are mutually exclusive")
elif (family == 'inet6') and (protocol == 'rip'):
raise ValueError("rip is not a valid protocol for family inet6")
elif (family == 'inet') and (protocol == 'ripng'):
raise ValueError("rip is not a valid protocol for family inet6")
else:
if (family == 'inet6') and (protocol == 'ospf'):
protocol = 'ospf6'
kwargs = dict(locals())
frr_command = frr_command_template.render(kwargs)
frr_command = re.sub(r'\s+', ' ', frr_command)
from vyos.util import cmd
output = cmd(f"vtysh -c '{frr_command}'")
if raw:
from json import loads
d = loads(output)
collect = []
for k,_ in d.items():
for l in d[k]:
collect.append(l)
return collect
else:
return output
if __name__ == '__main__':
try:
res = vyos.opmode.run(sys.modules[__name__])
if res:
print(res)
except (ValueError, vyos.opmode.Error) as e:
print(e)
sys.exit(1)
|