Browse Source

create and remove dhcpd host entry

develop
Robin Thoni 8 years ago
parent
commit
aec89ef47f
2 changed files with 35 additions and 11 deletions
  1. 2
    1
      config.example.json
  2. 33
    10
      localhostgen.py

+ 2
- 1
config.example.json View File

@@ -8,6 +8,7 @@
8 8
   },
9 9
   "domain": "local.example.com",
10 10
   "reverseDomain": "1.168.192.in-addr.arpa",
11
-  "dhcpdFile": "/etc/dhcp/dhcpd.conf",
11
+  "dhcpdFilePath": "/etc/dhcp/dhcpd.conf",
12
+  "dhcpdReloadCommand": "systemctl restart isc-dhcp-server",
12 13
   "recordTTL": 600
13 14
 }

+ 33
- 10
localhostgen.py View File

@@ -3,14 +3,15 @@
3 3
 import argparse
4 4
 import json
5 5
 from time import time
6
-
7 6
 import psycopg2
8 7
 import sys
8
+import re
9 9
 
10 10
 
11 11
 class LocalHostGen:
12 12
     db_config = None
13 13
     db_instance = None
14
+    dhcpd_replace_string = "# %%HOST%%"
14 15
 
15 16
     def __init__(self, db_config):
16 17
         self.db_config = db_config
@@ -86,18 +87,32 @@ class LocalHostGen:
86 87
         self.db_instance.commit()
87 88
         return record_id
88 89
 
89
-    def create_host(self, domain, reverse_domain, host, ip, mac, ttl):
90
-        if self.db_instance is None:
91
-            self.connect()
92
-        reverse_domain_record = self.get_reverse_domain_record(reverse_domain, ip)
90
+    def remove_host_dhcp(self, host, dhcpd_file_content):
91
+        return re.sub(" *host +%s +\{[^}]+} *\n" % host, "", dhcpd_file_content)
92
+
93
+    def create_host(self, domain, reverse_domain, host, ip, mac, ttl, dhcpd_file_path):
93 94
         record_full = "%s.%s" % (host, domain)
95
+        reverse_domain_record = self.get_reverse_domain_record(reverse_domain, ip)
94 96
 
95 97
         self.insert_host(domain, host, ip, ttl, 'A')
96 98
         self.insert_host(reverse_domain, reverse_domain_record, record_full, ttl, 'PTR')
97 99
 
98
-    def remove_host(self, domain, reverse_domain, host):
99
-        if self.db_instance is None:
100
-            self.connect()
100
+        with open(dhcpd_file_path, "r") as f:
101
+            dhcpd_file_content = f.read()
102
+
103
+        dhcpd_host_config = "host %s {\n" \
104
+                            "        hardware ethernet %s;\n" \
105
+                            "        fixed-address %s;\n" \
106
+                            "    }" % (host, mac, record_full)
107
+
108
+        dhcpd_file_content = self.remove_host_dhcp(host, dhcpd_file_content)
109
+        dhcpd_file_content = dhcpd_file_content.replace(self.dhcpd_replace_string, "%s\n    %s" %
110
+                                                        (dhcpd_host_config, self.dhcpd_replace_string))
111
+
112
+        with open(dhcpd_file_path, "w") as f:
113
+            f.write(dhcpd_file_content)
114
+
115
+    def remove_host(self, domain, reverse_domain, host, dhcpd_file_path):
101 116
         record_full = "%s.%s" % (host, domain)
102 117
         ip = self.get_record_content(record_full)
103 118
         if ip is not None:
@@ -106,6 +121,14 @@ class LocalHostGen:
106 121
             self.delete_host(record_full)
107 122
             self.delete_host(reverse_domain_record_full)
108 123
 
124
+        with open(dhcpd_file_path, "r") as f:
125
+            dhcpd_file_content = f.read()
126
+
127
+        dhcpd_file_content = self.remove_host_dhcp(host, dhcpd_file_content)
128
+
129
+        with open(dhcpd_file_path, "w") as f:
130
+            f.write(dhcpd_file_content)
131
+
109 132
 
110 133
 def eprint(*args, **kwargs):
111 134
     print(*args, file=sys.stderr, **kwargs)
@@ -140,9 +163,9 @@ def main():
140 163
         if ip is None or mac is None:
141 164
             eprint("IP and MAC are required to create a host")
142 165
             exit(1)
143
-        local_host_gen.create_host(config['domain'], config['reverseDomain'], host, ip, mac, ttl)
166
+        local_host_gen.create_host(config['domain'], config['reverseDomain'], host, ip, mac, ttl, config['dhcpdFilePath'])
144 167
     elif args.remove:
145
-        local_host_gen.remove_host(config['domain'], config['reverseDomain'], host)
168
+        local_host_gen.remove_host(config['domain'], config['reverseDomain'], host, config['dhcpdFilePath'])
146 169
 
147 170
     local_host_gen.disconnect()
148 171
 

Loading…
Cancel
Save