我创建了一个 python 脚本来尝试让我作为系统管理员的生活更轻松。此脚本的重点是将 Microsoft DHCP 服务器转储文件转换为排序的 CSV 文件。
我将在此处包含代码,并感谢各种改进。
我的问题
我的脚本创建了一个列表列表(每个 dhcp 预留一个)。例如:
[
# [DHCP SERVER, IP ADDRESS, MAC ADDRESS, HOSTNAME, DESCRIPTION]
[server1,172.16.0.120,31872fcefa33,wks120.domain.net,Description of client]
[server1,172.16.0.125,4791ca3d7279,wks125.domain.net,Description of client]
[server1,172.16.0.132,6035a71c930c,wks132.domain.net,Description of client]
...
]
未列出未使用的 IP 地址。但我希望我的脚本能够自动为所有未使用的 IP 地址添加子列表,并给他们一个评论,说“未注册”之类的。
我什至不知道如何开始在谷歌上搜索如何完成这项任务,所以任何帮助将不胜感激:)
剧本
#!/usr/bin/python
import sys, shlex
from operator import itemgetter
# Function: get_dhcp_reservations
#
# Extracts a list of ip reservations from a Microsoft DHCP server dump file
# then it stores the processed reservations them in a nested list
def get_dhcp_reservations(dmpFile):
# Setup empty records list
records = []
# Open dump file for reading
dmpFile = open(dmpFile,"r")
# Iterate dump file line by line
for line in dmpFile:
# Only user lines with the word "reservedip" in it
if "reservedip" in line:
# Split the line into fields excluding quoted substrings
field = shlex.split(line)
# Create a list of only the required fields
result = [field[2][1:9], field[7], field[8], field[9], field[10]]
# Append each new record as a nested list
records.append(result)
# Return the rendered data
return records
# Function: sort_reservations_by_ip
#
# Sorts all records by the IPv4 address field
def sort_reservations_by_ip(records):
# Temporarily convert dotted IPv4 address to tuples for sorting
for record in records:
record[1] = ip2tuple(record[1])
# Sort sublists by IP address
records.sort(key=itemgetter(1))
# Convert tuples back to dotted IPv4 addresses
for record in records:
record[1] = tuple2ip(record[1])
return records
# Function: ip2tuple
#
# Split ip address into a tuple of 4 integers (for sorting)
def ip2tuple(address):
return tuple(int(part) for part in address.split('.'))
# Function: tuple2ip
#
# Converts the tuple of 4 integers back to an dotted IPv4 address
def tuple2ip(address):
result = ""
for octet in address:
result += str(octet)+"."
return result[0:-1]
# Get DHCP reservations
records = get_dhcp_reservations(sys.argv[1])
# Sort reservations by IP address
records = sort_reservations_by_ip(records)
# Print column headings
print "DHCP Server,Reserved IP,MAC Address,Hostname,Description"
# Print in specified format records
for record in records:
print record[0]+","+record[1]+",\""+record[2]+"\","+record[3]+","+record[4]
注意:我也尝试过使用 python socket.inet_ntoa进行 IPv4 排序,如本网站其他主题中所建议的那样,但未能成功使其正常工作。
转储文件示例
根据请求,这里是一些转储文件
[Ommited content]
# ======================================================================
# Start Add ReservedIp to the Scope : 172.16.0.0, Server : server1.domain.net
# ======================================================================
Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.76 0800278882ae "wks126devlin.domain.net" "Viana (VM)" "BOTH"
Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.118 001e37322202 "WKS18.domain.net" "Kristof (linux)" "BOTH"
Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.132 000d607205a5 "WKS32.domain.net" "Lab PC" "BOTH"
Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.156 338925b532ca "wks56.domain.net" "Test PC" "BOTH"
Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.155 001422a7d474 "WKS55.domain.net" "Liesbeth" "BOTH"
Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.15 0800266cfe31 "xpsystst.domain.net" "Pascal (VM)" "BOTH"
[Ommited content]