-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcisco_color.c
288 lines (217 loc) · 11.7 KB
/
cisco_color.c
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Copyright 2011 Thomas St.Pierre. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY Thomas St.Pierre ''AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Thomas St.Pierre OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Thomas St.Pierre.
*/
#include <stdio.h>
#include <regex.h>
#include <string.h>
#define MAXMATCH 20
/* Terminal Color codes */
#define TERM_BRIGHT_GREEN "\033[1m"
#define TERM_GREEN "\033[1;32m"
#define TERM_BOLD_RED "\033[1;31m"
#define TERM_RED "\033[31m"
#define TERM_PURPLE "\033[35m"
#define TERM_BOLD_PURPLE "\033[1;35m"
#define TERM_BRIGHT_YELLOW "\033[1;33m"
#define TERM_YELLOW "\033[33m"
#define TERM_WHITE "\033[37m"
#define TERM_BLUE "\033[34m"
#define TERM_LIGHT_BLUE "\033[1;34m"
#define TERM_CYAN "\033[36m"
#define TERM_BG_GREEN "\033[42m"
#define TERM_WARNING "\033[41m\033[1m"
#define TERM_NONE "\033[0m"
/*
Define our rules!
Rules Are defined as a struct, first variable is a regex. The entire line is used to match
context of what you want to color. Within the regex you use sub-expressions to define
what to color. For example with the regex "This is (some) context", it will match the
entire line, but only the word 'some' will be colored. Second variable is an array which
contains strings that will be inserted right before every matched sub-expression. (usually
terminal escapes codes, but could be any string)
Last variable is used internally to store compiled regex's
*/
struct color_match{
const char *pattern;
const char *insert_before[MAXMATCH];
regex_t compiled;
} patterns [] = {
/* Logs */
{"^\\*?([A-Z][a-z]{2}[ 0-9]{3}[0-9 ]{0,5} [0-9]{1,2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3} [A-Z]{3}): (%[A-Z0-9_-]+:) (.*)$", {TERM_CYAN, TERM_RED, TERM_BLUE}},
/* Interface Name */
{"^(interface) ([a-zA-Z-]+)([0-9./]+)",{TERM_YELLOW, TERM_CYAN, TERM_BOLD_PURPLE}},
/* Interface parameters */
{"^ (description) (.*)$", {TERM_YELLOW, TERM_BLUE}},
{"^ (ip address) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) (secondary)",
{TERM_YELLOW, TERM_PURPLE, TERM_PURPLE, TERM_BRIGHT_GREEN}},
{"^ (ip address) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}).$",
{TERM_YELLOW, TERM_PURPLE, TERM_PURPLE}},
{"^ (ip vrf forwarding) ([a-zA-Z0-9_-]+).$", {TERM_YELLOW,TERM_RED}},
{"^ (ip nat) (inside|outside).$", {TERM_YELLOW,TERM_RED}},
{"^ (encapsulation dot1Q) ([0-9]{1,4}).$", {TERM_YELLOW, TERM_PURPLE }},
{"^ (shutdown).$", {TERM_RED}},
/* Interface Status */
{"is (up), line protocol is (down)", {TERM_BRIGHT_GREEN, TERM_BOLD_RED}},
{"is (down), line protocol is (down)", {TERM_BOLD_RED, TERM_BOLD_RED}},
{"is (up), line protocol is (up)", {TERM_BRIGHT_GREEN, TERM_BRIGHT_GREEN}},
{"is (administratively down), line protocol is (down)", {TERM_YELLOW, TERM_YELLOW}},
{"is ([a-z]+), line protocol is ([a-z]+)", {TERM_WARNING, TERM_WARNING}},
/* Interface speeds */
{"^ [0-9]{1,2} [a-z]{6} [a-z]{5,6} rate ([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3}) bits.*$", {TERM_PURPLE, TERM_CYAN, TERM_BLUE, TERM_CYAN}},
{"^ [0-9]{1,2} [a-z]{6} [a-z]{5,6} rate ([0-9]{1,3})([0-9]{3})([0-9]{3}) bits.*$", {TERM_CYAN, TERM_BLUE, TERM_CYAN}},
{"^ [0-9]{1,2} [a-z]{6} [a-z]{5,6} rate ([0-9]{1,3})([0-9]{3}) bits.*$", {TERM_BLUE, TERM_CYAN}},
{"^ [0-9]{1,2} [a-z]{6} [a-z]{5,6} rate ([0-9]{1,3}) bits.*$", {TERM_CYAN}},
/* Hostname / config prompts */
{"^([a-zA-Z0-9\\._\\-]*)(\\(config)(-[a-z_-]+)(\\))#",{TERM_BRIGHT_GREEN,TERM_RED,TERM_PURPLE,TERM_RED}},
{"^([a-zA-Z0-9\\._\\-]*)(\\(config\\))#",{TERM_BRIGHT_GREEN,TERM_RED}},
{"^([a-zA-Z0-9\\._-]*)#",{TERM_BRIGHT_GREEN}},
{"^([a-zA-Z0-9\\._-]*)>",{TERM_BRIGHT_GREEN}},
/* OSPF config */
{"^(router ospf) ([0-9]+) (vrf) ([a-zA-Z_-]+).$", {TERM_YELLOW, TERM_PURPLE, TERM_YELLOW, TERM_RED}},
{"^(router ospf) ([0-9]+).$", {TERM_YELLOW, TERM_PURPLE}},
{"^( log-adjacency-changes).$", {TERM_YELLOW}},
{"^ (redistribute) ([a-zA-Z0-9 ]+) (subnets).$", {TERM_YELLOW, TERM_PURPLE, TERM_YELLOW}},
{"^ (network) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3} [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) (area) ([0-9]+).$",
{TERM_YELLOW, TERM_PURPLE, TERM_YELLOW, TERM_RED}},
{"^ (default-information) (originate)(.*)$", { TERM_YELLOW, TERM_RED, TERM_PURPLE}},
/* Cisco "show route-map" */
{"^route-map (.+), (permit), sequence ([0-9]+)", {TERM_YELLOW, TERM_GREEN, TERM_PURPLE}},
{"^route-map (.+), (deny), sequence ([0-9]+)", {TERM_YELLOW, TERM_RED, TERM_PURPLE}},
{"^route-map (.+) (permit) ([0-9]+)", {TERM_YELLOW, TERM_GREEN, TERM_PURPLE}},
{"^route-map (.+) (deny) ([0-9]+)", {TERM_YELLOW, TERM_RED, TERM_PURPLE}},
/* BGP Config */
{"^(router bgp) ([0-9]+).$", {TERM_YELLOW, TERM_PURPLE}},
{"^ (address-family) ([a-z0-9]+) (vrf) ([a-zA-Z0-9_-]+).$", {TERM_YELLOW, TERM_PURPLE, TERM_YELLOW, TERM_RED}},
{"^ (address-family) ([a-z0-9]+).$", {TERM_YELLOW, TERM_PURPLE}},
{"^ {1,2}(no) (synchronization).$", {TERM_RED, TERM_YELLOW}},
{"^ {1,2}(neighbor) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) (remote-as) ([0-9]+).$",
{TERM_YELLOW, TERM_PURPLE, TERM_YELLOW, TERM_RED}},
{"^ {1,2}(neighbor) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) (description) ([a-zA-Z0-9 _-]+).$",
{TERM_YELLOW, TERM_PURPLE, TERM_YELLOW, TERM_BLUE}},
/* Routing */
{"^(Routing entry for )([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})/([0-9]{2}).$", {TERM_YELLOW,TERM_PURPLE,TERM_RED}},
{"^ Known via \"([a-z0-9 ]+)\"", {TERM_CYAN}},
{"^(ip route vrf) ([a-zA-Z0-9_-]*) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3} [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})(.*)$",
{TERM_YELLOW, TERM_RED, TERM_PURPLE, TERM_BRIGHT_GREEN}},
{"^(ip route) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3} [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})(.*)$",
{TERM_YELLOW, TERM_PURPLE, TERM_BRIGHT_GREEN}},
/* Juniper Hostname */
{"^(\\{[a-zA-Z0-9_:-]{1,30}\\}).$", {TERM_CYAN}},
{"^(\\{[a-zA-Z0-9_:-]{1,30}\\})(\\[[a-zA-Z0-9_ -]*\\])", {TERM_CYAN, TERM_PURPLE}},
{"^(\\[[a-zA-Z0-9_ -/]*\\]).$", {TERM_PURPLE}},
{"^([a-zA-Z0-9_-]{1,30})@([a-zA-Z0-9\\._-]{1,30})[#>]",{TERM_YELLOW, TERM_BRIGHT_GREEN}},
/* Juniper show | compare */
{"^\\+ (.*)$", {TERM_GREEN}},
{"^\\- (.*)$", {TERM_RED}},
/* Juniper config */
{"^[ ]*([-a-zA-Z0-9_/]+) ([^ ]+) (\\{).$", {TERM_BRIGHT_YELLOW, TERM_PURPLE, TERM_RED}},
{"^[ ]*([a-zA-Z0-9_ -/]+) (\\{).$", {TERM_BRIGHT_YELLOW, TERM_RED}},
{"^[ ]*(}).$", {TERM_RED}},
{"^[ ]*([a-zA-Z0-9/\\._-]+ )([^ ]+);.$", {TERM_LIGHT_BLUE, TERM_CYAN}},
{"^[ ]*([a-zA-Z0-9/\\._-]+);.$", {TERM_CYAN}},
{"^[ ]*([a-zA-Z0-9/\\._-]+) (\\[) (.+) (\\]);.$", {TERM_LIGHT_BLUE, TERM_YELLOW, TERM_CYAN, TERM_YELLOW}},
/* IP Addresses */
{"([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})(\\.[0-9]{1,3})(/[0-9]{1,2})", {TERM_WHITE, TERM_WHITE, TERM_RED}},
{"([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})(\\.[0-9]{1,3})", {TERM_WHITE, TERM_WHITE}},
/* Big numbers */
{" ([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3}) ", {TERM_PURPLE, TERM_CYAN, TERM_BLUE, TERM_CYAN}},
{" ([0-9]{1,3})([0-9]{3})([0-9]{3}) ", {TERM_CYAN, TERM_BLUE, TERM_CYAN}}
};
/* This function loops through each regex and compiles it so it's ready to use */
int init_patterns()
{
unsigned int i;
/* compile regexs */
for (i = 0; i < sizeof(patterns)/sizeof(patterns[0]); i++)
{
if (regcomp(&patterns[i].compiled, patterns[i].pattern, REG_EXTENDED|REG_NEWLINE))
{
fprintf(stderr, "Error compiling pattern: %s\n",
patterns[i].pattern);
return 1;
}
}
return 0;
}
/* This function is called for every incoming packet. It compares it to each regex and inserts
codes as appropriate */
int process_patterns(char *data, int data_len)
{
unsigned int i; /* main for loop, keeps track of which rule/pattern we're on */
int c; /* Used in inner loop to keep track of which sub-expression we're at */
char buffer[32767]; /* Buffer, we rebuild strings with color codes here */
char mydata[32767]; /* Keeps track of the data between loops */
int increased; /* Keeps track of how much we lengthened data */
regmatch_t matches[MAXMATCH]; /* Used as results of regexec for what we matched */
regoff_t last_match; /* used to loop through the data incase we match the data more than once */
increased = 0;
/* Copy the string to the correct length. Data refrences the whole SSH buffer (I think) and has garbage */
memset(mydata, '\0', sizeof(mydata));
strncat(mydata, data, data_len);
/* execute regexs */
for (i = 0; i < sizeof(patterns)/sizeof(patterns[0]); i++)
{
last_match = 0;
while (regexec(&patterns[i].compiled, mydata + last_match, MAXMATCH, matches, 0) == 0)
{
if ((int)matches[1].rm_eo > 0)
{
/* Reset variables */
memset(buffer, '\0', sizeof(buffer));
c = 1;
/* Copy start of string */
strncat(buffer, mydata, (int)matches[c].rm_so + last_match);
while (((int)matches[c].rm_so >= 0) && (c < MAXMATCH) )
{
/* Insert before match */
strcat(buffer, patterns[i].insert_before[c-1]);
/*copy what matched */
strncat(buffer, mydata + (int)matches[c].rm_so + last_match,
(int)matches[c].rm_eo - (int)matches[c].rm_so);
/* Insert the after match */
strcat(buffer, TERM_NONE);
/* Are there any more matches for this pattern? */
if ( (c+1 > MAXMATCH) || ((int)matches[c+1].rm_so < 0 ))
{
/* Nope! Finish string! */
strncat(buffer, mydata + (int)matches[c].rm_eo + last_match,
strlen(mydata) - (int)matches[c].rm_eo + last_match);
} else
{
/* Copy up until next match */
strncat(buffer, mydata + (int)matches[c].rm_eo + last_match,
(int)matches[c+1].rm_so - (int)matches[c].rm_eo);
}
/* Increase the data length by what we added */
increased += strlen(patterns[i].insert_before[c-1]) + strlen(TERM_NONE);
c++;
}
/* Copy the buffer back to mydata */
strcpy(mydata, buffer);
}
last_match += (int)matches[0].rm_eo;
}
}
/* Copy my data back to real data, and return increased length (used in channels.c) */
strcpy(data, mydata);
return increased;
}