mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Better ipv4/ipv6 addresses extracting and returning
This commit is contained in:
parent
8880dc8a3d
commit
4396baa584
1 changed files with 33 additions and 21 deletions
|
@ -169,7 +169,9 @@ def monitor_network(units=None, human_readable=False):
|
||||||
output = subprocess.check_output('ip route show'.split())
|
output = subprocess.check_output('ip route show'.split())
|
||||||
m = re.search('default via (.*) dev ([a-z]+[0-9]?)', output)
|
m = re.search('default via (.*) dev ([a-z]+[0-9]?)', output)
|
||||||
if m:
|
if m:
|
||||||
gateway = _extract_inet(m.group(1), True)
|
addr = _extract_inet(m.group(1), True)
|
||||||
|
if len(addr) == 1:
|
||||||
|
proto, gateway = addr.popitem()
|
||||||
|
|
||||||
result[u] = {
|
result[u] = {
|
||||||
'public_ip': p_ip,
|
'public_ip': p_ip,
|
||||||
|
@ -409,36 +411,46 @@ def _get_glances_api():
|
||||||
raise YunoHostError(1, _("Connection to Glances server failed"))
|
raise YunoHostError(1, _("Connection to Glances server failed"))
|
||||||
|
|
||||||
|
|
||||||
def _extract_inet(string, skip_netmask=False):
|
def _extract_inet(string, skip_netmask=False, skip_loopback=True):
|
||||||
"""
|
"""
|
||||||
Extract IP address (v4 or v6) from a string
|
Extract IP addresses (v4 and/or v6) from a string limited to one
|
||||||
|
address by protocol
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
string -- String to search in
|
string -- String to search in
|
||||||
|
skip_netmask -- True to skip subnet mask extraction
|
||||||
|
skip_loopback -- False to include addresses reserved for the
|
||||||
|
loopback interface
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A dict of {protocol: address} with protocol one of 'ipv4' or 'ipv6'
|
||||||
|
|
||||||
"""
|
"""
|
||||||
ip4 = '((25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}'
|
ip4_pattern = '((25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}'
|
||||||
ip6 = '(((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)'
|
ip6_pattern = '(((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)'
|
||||||
ip4 += '/[0-9]{1,2})' if not skip_netmask else ')'
|
ip4_pattern += '/[0-9]{1,2})' if not skip_netmask else ')'
|
||||||
ip6 += '/[0-9]{1,2})' if not skip_netmask else ')'
|
ip6_pattern += '/[0-9]{1,2})' if not skip_netmask else ')'
|
||||||
|
result = {}
|
||||||
|
|
||||||
ip4_prog = re.compile(ip4)
|
for m in re.finditer(ip4_pattern, string):
|
||||||
ip6_prog = re.compile(ip6)
|
addr = m.group(1)
|
||||||
result = []
|
if skip_loopback and addr.startswith('127.'):
|
||||||
|
continue
|
||||||
|
|
||||||
m = ip4_prog.search(string)
|
# Limit to only one result
|
||||||
if m:
|
result['ipv4'] = addr
|
||||||
result.append(m.group(1))
|
break
|
||||||
|
|
||||||
m = ip6_prog.search(string)
|
for m in re.finditer(ip6_pattern, string):
|
||||||
if m:
|
addr = m.group(1)
|
||||||
result.append(m.group(1))
|
if skip_loopback and addr == '::1':
|
||||||
|
continue
|
||||||
|
|
||||||
if len(result) > 1:
|
# Limit to only one result
|
||||||
return result
|
result['ipv6'] = addr
|
||||||
elif len(result) == 1:
|
break
|
||||||
return result[0]
|
|
||||||
return None
|
return result
|
||||||
|
|
||||||
|
|
||||||
def _binary_to_human(n, customary=False):
|
def _binary_to_human(n, customary=False):
|
||||||
|
|
Loading…
Add table
Reference in a new issue