Ver código fonte

create and remove dhcpd host entry

develop
Robin Thoni 8 anos atrás
pai
commit
aec89ef47f
2 arquivos alterados com 35 adições e 11 exclusões
  1. 2
    1
      config.example.json
  2. 33
    10
      localhostgen.py

+ 2
- 1
config.example.json Ver arquivo

8
   },
8
   },
9
   "domain": "local.example.com",
9
   "domain": "local.example.com",
10
   "reverseDomain": "1.168.192.in-addr.arpa",
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
   "recordTTL": 600
13
   "recordTTL": 600
13
 }
14
 }

+ 33
- 10
localhostgen.py Ver arquivo

3
 import argparse
3
 import argparse
4
 import json
4
 import json
5
 from time import time
5
 from time import time
6
-
7
 import psycopg2
6
 import psycopg2
8
 import sys
7
 import sys
8
+import re
9
 
9
 
10
 
10
 
11
 class LocalHostGen:
11
 class LocalHostGen:
12
     db_config = None
12
     db_config = None
13
     db_instance = None
13
     db_instance = None
14
+    dhcpd_replace_string = "# %%HOST%%"
14
 
15
 
15
     def __init__(self, db_config):
16
     def __init__(self, db_config):
16
         self.db_config = db_config
17
         self.db_config = db_config
86
         self.db_instance.commit()
87
         self.db_instance.commit()
87
         return record_id
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
         record_full = "%s.%s" % (host, domain)
94
         record_full = "%s.%s" % (host, domain)
95
+        reverse_domain_record = self.get_reverse_domain_record(reverse_domain, ip)
94
 
96
 
95
         self.insert_host(domain, host, ip, ttl, 'A')
97
         self.insert_host(domain, host, ip, ttl, 'A')
96
         self.insert_host(reverse_domain, reverse_domain_record, record_full, ttl, 'PTR')
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
         record_full = "%s.%s" % (host, domain)
116
         record_full = "%s.%s" % (host, domain)
102
         ip = self.get_record_content(record_full)
117
         ip = self.get_record_content(record_full)
103
         if ip is not None:
118
         if ip is not None:
106
             self.delete_host(record_full)
121
             self.delete_host(record_full)
107
             self.delete_host(reverse_domain_record_full)
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
 def eprint(*args, **kwargs):
133
 def eprint(*args, **kwargs):
111
     print(*args, file=sys.stderr, **kwargs)
134
     print(*args, file=sys.stderr, **kwargs)
140
         if ip is None or mac is None:
163
         if ip is None or mac is None:
141
             eprint("IP and MAC are required to create a host")
164
             eprint("IP and MAC are required to create a host")
142
             exit(1)
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
     elif args.remove:
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
     local_host_gen.disconnect()
170
     local_host_gen.disconnect()
148
 
171
 

Carregando…
Cancelar
Salvar