-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjunos.py
executable file
·51 lines (49 loc) · 2.26 KB
/
junos.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
def execute(host):
from netmiko import ConnectHandler
from prettytable import PrettyTable
from modules import ReadableRate
from modules import table_header
import re
interface, neighbor = "", ""
rx, tx = "", ""
table = PrettyTable(['Interface', 'In', 'Out', 'Neighbor', 'Metric', 'Total_Traffic'])
net_connect = ConnectHandler(**host)
isis_int = net_connect.send_command("show isis adjacency | match Up")
isis_int = isis_int.splitlines()
while '' in isis_int:
isis_int.pop(isis_int.index(''))
while '{master}' in isis_int:
isis_int.pop(isis_int.index('{master}'))
for line in isis_int:
total_traffic = 0
# Extract neighbor and interface
for item in line.split():
if re.search("..\-[0-9]+/[0-9]+/[0-9+]", item) is not None or re.search("^ae", item) is not None:
interface = item
neighbor = line.split()[1]
neighbor = neighbor.replace('-re0', '') # Don't care about Junipers with apply-
neighbor = neighbor.replace('-re1', '') # groups to append active RE to hostname
# Get interface traffic
show_int = net_connect.send_command("show interface " + interface.split('.')[0] + ' | match "put rate"')
for lines in show_int.splitlines():
for i, word in enumerate(lines.split()):
if 'bps' in word:
traf = int(lines.split()[i - 1])
total_traffic = total_traffic + traf
if 'nput' in lines:
rx = ReadableRate(traf)
elif "utput" in lines:
tx = ReadableRate(traf)
# Get isis metric
show_isis = net_connect.send_command("show isis interface | match " + interface)
show_isis = show_isis.splitlines()
while '' in show_isis:
show_isis.pop(show_isis.index(''))
for lines in show_isis:
for word in lines .split():
if re.search("[0-9]+/[0-9]+", word) is not None:
metric = word
table.add_row([interface, rx.rate, tx.rate, neighbor, metric, (total_traffic)])
net_connect.disconnect()
table_header(host['ip'])
print(table.get_string(sortby='Total_Traffic', reversesort=True))