我需要将基于dnsmasq的DHCP服务器配置转换为ISC dhcpd,因此有必要将一大堆固定IP地址转换为新格式。
输入格式为:
84:2b:2b:19:05:a7 192.168.14.6
00:50:56:00:00:07 192.168.14.7
...
输出必须类似:
host myhost1 {
hardware ethernet 84:2b:2b:19:05:a7
fixed address 192.168.14.6
}
主机名应通过反向DNS查询来解析。
答案 0 :(得分:1)
以下是示例python脚本(为清晰起见,代码更长):
import socket
import re
import sys
ethers_file = open(sys.argv[1],'r')
for line in ethers_file:
values = line.split()
mac = None
ip = None
if len(values) >=1 and re.match( r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$',values[0]) :
mac = values[0]
if len(values) >=2 and re.match( r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$',values[1]) :
ip = values[1]
hostname = None
if (mac is not None and ip is not None) :
try:
resolve_values = socket.gethostbyaddr(ip)
hostname = resolve_values[0];
except:
hostname = "host_" + ip.replace("\\.","_")
if (mac is not None and ip is not None) :
print "host " + hostname + " {"
print " hardware ethernet " + mac
print " fixed address " + ip
print "}"
ethers_file.close()