-
Notifications
You must be signed in to change notification settings - Fork 448
/
tdr-test.py
99 lines (84 loc) · 3.3 KB
/
tdr-test.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
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
#!/usr/bin/env python
# importing the cli module is necessary to run
# config or exec commands on the host.
import cli
# importing the time module works well in
# this script, as we are pacing some of the execution.
import time
# importing the time module works well in this script, as we are pacing
# some of the execution.
o1 = cli.execute('show ip int brief | include Ethernet1/0/.*up');
# Create this thing called o1. Make it be equal to the command above.
# This is also proper use of the cli.execute API. As you can see,
# this is CLI, taking advantage of regexp # matching. Essentially,
# we are running this command to get a list of all the interfaces
# that are UP on the host!
# Create this thing called intfs. Make it a Python dictionary.
intfs = dict()
# Create a function to grab the list of interfaces from passed in data.
def grab_intf(i):
# use the global intfs variable defined outside of the function.
global intfs
# If the passed in data is empty exit the function.
if i == "":
return
# Take the first column of space separated data
j = (i.split(' '))[0]
# Assign the interface name as a key value in the intfs dictionary.
intfs[j] = ""
# Take the o1 data, split it on each line,
for i in o1.split('\n'):
# and call the grab_intf function on each line.
grab_intf(i)
# Run through our interface list.
for i in intfs:
# While we run through our interface list, each time we iterate, create
# this thing called cmd, and set it equal to the output of the command.
cmd = "test cable-diagnostics tdr interface " + i
# And each time we do, create this thing called o2, and set it
# equal to the execution of each command for each interface on the host.
o2 = cli.execute(cmd);
# Create this thing called done and set it to a value of False. Essentially,
# we're not done!
done = False
# Create a loop condition for when we're not done.
while done == False:
# Remember our list of interfaces? Run through them again.
for i in intfs:
# If we already have data for an interface, then skip
if intfs[i] != "":
continue
# Try to get data for the interface we are working on.
# Once again, create this thing called cmd, and set it equal to the
# iterative exec command for all of our interfaces.
cmd = "show cable-diagnostics tdr interface " + i
# And create this thing called o2, and execute each CLI.
o2 = cli.execute(cmd);
# Parse the data we get. We are making sure the TDR test runs, and
# also completes.
if "Not Completed" in o2:
continue
else :
# We got valid data, set it into the dictionary value for the
# specific interface we are working on.
intfs[i] = o2
time.sleep(2)
# now loop again looking to see if we are all done
found_one = False
for i in intfs:
if intfs[i] == "":
found_one = True;
if found_one == False:
done = True
# We are done gathering now just print output
target = open('/bootflash/myoutputfile', 'w')
target.truncate()
for i in intfs:
title = "Interfaces: " + i
print(title)
target.write(title)
print(intfs[i])
target.write(intfs[i])
print("\n\n")
target.write("\n\n")
target.close()