admin管理员组文章数量:1025491
I want to parse ifconfig to get ip_address, net mask and broadcast. and these are optional fields. If it present, it should return but if not it should return None.
My below pattern works fine but if 'inet6' is not present. if not, then this pattern returns None. I am not sure why it is still trying to match with inet6.
output = """
en0: flags=8863<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:1a:2b:3c:4d:5e
inet6 fe80::1a2b:3c4d:5e6f:7g8h prefixlen 64 secured scopeid 0x4
inet 192.168.1.10 netmask 0xffffff00 broadcast 192.168.1.255
"""
# Regex pattern to capture MAC, IPv4 (inet), and IPv6 (inet6) addresses
pattern = repile(r'ether (?P<mac_address>[a-f0-9:]+).*?'
r'(?:\s+inet (?P<ip_address>[\d\.]+) '
r'netmask (?P<netmask>0x[a-f0-9]+) '
r'broadcast (?P<broadcast>[.a-f0-9:]+))?.*?')
print(pattern.search(output).groupdict())
I want to parse ifconfig to get ip_address, net mask and broadcast. and these are optional fields. If it present, it should return but if not it should return None.
My below pattern works fine but if 'inet6' is not present. if not, then this pattern returns None. I am not sure why it is still trying to match with inet6.
output = """
en0: flags=8863<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:1a:2b:3c:4d:5e
inet6 fe80::1a2b:3c4d:5e6f:7g8h prefixlen 64 secured scopeid 0x4
inet 192.168.1.10 netmask 0xffffff00 broadcast 192.168.1.255
"""
# Regex pattern to capture MAC, IPv4 (inet), and IPv6 (inet6) addresses
pattern = repile(r'ether (?P<mac_address>[a-f0-9:]+).*?'
r'(?:\s+inet (?P<ip_address>[\d\.]+) '
r'netmask (?P<netmask>0x[a-f0-9]+) '
r'broadcast (?P<broadcast>[.a-f0-9:]+))?.*?')
print(pattern.search(output).groupdict())
Share
Improve this question
asked Nov 17, 2024 at 6:38
premganeshpremganesh
853 silver badges9 bronze badges
3
|
1 Answer
Reset to default 0Try:
ether (?P<mac_address>[a-f0-9:]+)(?:(?!inet\b)[\w\W])+(?:inet (?P<ip_address>[\d.]+) netmask (?P<netmask>0x[a-f0-9]+) broadcast (?P<broadcast>[.a-f0-9:]+))?
See: regex101
See Python Demo:
import re
output = """
en0: flags=8863<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:1a:2b:3c:4d:5e
inet6 fe80::1a2b:3c4d:5e6f:7g8h prefixlen 64 secured scopeid 0x4
inet 192.168.1.10 netmask 0xffffff00 broadcast 192.168.1.255
"""
# Regex pattern to capture MAC, IPv4 (inet), and IPv6 (inet6) addresses
pattern = repile(r'ether (?P<mac_address>[a-f0-9:]+)(?:(?!inet\b)[\w\W])+(?:inet (?P<ip_address>[\d.]+) netmask (?P<netmask>0x[a-f0-9]+) broadcast (?P<broadcast>[.a-f0-9:]+))?')
print(pattern.search(output).groupdict())
Explanation
ether (?P<mac_address>[a-f0-9:]+)
: your ether part(?:
: then using a tempered greedy token(?!inet\b)
: that asserts you match up to exactlyinet
[\w\W]
: any character (including newlines)
)+
:(?:inet (?P<ip_address>[\d.]+) netmask (?P<netmask>0x[a-f0-9]+) broadcast (?P<broadcast>[.a-f0-9:]+))?
: then your inet, netmask, broadcast part
I want to parse ifconfig to get ip_address, net mask and broadcast. and these are optional fields. If it present, it should return but if not it should return None.
My below pattern works fine but if 'inet6' is not present. if not, then this pattern returns None. I am not sure why it is still trying to match with inet6.
output = """
en0: flags=8863<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:1a:2b:3c:4d:5e
inet6 fe80::1a2b:3c4d:5e6f:7g8h prefixlen 64 secured scopeid 0x4
inet 192.168.1.10 netmask 0xffffff00 broadcast 192.168.1.255
"""
# Regex pattern to capture MAC, IPv4 (inet), and IPv6 (inet6) addresses
pattern = repile(r'ether (?P<mac_address>[a-f0-9:]+).*?'
r'(?:\s+inet (?P<ip_address>[\d\.]+) '
r'netmask (?P<netmask>0x[a-f0-9]+) '
r'broadcast (?P<broadcast>[.a-f0-9:]+))?.*?')
print(pattern.search(output).groupdict())
I want to parse ifconfig to get ip_address, net mask and broadcast. and these are optional fields. If it present, it should return but if not it should return None.
My below pattern works fine but if 'inet6' is not present. if not, then this pattern returns None. I am not sure why it is still trying to match with inet6.
output = """
en0: flags=8863<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:1a:2b:3c:4d:5e
inet6 fe80::1a2b:3c4d:5e6f:7g8h prefixlen 64 secured scopeid 0x4
inet 192.168.1.10 netmask 0xffffff00 broadcast 192.168.1.255
"""
# Regex pattern to capture MAC, IPv4 (inet), and IPv6 (inet6) addresses
pattern = repile(r'ether (?P<mac_address>[a-f0-9:]+).*?'
r'(?:\s+inet (?P<ip_address>[\d\.]+) '
r'netmask (?P<netmask>0x[a-f0-9]+) '
r'broadcast (?P<broadcast>[.a-f0-9:]+))?.*?')
print(pattern.search(output).groupdict())
Share
Improve this question
asked Nov 17, 2024 at 6:38
premganeshpremganesh
853 silver badges9 bronze badges
3
- 1 This does not have to be a huge regex. Just read the lines, split by words, and if the first word is "inet" you know the IP is the next word. – Tim Roberts Commented Nov 17, 2024 at 6:41
-
That is because the line starting with
inet6
is not matched, which you can make optional in this case. See regex101/r/vGUB6I/1 – The fourth bird Commented Nov 17, 2024 at 9:30 -
Parsing
ifconfig
or etc output is a common FAQ; please search before asking. On many modern platforms, it is easy and obvious how to get the results directly in a machine readable form so you don't have to write yet another ad-hoc parser. – tripleee Commented Nov 18, 2024 at 14:00
1 Answer
Reset to default 0Try:
ether (?P<mac_address>[a-f0-9:]+)(?:(?!inet\b)[\w\W])+(?:inet (?P<ip_address>[\d.]+) netmask (?P<netmask>0x[a-f0-9]+) broadcast (?P<broadcast>[.a-f0-9:]+))?
See: regex101
See Python Demo:
import re
output = """
en0: flags=8863<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:1a:2b:3c:4d:5e
inet6 fe80::1a2b:3c4d:5e6f:7g8h prefixlen 64 secured scopeid 0x4
inet 192.168.1.10 netmask 0xffffff00 broadcast 192.168.1.255
"""
# Regex pattern to capture MAC, IPv4 (inet), and IPv6 (inet6) addresses
pattern = repile(r'ether (?P<mac_address>[a-f0-9:]+)(?:(?!inet\b)[\w\W])+(?:inet (?P<ip_address>[\d.]+) netmask (?P<netmask>0x[a-f0-9]+) broadcast (?P<broadcast>[.a-f0-9:]+))?')
print(pattern.search(output).groupdict())
Explanation
ether (?P<mac_address>[a-f0-9:]+)
: your ether part(?:
: then using a tempered greedy token(?!inet\b)
: that asserts you match up to exactlyinet
[\w\W]
: any character (including newlines)
)+
:(?:inet (?P<ip_address>[\d.]+) netmask (?P<netmask>0x[a-f0-9]+) broadcast (?P<broadcast>[.a-f0-9:]+))?
: then your inet, netmask, broadcast part
本文标签: python 3xRegex get parse inet valueStack Overflow
版权声明:本文标题:python 3.x - Regex get parse inet value - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745636795a2160500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
inet6
is not matched, which you can make optional in this case. See regex101/r/vGUB6I/1 – The fourth bird Commented Nov 17, 2024 at 9:30ifconfig
or etc output is a common FAQ; please search before asking. On many modern platforms, it is easy and obvious how to get the results directly in a machine readable form so you don't have to write yet another ad-hoc parser. – tripleee Commented Nov 18, 2024 at 14:00