将/ etc / ethers转换为/etc/dhcpd.conf

问题描述 投票:-1回答:1

我需要将基于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查询解析主机名。

linux dhcp dnsmasq
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.