@@ -66,12 +66,19 @@ def get_input_args():
66
66
def print_table (route_list , searched_ip_address ):
67
67
"""Setup and print the SR OS style table"""
68
68
# compute total width of table
69
- cols = [(30 , "Route" ), (20 , "Vprn" ), (20 , "Traffic Type" ), (20 , "Ipv Type" )]
69
+ cols = [
70
+ (30 , "Route" ),
71
+ (20 , "Vprn" ),
72
+ (20 , "Traffic Type" ),
73
+ (20 , "Ipv Type" ),
74
+ ]
70
75
width = sum ([col [0 ] for col in cols ])
71
76
72
77
# init and print table
73
78
table = Table (
74
- "Route List (nexthop: {})" .format (searched_ip_address ), cols , width = width
79
+ "Route List (nexthop: {})" .format (searched_ip_address ),
80
+ cols ,
81
+ width = width ,
75
82
)
76
83
table .print (route_list )
77
84
@@ -106,12 +113,16 @@ def main():
106
113
for if_name in vrtr_state ["interface" ]:
107
114
if_state = vrtr_state ["interface" ][if_name ]
108
115
if keys_exists (if_state , "ipv4" , "primary" , "oper-address" ):
109
- oper_address = if_state ["ipv4" ]["primary" ]["oper-address" ].data
116
+ oper_address = if_state ["ipv4" ]["primary" ][
117
+ "oper-address"
118
+ ].data
110
119
ip_address = ipaddress .IPv4Address (oper_address )
111
120
if_list .append ((if_state ["if-index" ], if_name , ip_address ))
112
121
113
122
# filter interface index list where ipv4 address is searched address
114
- if_index_list = [ifs [0 ].data for ifs in if_list if searched_ip_address == ifs [2 ]]
123
+ if_index_list = [
124
+ ifs [0 ].data for ifs in if_list if searched_ip_address == ifs [2 ]
125
+ ]
115
126
116
127
route_list = []
117
128
# iterate all vprns
@@ -125,21 +136,25 @@ def main():
125
136
if keys_exists (
126
137
vrtr_state , "route-table" , traffic_type , ipv_type , "route"
127
138
):
128
- routes_state = vrtr_state ["route-table" ][traffic_type ][ipv_type ][
129
- "route"
130
- ]
139
+ routes_state = vrtr_state ["route-table" ][traffic_type ][
140
+ ipv_type
141
+ ][ "route" ]
131
142
# iterate all routes
132
143
for route_name in routes_state :
133
144
if "nexthop" in routes_state [route_name ]:
134
145
# iterate all nexthop
135
- for nexthop_id in routes_state [route_name ]["nexthop" ]:
146
+ for nexthop_id in routes_state [route_name ][
147
+ "nexthop"
148
+ ]:
136
149
if keys_exists (
137
- routes_state [route_name ]["nexthop" ][nexthop_id ],
150
+ routes_state [route_name ]["nexthop" ][
151
+ nexthop_id
152
+ ],
138
153
"if-index" ,
139
154
):
140
- nexthop_if_index = routes_state [route_name ][
141
- "nexthop"
142
- ][nexthop_id ]["if-index" ].data
155
+ nexthop_if_index = routes_state [
156
+ route_name
157
+ ]["nexthop" ][ nexthop_id ]["if-index" ].data
143
158
nexthop_if_index = int (nexthop_if_index )
144
159
# add route if nexthop interface in interface index list
145
160
if nexthop_if_index in if_index_list :
@@ -156,13 +171,21 @@ def main():
156
171
if keys_exists (vrtr_state , "static-routes" ):
157
172
for route in vrtr_state ["static-routes" ]["route" ]:
158
173
# if a route with a next-hop exists
159
- if keys_exists (vrtr_state ["static-routes" ]["route" ][route ], "next-hop" ):
174
+ if keys_exists (
175
+ vrtr_state ["static-routes" ]["route" ][route ], "next-hop"
176
+ ):
160
177
# identify whether the address portion of the CIDR address is IPv4 or IPv6
161
- version = ipaddress .ip_address (route [0 ].split ("/" )[0 ]).version
178
+ version = ipaddress .ip_address (
179
+ route [0 ].split ("/" )[0 ]
180
+ ).version
162
181
if version == 4 :
163
- route_list .append ((route [0 ], vrtr_name , route [1 ], "ipv4" ))
182
+ route_list .append (
183
+ (route [0 ], vrtr_name , route [1 ], "ipv4" )
184
+ )
164
185
elif version == 6 :
165
- route_list .append ((route [0 ], vrtr_name , route [1 ], "ipv6" ))
186
+ route_list .append (
187
+ (route [0 ], vrtr_name , route [1 ], "ipv6" )
188
+ )
166
189
else :
167
190
print ("Unknown whether ipv4 or ipv6" )
168
191
sys .exit (- 1 )
@@ -173,8 +196,6 @@ def main():
173
196
# disconnect from router
174
197
connection_object .disconnect ()
175
198
176
- return 0
177
-
178
199
179
200
if __name__ == "__main__" :
180
201
main ()
0 commit comments