-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnsmasq-convert
66 lines (55 loc) · 1.73 KB
/
dnsmasq-convert
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
#!/usr/bin/env ruby
input_name = ARGV[0]
input_name = '/etc/dnsmasq.source' unless input_name
exit 1 unless File.file?(input_name)
def identify instance, fragment
case fragment
when /\A([0-9a-f]{2}:){5}[0-9a-f]{2}\Z/i # MAC
instance[:mac] ||= []
instance[:mac] << fragment
when /\A(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\Z/
instance[:ipv4] ||= []
instance[:ipv4] << fragment
when /port-\d{1,}\Z/i
instance[:port] ||= []
instance[:port] << fragment
when /\A[-a-z0-9.]{1,}\Z/i
instance[:name] ||= []
instance[:name] << fragment
else
raise "No matching regular expression was found for data fragment: #{fragment}!"
end
return instance
end
# INPUT
instances = []
File.readlines(input_name).each do |line|
line.chomp!
line.gsub!(/#.*\Z/, '')
next if line.empty?
instance = {}
line.scan(/[^\s]{1,}/).each{ |fragment| instance = identify(instance, fragment) }
instance[:name] = [ "inet-#{instance[:ipv4].first.tr('.', '-')}" ] if instance[:name].nil? # instance[:name].empty?
instances << instance
end
# OUTPUT
def banner io
io.puts "# !! DO NOT EDIT THIS FILE DIRECTLY !!"
io.puts "# !! EDIT /etc/dnsmasq.source, THEN !!"
io.puts "# !! CALL /etc/dnsmasq-convert !!"
io.puts "#"
end
File.open('/etc/dnsmasq.dhcp.conf', 'w') do |output|
banner(output)
instances.each do |instance|
output.puts '%s,%s,%s' % [ instance[:mac].join(','), instance[:ipv4].first, instance[:name].join(',') ]
end
end
File.open('/etc/dnsmasq.hosts.conf', 'w') do |output|
banner(output)
instances.each do |instance|
instance[:name].each do |name|
output.write '%-18s %s%s' % [ instance[:ipv4].first, name, "\n" ]
end
end
end