Skip to content

Commit

Permalink
Merge pull request #31196 from sakateka/utils-network-fix
Browse files Browse the repository at this point in the history
Here are a few fixes utils.network
  • Loading branch information
Mike Place committed Feb 18, 2016
2 parents d20a30b + a7b1102 commit a2f6447
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions salt/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,14 +973,17 @@ def ip_addrs6(interface=None, include_loopback=False, interface_data=None):
def hex2ip(hex_ip, invert=False):
'''
Convert a hex string to an ip, if a failure occurs the original hex is
returned
returned. If 'invert=True' assume that ip from /proc/net/<proto>
'''
if len(hex_ip) == 32: # ipv6
ip = []
for i in range(0, 32, 8):
ip_part = hex_ip[i:i + 8]
ip_part = [ip_part[x:x + 2] for x in range(0, 8, 2)]
ip.append("{0[3]}{0[2]}:{0[1]}{0[0]}".format(ip_part))
if invert:
ip.append("{0[3]}{0[2]}:{0[1]}{0[0]}".format(ip_part))
else:
ip.append("{0[0]}{0[1]}:{0[2]}{0[3]}".format(ip_part))
try:
return ipaddress.IPv6Address(":".join(ip)).compressed
except ipaddress.AddressValueError as ex:
Expand Down Expand Up @@ -1061,8 +1064,10 @@ def _remotes_on(port, which_end):
port = int(port)
ret = set()

proc_available = False
for statf in ['/proc/net/tcp', '/proc/net/tcp6']:
if os.path.isfile(statf):
proc_available = True
with salt.utils.fopen(statf, 'rb') as fp_:
for line in fp_:
if line.strip().startswith('sl'):
Expand All @@ -1072,7 +1077,7 @@ def _remotes_on(port, which_end):
if iret[sl][which_end] == port:
ret.add(iret[sl]['remote_addr'])

if not ret: # Fallback to use 'lsof' if /proc not available
if not proc_available: # Fallback to use OS specific tools
if salt.utils.is_sunos():
return _sunos_remotes_on(port, which_end)
if salt.utils.is_freebsd():
Expand Down Expand Up @@ -1298,6 +1303,11 @@ def _linux_remotes_on(port, which_end):
['lsof', '-iTCP:{0:d}'.format(port), '-n', '-P'] # pylint: disable=minimum-python-version
)
except subprocess.CalledProcessError as ex:
if ex.returncode == 1:
# Lsof return 1 if any error was detected, including the failure
# to locate Internet addresses, and it is not an error in this case.
log.warning('"lsof" returncode = 1, likely no active TCP sessions.')
return remotes
log.error('Failed "lsof" with returncode = {0}'.format(ex.returncode))
raise

Expand Down

0 comments on commit a2f6447

Please sign in to comment.