ia64/xen-unstable

changeset 4966:b1edecef79f8

bitkeeper revision 1.1438 (428a6cbbhokMA5cP0GT8z63oSTQwIA)

ip.py:
Make the get_current_ip* functions more robust -- default to look at the
interface which is currently used for the default route instead of eth0.
Fix get_current_ipaddr, get_current_ipmask and get_current_ipgw for
veth0.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Tue May 17 22:14:19 2005 +0000 (2005-05-17)
parents 9950cd01cebe
children b0242ba5318a
files tools/python/xen/util/ip.py
line diff
     1.1 --- a/tools/python/xen/util/ip.py	Tue May 17 18:17:46 2005 +0000
     1.2 +++ b/tools/python/xen/util/ip.py	Tue May 17 22:14:19 2005 +0000
     1.3 @@ -36,64 +36,70 @@ def _readline(fd):
     1.4  
     1.5  ##### Networking-related functions
     1.6  
     1.7 -"""Bridge for network backend.
     1.8 -When bridging is used, eth0 may not have an IP address,
     1.9 -as it may have been moved onto the bridge.
    1.10 -"""
    1.11 -NBE_BRIDGE = 'xen-br0'
    1.12 +def get_defaultroute():
    1.13 +    fd = os.popen('/sbin/ip route list 2>/dev/null')
    1.14 +    for line in fd.xreadlines():
    1.15 +        m = re.search('^default via ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) dev ([^ ]*)',
    1.16 +                      line)
    1.17 +        if m:
    1.18 +            return [m.group(1), m.group(2)]
    1.19 +    return [None, None]
    1.20  
    1.21 -def get_current_ipaddr(dev='eth0'):
    1.22 +def get_current_ipaddr(dev='defaultroute'):
    1.23      """Get the primary IP address for the given network interface.
    1.24  
    1.25 -    dev     network interface (default eth0)
    1.26 +    dev     network interface (default: default route device)
    1.27  
    1.28      returns interface address as a string
    1.29      """
    1.30 +    if dev == 'defaultroute':
    1.31 +        dev = get_defaultroute()[1]
    1.32 +    if not dev:
    1.33 +        return
    1.34      fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
    1.35 -    lines = _readlines(fd)
    1.36 -    for line in lines:
    1.37 +    for line in fd.xreadlines():
    1.38          m = re.search( '^\s+inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
    1.39                         line )
    1.40          if m:
    1.41              return m.group(1)
    1.42 -    if dev == 'eth0':
    1.43 -        return get_current_ipaddr(NBE_BRIDGE)
    1.44      return None
    1.45  
    1.46 -def get_current_ipmask(dev='eth0'):
    1.47 +def get_current_ipmask(dev='defaultroute'):
    1.48      """Get the primary IP netmask for a network interface.
    1.49  
    1.50 -    dev     network interface (default eth0)
    1.51 +    dev     network interface (default: default route device)
    1.52  
    1.53      returns interface netmask as a string
    1.54      """
    1.55 +    if dev == 'defaultroute':
    1.56 +        dev = get_defaultroute()[1]
    1.57 +    if not dev:
    1.58 +        return
    1.59      fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
    1.60 -    lines = _readlines(fd)
    1.61 -    for line in lines:
    1.62 +    for line in fd.xreadlines():
    1.63          m = re.search( '^.+Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
    1.64                         line )
    1.65          if m:
    1.66              return m.group(1)
    1.67 -    if dev == 'eth0':
    1.68 -        return get_current_ipmask(NBE_BRIDGE)
    1.69      return None
    1.70  
    1.71 -def get_current_ipgw(dev='eth0'):
    1.72 +def get_current_ipgw(dev='defaultroute'):
    1.73      """Get the IP gateway for a network interface.
    1.74  
    1.75 -    dev     network interface (default eth0)
    1.76 +    dev     network interface (default: default route device)
    1.77  
    1.78      returns gateway address as a string
    1.79      """
    1.80 +    if dev == 'defaultroute':
    1.81 +        return get_defaultroute()[0]
    1.82 +    if not dev:
    1.83 +        return
    1.84      fd = os.popen( '/sbin/route -n' )
    1.85 -    lines = _readlines(fd)
    1.86 -    for line in lines:
    1.87 +    for line in fd.xreadlines():
    1.88          m = re.search( '^\S+\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' +
    1.89                         '\s+\S+\s+\S*G.*' + dev + '.*', line )
    1.90          if m:
    1.91              return m.group(1)
    1.92 -    if dev == 'eth0':
    1.93 -        return get_current_ipgw(NBE_BRIDGE)
    1.94      return None
    1.95  
    1.96  def inet_aton(addr):