-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns-manage
More file actions
executable file
·113 lines (85 loc) · 2.78 KB
/
dns-manage
File metadata and controls
executable file
·113 lines (85 loc) · 2.78 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
#! /usr/bin/python3
"""
Manage host dns by creating/deleting symbolic links from
/etc/dnsmasq.d/hosts.enabled to /etc/dnsmasq.d/hosts.available.
There should be no need to restart dnsmasq, but adding symbolic links doesn't
seem to trigger a reload of the config..
The program is invoked by using symbolic links. The links are named:
dns-enable -- enable a host
dns-disable -- disable a host
dns-list -- list all hosts
"""
DNSMASQ_ETC = '/etc/dnsmasq.d'
DNSMASQ_AVAILABLE = DNSMASQ_ETC + '/hosts.available'
DNSMASQ_ENABLED = DNSMASQ_ETC + '/hosts.enabled'
DNSMASQ_CFG_SUFFIX = ".dns"
import argparse
import sys
import os
from os.path import isfile,join
import subprocess
def find(name,path):
for root,dirs,files in os.walk(path):
if name + DNSMASQ_CFG_SUFFIX in files:
return os.path.join(root,name + DNSMASQ_CFG_SUFFIX)
else:
return None
def restart():
"""
restart naemon
"""
subprocess.run(['/bin/systemctl','restart', 'naemon'])
def enhost(host):
"""
First, search the available hosts directory for the
desired host. If it doesn't exist, exit...
Next, search the enabled hosts directory for the
desired host. If it exists, exit...
Create a symbolic link in the enabled directory.
"""
available = find(host,DNSMASQ_AVAILABLE)
if not available:
print("%s not found in available directory -- " % host + DNSMASQ_AVAILABLE)
return
enabled = find(host,DNSMASQ_ENABLED)
if enabled:
print("%s is already enabled" % host)
return
src = available
dest = DNSMASQ_ENABLED + '/' + host + DNSMASQ_CFG_SUFFIX
print("Linking %s to %s" % (src,dest))
os.symlink(src,dest)
restart()
listhosts()
def dishost(host):
available = find(host,DNSMASQ_AVAILABLE)
if not available:
print("%s not found in available directory -- " % host + DNSMASQ_AVAILABLE)
enabled = find(host,DNSMASQ_ENABLED)
if not enabled:
print("%s is not enabled" % host)
return
print("Removing %s" % enabled)
os.remove(enabled)
restart()
listhosts()
def listhosts():
print("Available:")
files = [f for f in os.listdir(DNSMASQ_AVAILABLE) if isfile(join(DNSMASQ_AVAILABLE,f))]
for file in files:
print("\t" + file)
print("Enabled:")
files = [f for f in os.listdir(DNSMASQ_ENABLED) if isfile(join(DNSMASQ_ENABLED,f))]
for file in files:
print("\t" + file)
if __name__ == "__main__":
command = os.path.basename(sys.argv[0])
if command == 'dns-enable':
enhost(sys.argv[1])
elif command == 'dns-disable':
dishost(sys.argv[1])
elif command == 'dns-list':
listhosts()
else:
print("Use dns-enable, dns-disable, dns-list instead")
sys.exit()