-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex1_eapi_interfaces.py
42 lines (33 loc) · 1.26 KB
/
ex1_eapi_interfaces.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
#!/usr/bin/env python
'''
Use Arista's eAPI to obtain 'show interfaces' from the switch. Parse the 'show
interfaces' output to obtain the 'inOctets' and 'outOctets' fields for each of
the interfaces on the switch. Accomplish this using Arista's pyeapi library.
'''
import pyeapi
def pyeapi_result(output):
'''
Return the result value from the pyeapi output
'''
return output[0]['result']
def main():
'''
Use Arista's eAPI to obtain 'show interfaces' from the switch.
'''
eapi_conn = pyeapi.connect_to("pynet-sw3")
interfaces = eapi_conn.enable("show interfaces")
interfaces = pyeapi_result(interfaces)
# strip of dictionary
interfaces = interfaces['interfaces']
# inOctets/outOctets are fields inside 'interfaceCounters' dict
data_stats = {}
for interface, int_values in interfaces.items():
int_counters = int_values.get('interfaceCounters', {})
data_stats[interface] = (int_counters.get('inOctets'), int_counters.get('outOctets'))
# Print output data
print "\n{:20} {:<20} {:<20}".format("Interface", "inOctets", "outOctets")
for intf, octets in sorted(data_stats.items()):
print "\n{:20} {:<20} {:<20}".format(intf, octets[0], octets[1])
print
if __name__ == '__main__':
main()