Skip to content

Commit

Permalink
Merge pull request avocado-framework#3818 from smitterl/select_gw_iface
Browse files Browse the repository at this point in the history
utils_net: get default gateway for specific interface
  • Loading branch information
chloerh authored Jan 5, 2024
2 parents 7ab6f73 + f0d842b commit 26cc872
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions virttest/utils_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -3893,14 +3893,15 @@ def get_host_iface():


def get_default_gateway(iface_name=False, session=None, ip_ver='ipv4',
force_dhcp=False):
force_dhcp=False, target_iface=None):
"""
Get the Default Gateway or Interface of host or guest.
:param iface_name: Whether default interface (True), or default gateway
(False) is returned, defaults to False
:param session: shell/console session if any, defaults to None
:param ip_ver: ip version, defaults to 'ipv4'
:param target_iface: if given, get default gateway only for this device
:return: default gateway of target iface
"""
if ip_ver == 'ipv4':
Expand All @@ -3911,10 +3912,14 @@ def get_default_gateway(iface_name=False, session=None, ip_ver='ipv4',
raise ValueError(f'Unrecognized IP version {ip_ver}')
if force_dhcp:
ip_cmd = ip_cmd + '|grep dhcp'
if target_iface:
regex = "default.*%s" % target_iface
else:
regex = "default"
if iface_name:
cmd = "%s | awk '/default/ { print $5 }'" % ip_cmd
cmd = "%s | awk '/%s/ { print $5 }'" % (ip_cmd, regex)
else:
cmd = "%s | awk '/default/ { print $3 }'" % ip_cmd
cmd = "%s | awk '/%s/ { print $3 }'" % (ip_cmd, regex)
try:
_, output = utils_misc.cmd_status_output(cmd, shell=True, session=session)
if session:
Expand Down

0 comments on commit 26cc872

Please sign in to comment.