-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
150 lines (126 loc) · 4.56 KB
/
Copy pathutils.py
File metadata and controls
150 lines (126 loc) · 4.56 KB
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
#!/usr/bin/env python
# coding=utf-8
import os
import sys
import re
import socket
import commands
from config import suffix
server = '172.16.3.131'
#get dns (support by ip and hostname)
def getDns(record):
#判断是IP or hostname
if re.findall(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])', record) is not None:
try:
socket.gethostbyaddr(record)
return True
except socket.herror,e:
return False
else:
try:
socket.getaddrinfo(record, None)
return True
except socket.gaierror, e:
return False
#get ip by hostname
def getIpByHost(ip):
try:
result = socket.gethostbyaddr(ip)
return result[0]
#"sucess to get hostname by ip: %s hostname: %s" % (ip, result[0])
except socket.herror, e:
return False
#"faled to get hostname by ip: %s" % ip
#get hostname by ip
def getHostByIp(hostname):
try:
result = socket.getaddrinfo(hostname, None)
return result[0][4]
#"sucess to get ip by hostname: %s ip: %s" % (hostname, result[0][4])
except socket.gaierror, e:
return False
#"faled to get ip by hostname: %s" % hostname
#get dns by ip and check by hostname(ip<-->hostname,return True)
def getDnsIp(ip, hostname):
ip_host = getIpByHost(ip)
host_ip = getHostByIp(hostname)
if ip_host is not hostname and host_ip is not ip:
return "please check ip or hostname"
def getAddIpAddrCmd(host, ip, ttl):
cmd = "echo $'server %s \nupdate add %s %d A %s\r\nsend \r\nquit\n' | nsupdate -t5 -v -k ./nsupdate.key -v" % (server, host, ttl, ip)
result = commands.getstatusoutput(cmd)
return result
def getAddPTRCmd(host, rip, ttl):
cmd = "echo $'server %s \nupdate add %s %d PTR %s\r\nsend \r\nquit\n' | nsupdate -t5 -v -k ./nsupdate.key -v" % (server, rip, ttl, host)
result = commands.getstatusoutput(cmd)
return result
def getDelIpAddrCmd(host, ip, ttl):
cmd = "echo $'server %s \nupdate delete %s %d A %s\r\nsend \r\nquit\n' | nsupdate -t5 -v -k ./nsupdate.key -v" % (server, host, ttl, ip)
result = commands.getstatusoutput(cmd)
return result
def getDelPTRCmd(host, rip, ttl):
cmd = "echo $'server %s \nupdate delete %s %d PTR %s\r\nsend \r\nquit\n' | nsupdate -t5 -v -k ./nsupdate.key -v" % (server, rip, ttl, host)
result = commands.getstatusoutput(cmd)
return result
def getAddRecordCmd(record):
ip = record['ip']
rip = ('.').join(ip.split('.')[::-1]) + '.in-addr.arpa'
ttl = 60
host = record['domain']
msg = checkAddRecord(record)
if not msg[0]:
return msg
aresult = getAddIpAddrCmd(host, ip, ttl)
if aresult[0]:
return ("fail", aresult[1])
presult = getAddPTRCmd(host, rip, ttl)
if presult[0]:
return ("fail", presult[1])
return ("sucess", " ")
def getDelRecordCmd(record):
ip = record['ip']
rip = ('.').join(ip.split('.')[::-1]) + '.in-addr.arpa'
ttl = 60
host = record['domain']
msg = checkDelRecord(record)
if not msg[0]:
return msg
aresult = getDelIpAddrCmd(host, ip, ttl)
if aresult[0]:
return ("fail", aresult[1])
presult = getDelPTRCmd(host, rip, ttl)
if presult[0]:
return ("fail", presult[1])
return ("sucess", " ")
#check add record (check ip/hostname and check dns by ip and hostname)
def checkAddRecord(record):
if re.findall(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])', record['ip']) is None:
return ['False', 'invaild ip %s' % record['ip']]
if re.findall(suffix, record['domain']) is None:
return ['False', 'invaild doamin %s' % record['domain']]
if getDns(record['ip']):
return ['False', 'dns record duplicated: %s' % record['ip']]
if getDns(record['domain']):
return ['False', 'dns record duplicated: %s' % record['domain']]
return ['True',]
#check del record
def checkDelRecord(record):
if re.findall(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])', record['ip']) is None:
return ['False', 'invaild ip %s' % record['ip']]
if re.findall(suffix, record['domain']) is None:
return ['False', 'invaild doamin %s' % record['domain']]
return ['True', ]
#add the record (by check and add)
def addRecord(record):
err = checkAddRecord(record)
if not err[0]:
return err[1]
result = getAddRecordCmd(record)
return result
#del the record (by check and del)
def delRecord(record):
err = checkAddRecord(record)
if not err[0]:
return err[1]
result = getDelRecordCmd(record)
return result