Procházet zdrojové kódy

modified config file; create and remove wildcard

develop
Robin Thoni před 8 roky
rodič
revize
38edc5d526
2 změnil soubory, kde provedl 29 přidání a 11 odebrání
  1. 12
    5
      config.example.json
  2. 17
    6
      localhostgen.py

+ 12
- 5
config.example.json Zobrazit soubor

@@ -6,9 +6,16 @@
6 6
     "username": "powerdns",
7 7
     "password": "password"
8 8
   },
9
-  "domain": "local.example.com",
10
-  "reverseDomain": "1.168.192.in-addr.arpa",
11
-  "dhcpdFilePath": "/etc/dhcp/dhcpd.conf",
12
-  "dhcpdReloadCommand": "systemctl restart isc-dhcp-server",
13
-  "recordTTL": 600
9
+
10
+  "dns": {
11
+    "domain": "local.example.com",
12
+    "reverseDomain": "1.168.192.in-addr.arpa",
13
+    "recordTTL": 600,
14
+    "wildcard": true
15
+  },
16
+
17
+  "dhcpd": {
18
+    "filePath": "/etc/dhcp/dhcpd.conf",
19
+    "reloadCommand": "systemctl restart isc-dhcp-server"
20
+  }
14 21
 }

+ 17
- 6
localhostgen.py Zobrazit soubor

@@ -2,6 +2,7 @@
2 2
 
3 3
 import argparse
4 4
 import json
5
+import os
5 6
 from time import time
6 7
 import psycopg2
7 8
 import sys
@@ -82,19 +83,24 @@ class LocalHostGen:
82 83
     def delete_host(self, record):
83 84
         cur = self.db_instance.cursor()
84 85
         cur.execute("DELETE FROM records WHERE name='%s' RETURNING id" % record)
85
-        record_id = cur.fetchone()[0]
86
+        record_id = cur.fetchone()
86 87
         cur.close()
87 88
         self.db_instance.commit()
88
-        return record_id
89
+        if record_id is not None:
90
+            return record_id[0]
91
+        return None
89 92
 
90 93
     def remove_host_dhcp(self, host, dhcpd_file_content):
91 94
         return re.sub(" *host +%s +\{[^}]+} *\n" % host, "", dhcpd_file_content)
92 95
 
93
-    def create_host(self, domain, reverse_domain, host, ip, mac, ttl, dhcpd_file_path):
96
+    def create_host(self, domain, reverse_domain, host, ip, mac, ttl, dhcpd_file_path, wildcard):
94 97
         record_full = "%s.%s" % (host, domain)
98
+        host_wildcard = "*.%s" % host
95 99
         reverse_domain_record = self.get_reverse_domain_record(reverse_domain, ip)
96 100
 
97 101
         self.insert_host(domain, host, ip, ttl, 'A')
102
+        if wildcard:
103
+            self.insert_host(domain, host_wildcard, record_full, ttl, 'CNAME')
98 104
         self.insert_host(reverse_domain, reverse_domain_record, record_full, ttl, 'PTR')
99 105
 
100 106
         with open(dhcpd_file_path, "r") as f:
@@ -114,10 +120,12 @@ class LocalHostGen:
114 120
 
115 121
     def remove_host(self, domain, reverse_domain, host, dhcpd_file_path):
116 122
         record_full = "%s.%s" % (host, domain)
123
+        record_full_wildcard = "*.%s" % record_full
117 124
         ip = self.get_record_content(record_full)
118 125
         if ip is not None:
119 126
             reverse_domain_record = self.get_reverse_domain_record(reverse_domain, ip)
120 127
             reverse_domain_record_full = "%s.%s" %(reverse_domain_record, reverse_domain)
128
+            self.delete_host(record_full_wildcard)
121 129
             self.delete_host(record_full)
122 130
             self.delete_host(reverse_domain_record_full)
123 131
 
@@ -154,7 +162,6 @@ def main():
154 162
     host = args.host
155 163
     ip = args.ip
156 164
     mac = args.mac
157
-    ttl = config['recordTTL']
158 165
 
159 166
     local_host_gen = LocalHostGen(config['database'])
160 167
     local_host_gen.connect()
@@ -163,11 +170,15 @@ def main():
163 170
         if ip is None or mac is None:
164 171
             eprint("IP and MAC are required to create a host")
165 172
             exit(1)
166
-        local_host_gen.create_host(config['domain'], config['reverseDomain'], host, ip, mac, ttl, config['dhcpdFilePath'])
173
+        local_host_gen.create_host(config['dns']['domain'], config['dns']['reverseDomain'], host, ip, mac,
174
+                                   config['dns']['recordTTL'], config['dhcpd']['filePath'], config['dns']['wildcard'])
167 175
     elif args.remove:
168
-        local_host_gen.remove_host(config['domain'], config['reverseDomain'], host, config['dhcpdFilePath'])
176
+        local_host_gen.remove_host(config['dns']['domain'], config['dns']['reverseDomain'], host,
177
+                                   config['dhcpd']['filePath'])
169 178
 
170 179
     local_host_gen.disconnect()
171 180
 
181
+    os.system(config['dhcpd']['reloadCommand'])
182
+
172 183
 
173 184
 main()

Načítá se…
Zrušit
Uložit