This service will be undergoing maintenance at 00:00 UTC, 2017-10-23 It is expected to last about 30 minutes
Bug 1005746 - [RFE] feature, Add functions to get default NIC, get IPADDR and so on
[RFE] feature, Add functions to get default NIC, get IPADDR and so on
Status: NEW
Product: Fedora
Classification: Fedora
Component: beakerlib (Show other bugs)
rawhide
All Linux
low Severity unspecified
: ---
: ---
Assigned To: Dalibor Pospíšil
Fedora Extras Quality Assurance
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2013-09-09 06:30 EDT by Yin.JianHong
Modified: 2017-09-13 03:44 EDT (History)
6 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed:
Type: Bug
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)
Universal functions to get default nic, ipaddr list, ipaddr; (794 bytes, text/plain)
2013-09-09 06:30 EDT, Yin.JianHong
no flags Details

  None (edit)
Description Yin.JianHong 2013-09-09 06:30:55 EDT
Created attachment 795572 [details]
Universal functions to get default  nic, ipaddr list,  ipaddr;

Description of problem:
RFE: feature, Add functions to get default NIC, get IPADDR and so on
In many of our test cases, need to get the default NIC or IPADDR:
    ifconfig | grep eth | awk ...
    ip addr show  $CUR_IFACE | grep ...
It's better to support a Universal function to get these info.

Version-Release number of selected component (if applicable):
all beakerlib version.

How reproducible:
y

Steps to Reproduce:
1.
2.
3.

Actual results:


Expected results:
An optional implementation:
# get ip addr
getDefaultNic() {
  ip route | awk '/default/{match($0,"dev ([^ ]+)",M); print M[1]}'
}

getIp() {
  local ret
  local nic=$1
  local ver=$2
  local sc=${3}
  local ipaddr=`ip addr show $nic`;
  [ -z "$sc" ] && {
      sc=global;
      echo "$ipaddr"|grep -q 'inet6.*global' || sc=link;
  }

  case $ver in
  6|6nfs)
      ret=`echo "$ipaddr" |
          awk '/inet6.*'$sc'/{match($0,"inet6 ([0-9a-f:]+)",M); print M[1]}'`
      [ -n "$ret" -a $ver = 6nfs ] && ret=$ret%$nic;;
  4|*)
      ret=`echo "$ipaddr" |
          awk '/inet .*global/{match($0,"inet ([0-9.]+)",M); print M[1]}'`;;
  esac

  echo "$ret"
  [ -z "$ret" ] && return 1
}

getDefaultIp() {
  local nic=`getDefaultNic`
  [ -z "$nic" ] && return 1

  getIp "$nic" "$@"
}


Additional info:
Comment 3 Petr Muller 2013-10-01 05:15:58 EDT
After some thinking, I believe it is worth inclusion in the core
Comment 4 Hangbin Liu 2013-10-07 21:48:24 EDT
My methods to get some network info, just for reference

get_cur_iface()
{
        ip route show | awk '/default/{print $5}'
}


get_iface_mac()
{
        ip link show dev $1 | awk '/link\/ether/{print $2}'
}

get_iface_ip4()
{
        ip addr show dev $1 | awk '/inet /{print $2}' | cut -d'/' -f1
}

get_ip6_laddr()
{
        ip addr show dev $1 | awk '/fe80/{print $2}' | cut -d'/' -f1
}

get_iface_ip6()
{
        # Do not select link local address
        ip addr show dev $1 | grep -v fe80 | grep inet6 -m 1 | awk '{print $2}' | cut -d'/' -f1
}

get_iface_driver()
{
        ethtool -i $1 | awk '/driver:/{print $2}'
}

get_cur_gw_route4()
{
        ip route | grep "default via.* dev.*" | sed -n 's/^default via \([0-9\.]*\) dev .*/\1/p'
}
Comment 5 Fedora Admin XMLRPC Client 2014-09-02 08:14:19 EDT
This package has changed ownership in the Fedora Package Database.  Reassigning to the new owner of this component.
Comment 6 Jiri Jaburek 2014-10-02 10:01:04 EDT
What exactly would be the use case(s) for these functions? Some variant of multihost testing?

The functions don't make much sense without specific use cases, because they may return invalid/unexpected results simply due to the complexity of the networking subsystem, for example:

(In reply to Hangbin Liu from comment #4)
> My methods to get some network info, just for reference
> 
> get_cur_iface()
> {
>         ip route show | awk '/default/{print $5}'
> }

This works only in the simplest case with the default route being in the 'main' table and without policy routing. When you have multiple tables with multiple default routes, such function stops making sense.

> 
> 
> get_iface_mac()
> {
>         ip link show dev $1 | awk '/link\/ether/{print $2}'
> }

Not all interfaces are Ethernet, perhaps get_eth_iface_mac() ?

> 
> get_iface_ip4()
> {
>         ip addr show dev $1 | awk '/inet /{print $2}' | cut -d'/' -f1
> }

This can return multiple addresses with varying scopes, I presume that's expected.

> 
> get_ip6_laddr()
> {
>         ip addr show dev $1 | awk '/fe80/{print $2}' | cut -d'/' -f1
> }

Link-local IPv6 addresses are fe80::/10, that is fe80:0:0:0:0:0:0:0 to febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff.

> 
> get_iface_ip6()
> {
>         # Do not select link local address
>         ip addr show dev $1 | grep -v fe80 | grep inet6 -m 1 | awk '{print
> $2}' | cut -d'/' -f1
> }

I would expect all IPv6 addresses, given the function name, like with IPv4, which didn't exclude IPv4 link-local addresses (169.254.0.0/16).

Instead of grep -v on specific prefixes, using "scope global" may be better for the use case - it doesn't guarantee that link-local addresses won't be present, just that the link scope will be excluded.

> 
> get_iface_driver()
> {
>         ethtool -i $1 | awk '/driver:/{print $2}'
> }

Again, works only on Ethernet, so perhaps get_eth_iface_driver() ?

> 
> get_cur_gw_route4()
> {
>         ip route | grep "default via.* dev.*" | sed -n 's/^default via
> \([0-9\.]*\) dev .*/\1/p'
> }

See get_cur_iface() limitations.


As I wrote in the beginning, I'm not against the functionality itself, given a specific set of (known) use cases, however the functions won't really be generic.

If you decide to include these, consider using "ip -o", which outputs one entry per line for easier / more robust parsing and "ip -4" / "ip -6" for IPv4 and IPv6 function variants. Separating scopes to "global" and "link" may be easier than grep-ing out link-local addresses, see man 8 ip-address.
Comment 8 Fedora End Of Life 2015-01-09 17:17:43 EST
This message is a notice that Fedora 19 is now at end of life. Fedora 
has stopped maintaining and issuing updates for Fedora 19. It is 
Fedora's policy to close all bug reports from releases that are no 
longer maintained. Approximately 4 (four) weeks from now this bug will
be closed as EOL if it remains open with a Fedora 'version' of '19'.

Package Maintainer: If you wish for this bug to remain open because you
plan to fix it in a currently maintained version, simply change the 'version' 
to a later Fedora version.

Thank you for reporting this issue and we are sorry that we were not 
able to fix it before Fedora 19 is end of life. If you would still like 
to see this bug fixed and are able to reproduce it against a later version 
of Fedora, you are encouraged  change the 'version' to a later Fedora 
version prior this bug is closed as described in the policy above.

Although we aim to fix as many bugs as possible during every release's 
lifetime, sometimes those efforts are overtaken by events. Often a 
more recent Fedora release includes newer upstream software that fixes 
bugs or makes them obsolete.
Comment 9 Dalibor Pospíšil 2016-05-26 09:33:45 EDT
As there are more opinions on approach to this I would suggest to implement it as a library to fit exactly your needs.

Note You need to log in before you can comment on or make changes to this bug.