Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added default interface detection for Windows.. addresses issue #25 #56

Merged
merged 2 commits into from
Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ifcfg/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def get_patterns(cls):
r"^ Physical Address. . . . . . . . . : (?P<ether>[ABCDEFabcdef\d-]+)",
r"^ IPv4 Address. . . . . . . . . . . : (?P<inet4>[^\s\(]+)",
r"^ IPv6 Address. . . . . . . . . . . : (?P<inet6>[ABCDEFabcdef\d\:\%]+)",
r"^\s+Default Gateway . . . . . . . . . : (?P<default_gateway>[^\s\(]+)",
]

@property
Expand All @@ -209,6 +210,15 @@ def alter(self, interfaces):
interfaces[device]['ether'] = device_dict['ether'].replace('-', ':')
return interfaces

@property
def default_interface(self):
"""
Returns the default interface device.
"""
for _ifn, interface in self.interfaces.items():
gateway = interface.get('default_gateway', '').strip()
if gateway and gateway != '::' and not gateway.startswith('127'):
return interface

class UnixParser(Parser):

Expand Down
41 changes: 41 additions & 0 deletions tests/ipconfig_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def test_windows10(self):
eq_(interfaces['Ethernet adapter Ethernet']['inet'], '192.168.1.2')
eq_(interfaces['Ethernet adapter Ethernet']['ether'], '11:11:11:11:a1:fa')

def test_default_interface_windows10(self):
ifcfg.distro = 'Windows'
ifcfg.Parser = ifcfg.get_parser_class()

parser = ifcfg.get_parser(ifconfig=ipconfig_out.WINDOWS_10_ETH)
default = parser.default_interface
ok_(default)
eq_(default['inet'], '192.168.1.2')
eq_(default['default_gateway'], '192.168.1.1')

def test_windows10_w_2_ethernets(self):
ifcfg.distro = "Windows"
Expand Down Expand Up @@ -75,6 +84,18 @@ def test_windows10_w_2_ethernets(self):
eq_(interfaces['Ethernet adapter Ethernet 2']['ether'], '08:00:27:0d:9a:0b')
eq_(interfaces['Ethernet adapter Ethernet 2']['inet6'], [])

def test_default_interface_windows10_w_2_ethernets(self):
ifcfg.distro = 'Windows'
ifcfg.Parser = ifcfg.get_parser_class()

parser = ifcfg.get_parser(
ifconfig=ipconfig_out.WINDOWS_10_WITH_2_ETHERNETS
)
default = parser.default_interface
ok_(default)
eq_(default['inet'], '10.0.2.15')
eq_(default['default_gateway'], '10.0.2.2')

def test_windows7vm(self):
ifcfg.distro = "Windows"
ifcfg.Parser = ifcfg.get_parser_class()
Expand All @@ -96,6 +117,26 @@ def test_windows7vm(self):
0
)

def test_default_interface_windows7vm(self):
ifcfg.distro = 'Windows'
ifcfg.Parser = ifcfg.get_parser_class()

parser = ifcfg.get_parser(ifconfig=ipconfig_out.WINDOWS_7_VM)
default = parser.default_interface
ok_(default)
eq_(default['inet'], '10.0.2.15')
eq_(default['default_gateway'], '10.0.2.2')

def test_default_interface_windows10_wlan(self):
ifcfg.distro = 'Windows'
ifcfg.Parser = ifcfg.get_parser_class()

parser = ifcfg.get_parser(ifconfig=ipconfig_out.WINDOWS_10_WLAN)
default = parser.default_interface
ok_(default)
eq_(default['inet'], '192.168.40.219')
eq_(default['default_gateway'], '192.168.40.1')

# Add a character that's known to fail in cp1252 encoding
# Patching `wait` is needed because on CI, these process (for Windows) don't really exist and cannot be executed
# `wait` sets the returncode which must be 0 for the parser to run
Expand Down