Created
January 23, 2020 11:05
-
-
Save vkushnir/e26454404c3d5b8db6c732df53370288 to your computer and use it in GitHub Desktop.
DLink to HTML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| import argparse | |
| import os | |
| import re | |
| from shutil import copyfile, rmtree | |
| from subprocess import check_output | |
| from tempfile import mkdtemp | |
| __version__ = "0.01" | |
| __copyright__ = "Vladimir Kushnir aka Kvantum i(c)2020" | |
| def get_options(): | |
| parser = argparse.ArgumentParser(usage="%(prog)s [options] <log file>", | |
| description="Convert D-Link log files to HTML.") | |
| parser.add_argument("--version", action="version", version="%(prog)s " + __version__) | |
| parser.add_argument("-v", action="store_true", dest="verbose", default=False, help="Verbose output.") | |
| parser.add_argument("-p", "--path", dest="path", default=os.getcwd(), help="Path to Log files location.") | |
| parser.add_argument("-w", "--write", dest="html", help="Write output to file.") | |
| parser.add_argument("files", type=str, nargs="+", help="Log files.") | |
| return parser.parse_args() | |
| def get_files(options: argparse.Namespace): | |
| files = [] | |
| for mask in options.files: | |
| files.extend(check_output(["find", options.path, "-type", "f", "-name", mask]).split()) | |
| if options.verbose: | |
| print("Found {} files.".format(len(files))) | |
| print(*files, sep="\n") | |
| return files | |
| def get_file(fn): | |
| pass | |
| def do_files(files, html, prog): | |
| html.write(""" | |
| <table> | |
| <tr> | |
| <th>IP</th><th>SWITCH MODEL</th><th>DEVICE INFO</th><th>VLANS</th> | |
| </tr> | |
| """) | |
| for file in files: | |
| do_file(file, html, prog) | |
| html.write(""" | |
| </table> | |
| """) | |
| def do_file(fn, html, prog): | |
| with open(fn, 'r') as f: | |
| for line in f: | |
| html.write(" <tr>\n") | |
| do_line(line, html, prog) | |
| html.write(" </tr>\n") | |
| def do_line(line, html, prog): | |
| data = prog.match(line) | |
| html.write("<td class=\"ip\" nowrap >{}</td><td class=\"model\" nowrap >{}</td><td class=\"info\" nowrap >{}</td><td class=\"vlans\">{}</td>". | |
| format(data.group('ip'), data.group('model'),data.group('info'),data.group('vlans'))) | |
| if __name__ == "__main__": | |
| # TODO: Check if no files or folders exists | |
| # TODO: Add option to wait anykey before exit | |
| prog = re.compile("(?P<ip>\d+\.\d+\.\d+\.\d+):\s+(?P<model>\S+)\s+\[(?P<info>\S+)\]\s+(?P<vlans>.*)") | |
| opt = get_options() | |
| if opt.verbose: | |
| print("Seaching files in: \"{}\"".format(opt.path)) | |
| with open(opt.html, 'w') as html: | |
| html.write(""" | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"> | |
| </head> | |
| <body>\n""") | |
| do_files(opt.files, html, prog) | |
| html.write(""" | |
| </body> | |
| </html>\n""") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| body { margin: 0 } | |
| body, table, th, td { font-size:8pt; font-family:"Monospace",sans-serif; } | |
| p { margin-top:1cm; } | |
| h1 { font-size:12pt; margin-top:2cm; text-align:center; font-style: italic; font-weight: bold; text-decoration: underline; } | |
| table { font-size:12pt; border: 1px solid black; } | |
| table.no_border { border: none; } | |
| tbody, tr, th, td { border: inherit; } | |
| th { font-style: italic; } | |
| th, td { padding:1pt 5.4pt 1pt 5.4pt; } | |
| ul, li { margin: 0; padding: 0; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment