python - Python 3 正则表达式租赁 DHCP
问题描述
我在 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)
我有几种单独的模式来查找结果,但我不知道如何将它们组合在一起以满足条件
当且仅当“绑定状态处于活动状态;”
解决方案
我们可以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"']
推荐阅读
- ruby-on-rails - Rails 5.2:从初始化程序中要求 ActiveRecord
- c# - 赋值循环回车
- python - matplotlib极地流图与颤动
- wordpress - Wordpress - 在 functions.php 上获取当前语言
- c# - 如何在 Unity 中记录游戏对象碰撞
- android - android listview 在其他元素之上显示项目
- css - 如何仅使用 vue 和 css 使用动画制作背景颜色切换
- server-sent-events - 使用 Koa 处理服务器发送的事件
- react-native - React Native 按钮错误问题,不能有点击监听
- python-multithreading - Python:运行一个在新线程中接受参数的类方法