【问题标题】:Python 3 Regular expression Lease DHCPPython 3 正则表达式租赁 DHCP
【发布时间】:2021-08-23 03:40:30
【问题描述】:

我在 DHCP 租用文件中有以下信息

lease 201.249.14.226 { 
  starts epoch 1586469086; # Thu Apr 09 17:51:26 2020
  ends epoch 1587765086; # Fri Apr 24 17:51:26 2020
  tstp epoch 1587765086; # Fri Apr 24 17:51:26 2020
  cltt epoch 1586469086; # Thu Apr 09 17:51:26 2020
  binding state free;
  next binding state free;
  rewind binding state free;
  hardware ethernet f8:d1:11:50:1c:2b;
  set vendor-class-identifier = "MSFT 5.0";
  client-hostname "TL-MR3420";
}
lease 201.249.11.68 {
  starts epoch 1586469229; # Thu Apr 09 17:53:49 2020
  ends epoch 1587765229; # Fri Apr 24 17:53:49 2020
  tstp epoch 1587765229; # Fri Apr 24 17:53:49 2020
  cltt epoch 1586469440; # Thu Apr 09 17:57:20 2020
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 34:4d:ea:fa:9d:10;
  option agent.circuit-id "mil-bras-06--172.26.22.237 trunk 0/1/0/2:427.32";
  client-hostname "ZTE";
}
lease 201.249.11.199 {
  starts epoch 1586469229; # Thu Apr 09 17:53:49 2020
  ends epoch 1587765229; # Fri Apr 24 17:53:49 2020
  tstp epoch 1587765229; # Fri Apr 24 17:53:49 2020
  cltt epoch 1586469440; # Thu Apr 09 17:57:20 2020
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 34:4d:ea:fa:9d:10;
  option agent.circuit-id "cnt-bras-07--172.26.220.225 trunk 0/1/0/2:427.20";
  client-hostname "ZTE";
}

是一个缩减样本

我需要在应用Python re后得到 当且仅当“绑定状态激活”;

下一个:

lease 201.249.11.68
binding state active;
hardware ethernet 34: 4d: ea: fa: 9d: 10;
option agent.circuit-id "mil-bras-06--172.26.22.237 trunk 0/1/0/2: 427.32";
client-hostname "ZTE";

lease 201.249.11.199
binding state active;
hardware ethernet 34: 4d: ea: fa: 9d: 10;
option agent.circuit-id "cnt-bras-07--172.26.220.225 trunk 0/1/0/2: 427.20";
client-hostname "ZTE"; 

不强制使用python模块(正则表达式) 不知道有没有更好的想法或者替代方案

我正在这样做......

import re
with open('/var/lib/dhcp.leases', 'r') as f:
    #pattern = re.compile(r'((option agent.circuit-id)\s"(\w{3}-\w{4}-\d{2}\W{2}))')
    #pattern = re.compile(r'(\W{2}((\d{0,3}).(\d{0,3}).(\d{0,3}).(\d{0,3})\s\w{5}\s[0-9/]{7}:\d{1,3}.\d{1,3}"))')(
    #pattern = re.compile(r'(lease [0-9.]+)')
    #pattern = re.compile(r'binding state active;')
    #pattern = re.compile(r'(hardware ethernet).(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2})')
    #pattern = re.compile(r'(option agent.circuit-id)\s["]+(\w{3}-\w{4}-\d{2}\W{2}\d{1,3}[.]\d{1,3}[.]\d{1,3}.*?)')
    contents = f.read()
    matches = pattern.finditer(contents)
    for match in matches:
        print(match)

我有几个单独的模式来查找结果 但我不知道如何将它们放在一起满足条件

当且仅当“绑定状态激活;”

【问题讨论】:

    标签: python python-3.8 python-re


    【解决方案1】:

    我们可以在这里尝试使用re.findall,使用正则表达式模式来定位和捕获所需的文本:

    matches = re.findall(r'(lease \d+(?:\.\d+){3}) \{[^}]+(binding state active;)[^}]+(hardware[^}]+option[^}]+client[^;]+)', inp)
    output = ['\n'.join(x) for x in matches]
    print(output)
    

    打印出来:

    ['lease 201.249.11.68\nbinding state active;\nhardware ethernet 34:4d:ea:fa:9d:10;\n  option agent.circuit-id "mil-bras-06--172.26.22.237 trunk 0/1/0/2:427.32";\n  client-hostname "ZTE"',
     'lease 201.249.11.199\nbinding state active;\nhardware ethernet 34:4d:ea:fa:9d:10;\n  option agent.circuit-id "cnt-bras-07--172.26.220.225 trunk 0/1/0/2:427.20";\n  client-hostname "ZTE"']
    

    【讨论】:

    • 不知道怎么实现
    • 请详细说明你不知道的。在我上面的回答中,只需将您的文本文件读入inp 变量,然后使用我的代码。
    • 工作,谢谢蒂姆
    【解决方案2】:

    我会使用awk(因为你提到python不是必需的),如下(1行有问题但可以修复):

    Mac_3.2.57$# save lease line;  if bind active, print lease line and active line;  look for other lines and print iff bind is active (finding } logically DE-acivates); plus some format
    Mac_3.2.57$cat input.txt | awk '{if($1=="lease"){L=$0;sub(/{/,"",L)};if($0~/ *binding state active; */){print L;f=1};if($0!~/ *next binding state free; */&&$0!~/ *rewind binding state free; */&&$0!~/ *} */&&f==1){print $0};if($0~/ *} */){f=0};if(f==2){print $0}}'
    lease 201.249.11.68 
      binding state active;
      hardware ethernet 34:4d:ea:fa:9d:10;
      option agent.circuit-id "mil-bras-06--172.26.22.237 trunk 0/1/0/2:427.32";
      client-hostname "ZTE";
    lease 201.249.11.199 
      binding state active;
      hardware ethernet 34:4d:ea:fa:9d:10;
      option agent.circuit-id "cnt-bras-07--172.26.220.225 trunk 0/1/0/2:427.20";
      client-hostname "ZTE";
    Mac_3.2.57$
    Mac_3.2.57$cat input.txt 
    lease 201.249.14.226 { 
      starts epoch 1586469086; # Thu Apr 09 17:51:26 2020
      ends epoch 1587765086; # Fri Apr 24 17:51:26 2020
      tstp epoch 1587765086; # Fri Apr 24 17:51:26 2020
      cltt epoch 1586469086; # Thu Apr 09 17:51:26 2020
      binding state free;
      next binding state free;
      rewind binding state free;
      hardware ethernet f8:d1:11:50:1c:2b;
      set vendor-class-identifier = "MSFT 5.0";
      client-hostname "TL-MR3420";
    }
    lease 201.249.11.68 {
      starts epoch 1586469229; # Thu Apr 09 17:53:49 2020
      ends epoch 1587765229; # Fri Apr 24 17:53:49 2020
      tstp epoch 1587765229; # Fri Apr 24 17:53:49 2020
      cltt epoch 1586469440; # Thu Apr 09 17:57:20 2020
      binding state active;
      next binding state free;
      rewind binding state free;
      hardware ethernet 34:4d:ea:fa:9d:10;
      option agent.circuit-id "mil-bras-06--172.26.22.237 trunk 0/1/0/2:427.32";
      client-hostname "ZTE";
    }
    lease 201.249.11.199 {
      starts epoch 1586469229; # Thu Apr 09 17:53:49 2020
      ends epoch 1587765229; # Fri Apr 24 17:53:49 2020
      tstp epoch 1587765229; # Fri Apr 24 17:53:49 2020
      cltt epoch 1586469440; # Thu Apr 09 17:57:20 2020
      binding state active;
      next binding state free;
      rewind binding state free;
      hardware ethernet 34:4d:ea:fa:9d:10;
      option agent.circuit-id "cnt-bras-07--172.26.220.225 trunk 0/1/0/2:427.20";
      client-hostname "ZTE";
    }
    Mac_3.2.57$
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 2011-08-06
      • 2013-06-04
      相关资源
      最近更新 更多