add get_route function to network module

This commit is contained in:
Krzysztof Pawłowski 2015-06-17 12:53:15 +02:00
parent c8713f2d00
commit f3d184c478

View file

@ -1108,3 +1108,29 @@ def default_route(family=None):
ret.append(route)
return ret
def get_route(ip):
'''
Return routing information for given destination ip
CLI Example::
salt '*' network.get_route 10.10.10.10
'''
if __grains__['kernel'] == 'Linux':
cmd = 'ip route get {0}'.format(ip)
out = __salt__['cmd.run'](cmd, python_shell=True)
regexp = re.compile(r'(via\s+(?P<gateway>[\w\.:]+))?\s+dev\s+(?P<interface>[\w\W]+)\s+src\s+(?P<source>[\w\.:]+)')
m = regexp.search(out.splitlines()[0])
ret = {
'destination': ip,
'gateway': m.group('gateway'),
'interface': m.group('interface'),
'source': m.group('source')}
return ret
else:
raise CommandExecutionError('Not yet supported on this platform')