mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Handle ifconfig output differently for NetBSD >= 8.0
The netmask is now returned as part of the IP address in the ifconfig output. fixes #48856
This commit is contained in:
parent
ab1a713bc3
commit
80f8a667d1
1 changed files with 79 additions and 0 deletions
|
@ -36,6 +36,7 @@ import salt.utils.zeromq
|
|||
from salt._compat import ipaddress
|
||||
from salt.exceptions import SaltClientError, SaltSystemExit
|
||||
from salt.utils.decorators.jinja import jinja_filter
|
||||
from salt.utils.versions import LooseVersion
|
||||
|
||||
# inet_pton does not exist in Windows, this is a workaround
|
||||
if salt.utils.platform.is_windows():
|
||||
|
@ -865,6 +866,82 @@ def linux_interfaces():
|
|||
ifaces = _interfaces_ifconfig(salt.utils.stringutils.to_str(cmd))
|
||||
return ifaces
|
||||
|
||||
def _netbsd_interfaces_ifconfig(out):
|
||||
'''
|
||||
Uses ifconfig to return a dictionary of interfaces with various information
|
||||
about each (up/down state, ip address, netmask, and hwaddr)
|
||||
'''
|
||||
ret = dict()
|
||||
|
||||
piface = re.compile(r'^([^\s:]+)')
|
||||
pmac = re.compile('.*?address: ([0-9a-f:]+)')
|
||||
|
||||
pip = re.compile(r'.*?inet [^\d]*(.*?)/([\d]*)\s')
|
||||
pip6 = re.compile('.*?inet6 ([0-9a-f:]+)%([a-zA-Z0-9]*)/([\d]*)\s')
|
||||
|
||||
pupdown = re.compile('UP')
|
||||
pbcast = re.compile(r'.*?broadcast ([\d\.]+)')
|
||||
|
||||
groups = re.compile('\r?\n(?=\\S)').split(out)
|
||||
for group in groups:
|
||||
data = dict()
|
||||
iface = ''
|
||||
updown = False
|
||||
for line in group.splitlines():
|
||||
miface = piface.match(line)
|
||||
mmac = pmac.match(line)
|
||||
mip = pip.match(line)
|
||||
mip6 = pip6.match(line)
|
||||
mupdown = pupdown.search(line)
|
||||
if miface:
|
||||
iface = miface.group(1)
|
||||
if mmac:
|
||||
data['hwaddr'] = mmac.group(1)
|
||||
if mip:
|
||||
if 'inet' not in data:
|
||||
data['inet'] = list()
|
||||
addr_obj = dict()
|
||||
addr_obj['address'] = mip.group(1)
|
||||
mmask = mip.group(2)
|
||||
if mip.group(2):
|
||||
addr_obj['netmask'] = cidr_to_ipv4_netmask(mip.group(2))
|
||||
mbcast = pbcast.match(line)
|
||||
if mbcast:
|
||||
addr_obj['broadcast'] = mbcast.group(1)
|
||||
data['inet'].append(addr_obj)
|
||||
if mupdown:
|
||||
updown = True
|
||||
if mip6:
|
||||
if 'inet6' not in data:
|
||||
data['inet6'] = list()
|
||||
addr_obj = dict()
|
||||
addr_obj['address'] = mip6.group(1)
|
||||
mmask6 = mip6.group(3)
|
||||
addr_obj['scope'] = mip6.group(2)
|
||||
addr_obj['prefixlen'] = mip6.group(3)
|
||||
data['inet6'].append(addr_obj)
|
||||
data['up'] = updown
|
||||
ret[iface] = data
|
||||
del data
|
||||
return ret
|
||||
|
||||
def netbsd_interfaces():
|
||||
'''
|
||||
Obtain interface information for NetBSD >= 8 where the ifconfig
|
||||
output diverged from other BSD variants (Netmask is now part of the
|
||||
address)
|
||||
'''
|
||||
# NetBSD versions prior to 8.0 can still use linux_interfaces()
|
||||
if LooseVersion(os.uname()[2]) < LooseVersion('8.0'):
|
||||
return linux_interfaces()
|
||||
|
||||
ifconfig_path = salt.utils.path.which('ifconfig')
|
||||
cmd = subprocess.Popen(
|
||||
'{0} -a'.format(ifconfig_path),
|
||||
shell=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT).communicate()[0]
|
||||
return _netbsd_interfaces_ifconfig(salt.utils.stringutils.to_str(cmd))
|
||||
|
||||
def _interfaces_ipconfig(out):
|
||||
'''
|
||||
|
@ -967,6 +1044,8 @@ def interfaces():
|
|||
'''
|
||||
if salt.utils.platform.is_windows():
|
||||
return win_interfaces()
|
||||
elif salt.utils.platform.is_netbsd():
|
||||
return netbsd_interfaces()
|
||||
else:
|
||||
return linux_interfaces()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue