使用python从dhcpd.leases中删除具有mac的租约? [英] Removing a lease that has giving mac from dhcpd.leases with python?

查看:204
本文介绍了使用python从dhcpd.leases中删除具有mac的租约?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据python的mac地址使用python从dhcpd.lease中删除租约.

I am trying to remove a lease from dhcpd.lease with python according to its mac address.

这是dhcpd.lease示例

This is a dhcpd.lease example

lease 10.14.53.253 {
  starts 3 2012/10/17 09:27:20;
  ends 4 2012/10/18 09:27:20;
  tstp 4 2012/10/18 09:27:20;
  binding state free;
  hardware ethernet 00:23:18:62:31:5b;
} 
lease 10.14.53.252 {
  starts 3 2012/10/17 10:15:17;
  ends 4 2012/10/18 10:15:17;
  tstp 4 2012/10/18 10:15:17;
  binding state free;
  hardware ethernet 70:71:bc:c8:46:3c;
  uid "\001pq\274\310F<";
}

假设我得到的是"00:23:18:62:31:5b".然后,我应该删除属于此租约的所有行.删除后,文件应类似于

Assume I am given '00:23:18:62:31:5b'. Then I should remove all line belong to this lease. After deletion, file should look like

lease 10.14.53.252 {
  starts 3 2012/10/17 10:15:17;
  ends 4 2012/10/18 10:15:17;
  tstp 4 2012/10/18 10:15:17;
  binding state free;
  hardware ethernet 70:71:bc:c8:46:3c;
  uid "\001pq\274\310F<";
}

我很简单地读取文件并将其放入字符串,但是在此之后我不知道该怎么办.我试过这个正则表达式,但没有用.它仅检查文件的第一行.

I am simple reading a file and put it a string but I have no idea what to do after that. I tried this regex but didn't work. It checked only first line of the file.

fh = open(DHCPFILE)
lines = fh.read()
fh.close()
m = re.match(r"(.*lease.*%s.*})" % mac ,lines)

推荐答案

这个问题的形状不是正则表达式钉子,所以请放下锤子.

This problem is not shaped like a regular expression nail, so please put that hammer down.

正确的工具是将内容解析为python结构,过滤掉不需要的项目,然后再次写出剩余的条目.

The correct tool would be to parse the contents into a python structure, filtering out the items you don't want, then writing out the remaining entries again.

pyparsing 将使解析工作变得容易;以下内容基于现有示例:

pyparsing would make the parsing job easy; the following is based on an existing example:

from pyparsing import *

LBRACE,RBRACE,SEMI,QUOTE = map(Suppress,'{};"')
ipAddress = Combine(Word(nums) + ('.' + Word(nums))*3)
hexint = Word(hexnums,exact=2)
macAddress = Combine(hexint + (':'+hexint)*5)
hdwType = Word(alphanums)

yyyymmdd = Combine((Word(nums,exact=4)|Word(nums,exact=2))+
                    ('/'+Word(nums,exact=2))*2)
hhmmss = Combine(Word(nums,exact=2)+(':'+Word(nums,exact=2))*2)
dateRef = oneOf(list("0123456"))("weekday") + yyyymmdd("date") + \
                                                        hhmmss("time")

startsStmt = "starts" + dateRef + SEMI
endsStmt = "ends" + (dateRef | "never") + SEMI
tstpStmt = "tstp" + dateRef + SEMI
tsfpStmt = "tsfp" + dateRef + SEMI
hdwStmt = "hardware" + hdwType("type") + macAddress("mac") + SEMI
uidStmt = "uid" + QuotedString('"')("uid") + SEMI
bindingStmt = "binding" + Word(alphanums) + Word(alphanums) + SEMI

leaseStatement = startsStmt | endsStmt | tstpStmt | tsfpStmt | hdwStmt | \
                                                        uidStmt | bindingStmt
leaseDef = "lease" + ipAddress("ipaddress") + LBRACE + \
                            Dict(ZeroOrMore(Group(leaseStatement))) + RBRACE

input = open(DHCPLEASEFILE).read()
with open(OUTPUTFILE, 'w') as output:
    for lease, start, stop in leaseDef.scanString(input):
        if lease.hardware.mac != mac:
            output.write(input[start:stop])

上面的代码简洁地定义了dhcp.leases文件的语法,然后使用scanString()解析出文件中的每个租约. scanString()返回一个匹配项序列,每个匹配项都包含一个解析结果以及原始字符串中的开始和结束位置.

The above code tersely defines the grammar of a dhcp.leases file, then uses scanString() to parse out each lease in the file. scanString() returns a sequence of matches, each consisting of a parse result and the start and end positions in the original string.

解析结果具有.hardware.mac属性(如果输入中不存在任何硬件语句,您可能希望捕获AttributeError异常),从而很容易测试要删除的MAC地址.如果MAC地址不匹配,我们使用startstop位置将整个租约写回到输出文件中,以获取该租约的原始文本(比从解析的信息格式化租约更容易)

The parse result has a .hardware.mac attribute (you may want to catch AttributeError exceptions on that, in case no hardware statement was present in the input), making it easy to test for your MAC address to remove. If the MAC address doesn't match, we write the whole lease back to an output file, using the start and stop positions to get the original text for that lease (much easier than formatting the lease from the parsed information).

这篇关于使用python从dhcpd.leases中删除具有mac的租约?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆