Skip to content

Instantly share code, notes, and snippets.

@spdkils
Created September 27, 2017 05:38
Show Gist options
  • Save spdkils/2e79c3eecc09280b8ff058a668258c9d to your computer and use it in GitHub Desktop.
Save spdkils/2e79c3eecc09280b8ff058a668258c9d to your computer and use it in GitHub Desktop.
# Sexy Lexi
import collections
import re
import acltests
import aceNamePort
from tkinter import *
root = Tk()
Token = collections.namedtuple('Token', ['typ', 'value', 'line', 'column'])
def tokenit(ace):
keywords = {'permit', 'deny', 'ip', 'tcp', 'udp', 'icmp', 'any', 'host', 'eq', 'gt', 'lt', 'range', 'established', 'reflect', 'timeout', 'reflect', 'timeout'}
tokens = [
('COMMENT', r'\!.*'),
('REMARK', r'(remark .+)'),
('ACTION', r'\b(permit|deny)\b'),
('PROTOCOL', r'\b(ip|tcp|icmp|udp|eigrp)\b'),
('LTGT', r'\b(gt|lt)\b'),
('RANGE', r'\b(range)\b'),
('EQ', r'\b(eq)\b'),
('LOG', r'\blog(-input)?\b'),
('NAME', r'(\b[a-zA-Z\-_]+\b)'),
('NETWORK', r'\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) ' +
r'\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'),
('ADDRESS', r'\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'),
('NUMBER', r'\b(?<!\.)\d+(?!\.)\b'),
('SPACE', r' '),
('NEWLINE', '\n'),
('OOPS', r'.'), ]
tok_reg = '|'.join('(?P<%s>%s)' % pair for pair in tokens)
line_start = 0
line_num = 1
for mo in re.finditer(tok_reg, ace):
kind = mo.lastgroup
value = mo.group(kind)
if kind == 'NEWLINE':
line_start = mo.end()
line_num += 1
elif kind == 'NAME' and value in keywords:
kind = value.upper()
elif kind == 'NAME' and value in aceNamePort.icmp_ports:
kind = 'ICMP_PORT'
elif kind == 'NAME' and value in aceNamePort.tcp_ports:
kind = 'TCP_PORT'
elif kind == 'NAME' and value in aceNamePort.udp_ports:
kind = 'UDP_PORT'
if kind == 'SPACE':
pass
else:
column = mo.start() - line_start
yield Token(kind, value, line_num, column)
lasttext = ''
def callfunc():
global lasttext
mytext.after(100, callfunc)
gettext = mytext.get(1.0, END)
if gettext != lasttext:
print([x for x in tokenit(gettext)])
lasttext = gettext
mytext = Text(root)
mytext.pack(fill=BOTH)
root.after(100, callfunc)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment