diff --git a/salt/modules/network.py b/salt/modules/network.py index e5fa4f05b1..8b4dfcead4 100644 --- a/salt/modules/network.py +++ b/salt/modules/network.py @@ -848,17 +848,16 @@ def traceroute(host): if salt.utils.platform.is_sunos() or salt.utils.platform.is_aix(): traceroute_version = [0, 0, 0] else: - cmd2 = 'traceroute --version' - out2 = __salt__['cmd.run'](cmd2) + version_out = __salt__['cmd.run']('traceroute --version') try: # Linux traceroute version looks like: # Modern traceroute for Linux, version 2.0.19, Dec 10 2012 # Darwin and FreeBSD traceroute version looks like: Version 1.4a12+[FreeBSD|Darwin] - traceroute_version_raw = re.findall(r'.*[Vv]ersion (\d+)\.([\w\+]+)\.*(\w*)', out2)[0] - log.debug('traceroute_version_raw: %s', traceroute_version_raw) + version_raw = re.findall(r'.*[Vv]ersion (\d+)\.([\w\+]+)\.*(\w*)', version_out)[0] + log.debug('traceroute_version_raw: %s', version_raw) traceroute_version = [] - for t in traceroute_version_raw: + for t in version_raw: try: traceroute_version.append(int(t)) except ValueError: @@ -873,26 +872,28 @@ def traceroute(host): traceroute_version = [0, 0, 0] for line in out.splitlines(): + # Pre requirements for line parsing + skip_line = False if ' ' not in line: - continue + skip_line = True if line.startswith('traceroute'): - continue - + skip_line = True if salt.utils.platform.is_aix(): if line.startswith('trying to get source for'): - continue - + skip_line = True if line.startswith('source should be'): - continue - + skip_line = True if line.startswith('outgoing MTU'): - continue - + skip_line = True if line.startswith('fragmentation required'): - continue + skip_line = True + if skip_line: + log.debug('Skipping traceroute output line: %s', line) + continue + # Parse output from unix variants if 'Darwin' in six.text_type(traceroute_version[1]) or \ - 'FreeBSD' in six.text_type(traceroute_version[1]) or \ + 'FreeBSD' in six.text_type(traceroute_version[1]) or \ __grains__['kernel'] in ('SunOS', 'AIX'): try: traceline = re.findall(r'\s*(\d*)\s+(.*)\s+\((.*)\)\s+(.*)$', line)[0] @@ -919,14 +920,15 @@ def traceroute(host): except IndexError: result = {} + # Parse output from specific version ranges elif (traceroute_version[0] >= 2 and traceroute_version[2] >= 14 or traceroute_version[0] >= 2 and traceroute_version[1] > 0): comps = line.split(' ') - if comps[1] == '* * *': + if len(comps) >= 2 and comps[1] == '* * *': result = { 'count': int(comps[0]), 'hostname': '*'} - else: + elif len(comps) >= 5: result = { 'count': int(comps[0]), 'hostname': comps[1].split()[0], @@ -934,21 +936,29 @@ def traceroute(host): 'ms1': float(comps[2].split()[0]), 'ms2': float(comps[3].split()[0]), 'ms3': float(comps[4].split()[0])} + else: + result = {} + + # Parse anything else else: comps = line.split() - result = { - 'count': comps[0], - 'hostname': comps[1], - 'ip': comps[2], - 'ms1': comps[4], - 'ms2': comps[6], - 'ms3': comps[8], - 'ping1': comps[3], - 'ping2': comps[5], - 'ping3': comps[7]} + if len(comps) >= 8: + result = { + 'count': comps[0], + 'hostname': comps[1], + 'ip': comps[2], + 'ms1': comps[4], + 'ms2': comps[6], + 'ms3': comps[8], + 'ping1': comps[3], + 'ping2': comps[5], + 'ping3': comps[7]} + else: + result = {} ret.append(result) - + if not result: + log.warn('Cannot parse traceroute output line: %s', line) return ret