forked from achinnac/nanog66-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyops101.py
100 lines (81 loc) · 2.01 KB
/
pyops101.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
import re
SYSLOG = 'syslog.txt'
DATESTAMP_RE = r'(\d+\s+\w+\s+\d+)'
SPACE_RE = r'\s+'
TIMESTAMP_RE = r'(\d+:\d+:\d+)'
DEVICE_RE = r'(\S+)'
ERROR_CODE_RE = r'%(\S+):'
ERROR_MSG_RE = r'(.*)'
SYSLOG_RE = DATESTAMP_RE + SPACE_RE + TIMESTAMP_RE + SPACE_RE + \
DEVICE_RE + SPACE_RE + ERROR_CODE_RE + SPACE_RE + ERROR_MSG_RE
## (\d+\s\w+\s\d+)\s+(\d+:\d+:\d+)\s+(\S+)\s+%(.*):\s+(.*)
with open(SYSLOG, 'r') as f:
log_line = f.readlines()
for line in log_line:
#print line
matched = re.match(SYSLOG_RE, line)
#print matched
if not matched:
continue
datestamp, timestamp, device, error_code, error_msg = matched.groups()
if 'ETHPORT-5-IF_DOWN_INTERFACE_REMOVE' in error_code:
print('Found a known error in {0} -- attempting remediation.'.format(device))
interface = re.match(r'.+(\d+)/\d+', error_msg)
module_name = interface.group(1)
command = ('show module {0}'.format(module_name))
print('--> Sent command {0}'.format(command))
#print('Executing..')
### Progress bar
import time
import sys
def do_task():
time.sleep(1)
def example_1(n):
for i in range(n):
do_task()
print '\b.',
sys.stdout.flush()
print ' Done!'
#print '--> Executing', example_1(10)
###
# import time
# import sys
#
# def do_task():
# time.sleep(1)
#
# def example_1(n):
# steps = n/10
# for i in range(n):
# do_task()
# if i%steps == 0:
# print '\b.',
# sys.stdout.flush()
# print ' Done!'
#
# print 'Starting ',
# sys.stdout.flush()
# example_1(100)
def progress_bar(n):
import sys
import time
print 'Loading..... ',
sys.stdout.flush()
i = 0
while i <= n:
if (i%4) == 0:
sys.stdout.write('\b/')
elif (i%4) == 1:
sys.stdout.write('\b-')
elif (i%4) == 2:
sys.stdout.write('\b\\')
elif (i%4) == 3:
sys.stdout.write('\b|')
sys.stdout.flush()
time.sleep(0.2)
i+=1
print '\b\b done!'
progress_bar(10)
progress_bar(15)
progress_bar(22)
progress_bar(10)