Revision 88d14415 lib/utils.py

b/lib/utils.py
749 749

  
750 750
  """
751 751
  return ' '.join([ShellQuote(i) for i in args])
752

  
753

  
754
def _ParseIpOutput(output):
755
  """Parsing code for GetLocalIPAddresses().
756

  
757
  This function is split out, so we can unit test it.
758

  
759
  """
760
  re_ip = re.compile('^(\d+\.\d+\.\d+\.\d+)(?:/\d+)$')
761

  
762
  ips = []
763
  for line in output.splitlines(False):
764
    fields = line.split()
765
    if len(line) < 4:
766
      continue
767
    m = re_ip.match(fields[3])
768
    if m:
769
      ips.append(m.group(1))
770

  
771
  return ips
772

  
773

  
774
def GetLocalIPAddresses():
775
  """Gets a list of all local IP addresses.
776

  
777
  Should this break one day, a small Python module written in C could
778
  use the API call getifaddrs().
779

  
780
  """
781
  result = RunCmd(["ip", "-family", "inet", "-oneline", "addr", "show"])
782
  if result.failed:
783
    raise errors.OpExecError("Command '%s' failed, error: %s,"
784
      " output: %s" % (result.cmd, result.fail_reason, result.output))
785

  
786
  return _ParseIpOutput(result.output)

Also available in: Unified diff