#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Class for read or modify dhcpd server config file
Last versión:
http://lwp-l.com/s5385
readFile() -> read dhcpd file received in constructor and put data in two lists
getValue() -> return value from key received
sample:
key: "option domain-name-servers"
return: "option domain-name-servers ns1.example.org, ns2.example.org;"
setValue() -> update the value if exist or add new value if not exists
removeValue() -> remove values
clearSubnet() -> remove the subnets created or readed from file
newSubnet() -> create a new subnet
getSubnetNumber() -> return the number of subnets
getSubnetValue() -> return value of indicated subnet
setSubnetValue() -> add or update values of the subnet
save() -> save any changes to the file
config file is similar to:
option domain-name "example.org"; getValue(), setValue()
option domain-name-servers ns1.example.org, ns2.example.org; getValue(), setValue()
default-lease-time 600; getValue(), setValue()
max-lease-time 7200; getValue(), setValue()
subnet 10.5.5.0 netmask 255.255.255.224 { getSubnetNumber(), newSubnet(), clearSubnet(), getSubnetValue()
range 10.5.5.26 10.5.5.30; setSubnetValue(), getSubnetValue()
option domain-name-servers ns1.internal.example.org; setSubnetValue(), getSubnetValue()
option domain-name "internal.example.org"; setSubnetValue(), getSubnetValue()
option routers 10.5.5.1; setSubnetValue(), getSubnetValue()
option broadcast-address 10.5.5.31; setSubnetValue(), getSubnetValue()
default-lease-time 600; setSubnetValue(), getSubnetValue()
max-lease-time 7200; setSubnetValue(), getSubnetValue()
}
subnet 10.10.10.0 netmask 255.255.255.0 { getSubnetNumber(), newSubnet(), clearSubnet(), getSubnetValue()
range 10.10.10.200 10.10.10.230; setSubnetValue(), getSubnetValue()
option domain-name-servers 8.8.8.8; setSubnetValue(), getSubnetValue()
option domain-name "internal.example.org"; setSubnetValue(), getSubnetValue()
option routers 10.10.10.1; setSubnetValue(), getSubnetValue()
option broadcast-address 10.10.10.255; setSubnetValue(), getSubnetValue()
}
"""
import os, sys
from subprocess import Popen, PIPE
import glob
import re
__ver__="0.2"
class Dhcpd():
""" Contain a file name and path to dhcpd server """
file=""
""" variables that contain the vaues and subnets """
valuesData=[]
subnetData=[]
def __init__(self, file):
"""
Parameters:
file string - path and file name
"""
self.file=file
def readFile(self):
"""
This function read dhcpd file an set data into vars
Return
boolean - False if not possible read the file
"""
self.valuesData=[]
self.subnetData=[]
if not self.file or not os.path.exists(self.file):
return False
inSubnet=False
subnet=-1
with open(self.file,"r") as f:
for line in f:
line=line.strip()
if line and line[0]!="#":
if len(line)>=7 and line[:7]=="subnet ":
inSubnet=True
subnet+=1
self.subnetData.append([])
if inSubnet:
self.subnetData[subnet].append(line)
else:
self.valuesData.append(line)
if inSubnet and line and line[0]=="}":
inSubnet=False
return True
def getValue(self, key):
"""
Function that return a line for a value in dhcpd config file
Find the text that start line to len the value is same
Parameteres:
key string
Return:
string
"""
for line in self.valuesData:
if len(line)>=len(key) and line[0:len(key)]==key:
return line
return ""
def setValue(self, key, value):
"""
Function to update value if exist or add new value if not exists
Parameters:
key string
value string
return:
integer - [1 updated, 2 insert]
"""
for i in range(len(self.valuesData)):
line=self.valuesData[i]
if len(line)>=len(key) and line[0:len(key)]==key:
self.valuesData[i]=key+" "+value.strip()
return 1
self.valuesData.append(key+" "+value)
return 2
def removeValue(self, key):
"""
Function to remove values
Parameters:
key string
return:
boolean
"""
for line in self.valuesData:
if len(line)>=len(key) and line[0:len(key)]==key:
self.valuesData.remove(line)
return True
return False
def clearSubnet(self):
"""
function that remove the subnets created or readed from file
"""
self.subnetData=[]
return self
def newSubnet(self, ip, netmask):
"""
This function create a new subnet
Parameters:
ip string
netmask string
Return:
int - index of new subnet
"""
newIndex=len(self.subnetData)
self.subnetData.append([])
self.subnetData[newIndex].append("subnet {} netmask {}".format(ip, netmask))
self.subnetData[newIndex].append("{")
self.subnetData[newIndex].append("}")
return newIndex
def getSubnetNumber(self):
"""
Function that return the number of subnets
Return:
int
"""
return len(self.subnetData)
def getSubnetValue(self, index, key):
"""
Function that return value for subnet
Parameteres:
index int - index of subnet
key string
Return:
string
"""
if len(self.subnetData)==0 or index<0 or index>len(self.subnetData)-1:
return ""
for line in self.subnetData[index]:
if len(line)>=len(key) and line[0:len(key)]==key:
return line
return ""
def setSubnetValue(self, index, key, value):
"""
Function to add or update values of the subnet
Parameters:
index int - define the subnet index
key string - key to update or insert
value string
Return:
integer - [1 updated, 2 insert]
"""
if len(self.subnetData)==0 or index<0 or index>len(self.subnetData)-1:
return self
for i in range(len(self.subnetData[index])):
line=self.subnetData[index][i]
if len(line)>=len(key) and line[0:len(key)]==key:
self.subnetData[index][i]=key+" "+value.strip()
return 1
self.subnetData[index][-1]=key+" "+value
self.subnetData[index].append("}")
return 2
def save(self):
"""
This function save the file with the changes
Return:
int - number of lines saved in a file | -1 not writable file
"""
if not os.access(self.file, os.W_OK):
return -1
linesSaved=0
with open(self.file, "w") as f:
f.write("# file created from class dhcpd.py\n")
f.write("# http://lwp-l.com/s5385\n\n")
for line in self.valuesData:
linesSaved+=1
f.write(line+"\n")
for subnets in self.subnetData:
for line in subnets:
linesSaved+=1
f.write(line+"\n")
return linesSaved
Comentarios sobre la versión: 0.2 (1)