Merge pull request #41103 from lorengordon/win.get_route

Adds a get_route() function to win_network.py
This commit is contained in:
Erik Johnson 2017-05-06 01:19:41 -05:00 committed by GitHub
commit 2af89beb53
2 changed files with 82 additions and 0 deletions

View File

@ -4,6 +4,8 @@ Module for gathering and managing network information
'''
from __future__ import absolute_import
import re
# Import salt libs
import salt.utils
import hashlib
@ -197,6 +199,35 @@ def nslookup(host):
return ret
def get_route(ip):
'''
Return routing information for given destination ip
.. versionadded:: 2016.11.6
CLI Example::
salt '*' network.get_route 10.10.10.10
'''
cmd = 'Find-NetRoute -RemoteIPAddress {0}'.format(ip)
out = __salt__['cmd.run'](cmd, shell='powershell', python_shell=True)
regexp = re.compile(
r"^IPAddress\s+:\s(?P<source>[\d\.:]+)?.*"
r"^InterfaceAlias\s+:\s(?P<interface>[\w\.\:\-\ ]+)?.*"
r"^NextHop\s+:\s(?P<gateway>[\d\.:]+)",
flags=re.MULTILINE | re.DOTALL
)
m = regexp.search(out)
ret = {
'destination': ip,
'gateway': m.group('gateway'),
'interface': m.group('interface'),
'source': m.group('source')
}
return ret
def dig(host):
'''
Performs a DNS lookup with dig

View File

@ -213,6 +213,57 @@ class WinNetworkTestCase(TestCase):
MagicMock(return_value=True)):
self.assertTrue(win_network.in_subnet('10.1.1.0/16'))
# 'get_route' function tests: 1
def test_get_route(self):
'''
Test if it return information on open ports and states
'''
ret = ('\n\n'
'IPAddress : 10.0.0.15\n'
'InterfaceIndex : 3\n'
'InterfaceAlias : Wi-Fi\n'
'AddressFamily : IPv4\n'
'Type : Unicast\n'
'PrefixLength : 24\n'
'PrefixOrigin : Dhcp\n'
'SuffixOrigin : Dhcp\n'
'AddressState : Preferred\n'
'ValidLifetime : 6.17:52:39\n'
'PreferredLifetime : 6.17:52:39\n'
'SkipAsSource : False\n'
'PolicyStore : ActiveStore\n'
'\n\n'
'Caption :\n'
'Description :\n'
'ElementName :\n'
'InstanceID : :8:8:8:9:55=55;:8;8;:8;55;\n'
'AdminDistance :\n'
'DestinationAddress :\n'
'IsStatic :\n'
'RouteMetric : 0\n'
'TypeOfRoute : 3\n'
'AddressFamily : IPv4\n'
'CompartmentId : 1\n'
'DestinationPrefix : 0.0.0.0/0\n'
'InterfaceAlias : Wi-Fi\n'
'InterfaceIndex : 3\n'
'NextHop : 10.0.0.1\n'
'PreferredLifetime : 6.23:14:43\n'
'Protocol : NetMgmt\n'
'Publish : No\n'
'Store : ActiveStore\n'
'ValidLifetime : 6.23:14:43\n'
'PSComputerName :\n'
'ifIndex : 3')
mock = MagicMock(return_value=ret)
with patch.dict(win_network.__salt__, {'cmd.run': mock}):
self.assertDictEqual(win_network.get_route('192.0.0.8'),
{'destination': '192.0.0.8',
'gateway': '10.0.0.1',
'interface': 'Wi-Fi',
'source': '10.0.0.15'})
if __name__ == '__main__':
from integration import run_tests