-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice-ptr-update.py
executable file
·112 lines (95 loc) · 3.55 KB
/
device-ptr-update.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
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python
# -*- coding: utf-8 -*-
# DNS PTR updater
# Copyright (C) 2017 Pavle Obradovic (pajaja)
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import logging
import sys
import os
import time
from classes import Config
from classes import Device
from classes import DnsCheck
from classes import Dispatcher
from classes import EmailReport
from classes.output.tabular_utf8 import TabularUtf8Output
__version__ = '0.4.4'
reload(sys)
sys.setdefaultencoding('utf8')
start_time = time.time()
logging.basicConfig(
filename=__file__.rstrip('.py|.pyc') + '.log',
format="%(asctime)s - %(levelname)s - %(name)s:%(funcName)s - %(message)s",
level=logging.DEBUG
)
parser = argparse.ArgumentParser()
parser.add_argument("hostname", type=str, help="device hostname (FQDN)")
parser.add_argument("-c", "--check", help="check PTRs but don't update them",
action="store_true")
parser.add_argument("-d", "--diff", help="show only differences",
action="store_true")
parser.add_argument("-t", "--terse", help="terse output - don't display domains",
action="store_true")
args = parser.parse_args()
hostname = args.hostname
check_only = args.check
diff_only = args.diff
terse = args.terse
config = Config(check_only=check_only,
diff_only=diff_only,
terse=terse)
dispatcher = Dispatcher(config)
print "Loaded connectors: %s" % ', '.join(dispatcher.get_connector_list())
dns = DnsCheck(config=config)
output = TabularUtf8Output()
fqdn = dns.get_fqdn(hostname)
if fqdn:
d = Device(hostname=fqdn, config=config)
if d.get_interfaces():
d.check_ptrs()
print output.display_device_detailed(d)
# Filter all PTRs that don't have status equal to STATUS_NOT_UPDATED or STATUS_NOT_CREATED
ptrs_for_update = {
k: v for k, v in d.get_ptrs().iteritems()
if v.status in
(DnsCheck.STATUS_NOT_UPDATED, DnsCheck.STATUS_NOT_CREATED)
}
#print ptrs_for_update
dispatcher.save_ptrs(ptrs_for_update)
email = EmailReport(
config=config,
device=fqdn,
interface_number=d.get_number_of_interfaces(),
ip_number=d.get_number_of_ip_addresses(),
delta_time=time.time() - start_time,
connector_number=len(dispatcher.get_connector_list()),
app_name=os.path.basename(__file__),
app_version=__version__
)
email.generate_report(ptrs=ptrs_for_update)
else:
email = EmailReport(
config=config,
device=fqdn,
delta_time=time.time() - start_time,
connector_number=len(dispatcher.get_connector_list()),
app_name=os.path.basename(__file__),
app_version=__version__
)
email.generate_report(error_message="Error connecting to %s" % d.hostname)
print "Error connecting to %s" % d.hostname
else:
print "Error resolving %s" % hostname