我创建了一个Python脚本,旨在让我作为系统管理员的生活更加轻松。该脚本的目的是将Microsoft DHCP服务器转储文件转换为排序后的CSV文件。
我将在此处包含代码,并感谢所有种类的改进。
我的问题是,我的脚本创建了一个列表的列表(每个DHCP预留一个列表)。例如:
未使用的IP地址未列出。但我希望我的脚本能够自动添加所有未使用的IP地址之间的子列表,并给它们一个注释,例如“未注册”。
我不知道如何开始在谷歌上搜索如何完成这个任务,所以任何帮助都将不胜感激 :) 脚本内容:
我将在此处包含代码,并感谢所有种类的改进。
我的问题是,我的脚本创建了一个列表的列表(每个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]
dmpFile
内容的例子吗? - bernard paulus