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 Jan 29, 2025
1 parent a5ea0d1 commit b8aacc1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions sssd_test_framework/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,31 @@ def grep(self, pattern: str, paths: str | list[str], args: list[str] | None = No

return command.rc == 0

def connected_interface(self) -> str:
"""
Retrieves the currently connected network interface on a Linux system by executing the
'ip route show default' command on a remote host.
This method uses the `exec` method of the `host.conn` object to run the 'ip route show default'
command on the remote host. It then parses the output to determine the network interface
used for the default route.
Returns:
str: The name of the connected network interface (e.g., 'eth0', 'wlan0') if found.
If the command fails, returns empty string.
Example:
>>> client.tools.connected_interface()
'eth0'
"""
result = self.host.conn.exec(["ip", "route", "show", "default"])
if result.rc == 0:
output = result.stdout.strip()
if output:
interface = output.split()[4] # The interface name is the 5th field
return interface
return ""

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 b8aacc1

Please sign in to comment.