Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dnsviz « bin - github.com/dnsviz/dnsviz.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4fa4cb5d3b3f21b9c1b224b2f5bf1aae3acfa23a (plain)
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
#!/usr/bin/env python
#
# This file is a part of DNSViz, a tool suite for DNS/DNSSEC monitoring,
# analysis, and visualization.
# Created by Casey Deccio (casey@deccio.net)
#
# Copyright 2015-2016 VeriSign, Inc.
#
# Copyright 2016-2019 Casey Deccio
#
# DNSViz is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# DNSViz is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with DNSViz.  If not, see <http://www.gnu.org/licenses/>.
#

from __future__ import unicode_literals

import getopt
import importlib
import logging
import os
import sys

CMD_DIR1 = 'dnsviz'
CMD_DIR2 = 'commands'

logging.basicConfig(level=logging.WARNING, format='%(message)s')
logger = logging.getLogger()

def check_deps():
    # check dnspython dependency
    try:
        import dns.name
    except ImportError:
        sys.stderr.write('Error: dnspython does not appear to be installed\n')
        sys.exit(1)

def usage(err=None):
    if err is not None:
        err += '\n\n'
    else:
        err = ''
    sys.stderr.write('''%sUsage: dnsviz [options] <command> [args]
Options:
    -p <path>      - Add path to the python path.
Commands:
    probe          - Issue diagnostic DNS queries.
    grok           - Assess diagnostic DNS queries.
    graph          - Graph the assessment of diagnostic DNS queries.
    print          - Process diagnostic DNS queries to textual output.
    query          - Assess a DNS query.
    help [<command>]
                   - Show usage for a command.
''' % (err))

def main():
    check_deps()

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'p:')
    except getopt.GetoptError as e:
        usage(str(e) + '\n')
        sys.exit(1)

    opts = dict(opts)

    if len(args) < 1:
        usage()
        sys.exit(0)

    if args[0] == 'help':
        if len(args) < 2:
            usage()
            sys.exit(0)

        command = args[1]
    else:
        command = args[0]

    if '-p' in opts:
        sys.path.insert(0, opts['-p'])

    # first try importing just the commands module to make sure
    # dnsviz is properly reachable with the current path
    importlib.import_module('%s.%s' % (CMD_DIR1, CMD_DIR2))

    # now try importing the module for the actual command
    try:
        mod = importlib.import_module('%s.%s.%s' % (CMD_DIR1, CMD_DIR2, command))
    except ImportError:
        # See if the filename corresponding to the module we were trying to
        # import was found in the stack.  If so, then the error is with
        # importing something from inside that file.  Otherwise, it was that we
        # couldn't find the file corresponding to the command, so it was thus
        # an illegitmate command.
        exc_frame = sys.exc_info()[2]
        frame1 = exc_frame.tb_next.tb_next

        found_file = False
        while frame1 is not None:
            filename = frame1.tb_frame.f_code.co_filename
            cmd_dir2, filename = os.path.split(filename)
            if filename.endswith('.py'):
                filename = filename[:-3]
            cmd_dir1, cmd_dir2 = os.path.split(cmd_dir2)
            cmd_dir0, cmd_dir1 = os.path.split(cmd_dir1)
            if cmd_dir1 == CMD_DIR1 and \
                    cmd_dir2 == CMD_DIR2 and \
                    filename == command:
                found_file = True
                break
            frame1 = frame1.tb_next

        if found_file:
            raise
        else:
            sys.stderr.write('Invalid command: %s\n' % command)
            sys.exit(1)

    if args[0] == 'help':
        helper = mod.build_helper(logger, sys.argv[0], command)
        helper.parser.print_help()
    else:
        mod.main(args)

if __name__ == "__main__":
    main()