Skip to content

Commit

Permalink
Create method to find currently connected network interface
Browse files Browse the repository at this point in the history
Create method to find currently connected network interface
  • Loading branch information
aborah-sudo committed Feb 11, 2025
1 parent 875b35b commit 53b40aa
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions sssd_test_framework/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,46 @@ def grep(self, pattern: str, paths: str | list[str], args: list[str] | None = No

return command.rc == 0

def connected_interface(self) -> str:
"""
Test to retrieve and validate the default network interface from the system's routing table.
This test executes the `ip route show default` command on the host and parses the output
using `jc.parse` to extract routing information. It ensures the output is in the expected format,
validates the presence of a single default interface, and extracts the interface name.
Raises:
ValueError: If the output from `jc.parse` is not in the expected format (a list of dictionaries),
if there is no default interface or multiple default interfaces are found,
or if the default interface is not set.
Returns:
str: The name of the default network interface.
Example:
>>> client.tools.connected_interface()
eth0
"""
result = self.host.conn.exec(["ip", "route", "show", "default"])
output = jc.parse("ip_route", result.stdout)

# Ensure output is a list
if isinstance(output, dict):
output = [output] # Convert single dictionary to a list of one dictionary
elif not isinstance(output, list):
raise ValueError("Unexpected output format from jc.parse")

# Validate the output
if not output or len(output) != 1:
raise ValueError("Unexpected number of default interfaces")

# Extract the interface
interface = output[0].get("dev")
if not interface:
raise ValueError("Default interface is not set")

return interface

def tcpdump(self, pcap_path: str, args: list[Any] | None = None) -> SSHKillableProcess:
"""
Run tcpdump. The packets are captured in ``pcap_path``.
Expand Down

0 comments on commit 53b40aa

Please sign in to comment.