gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:52:17 30 4
gpt4 key购买 nike

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

最佳答案

这是示例 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()

关于linux - 将/etc/ethers转换为/etc/dhcpd.conf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54865347/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com