Revision 2c30e9d7 lib/utils.py

b/lib/utils.py
32 32
import socket
33 33
import tempfile
34 34
import shutil
35
from errno import ENOENT, ENOTDIR, EISDIR, EEXIST
35
from errno import ENOENT, ENOTDIR, EISDIR, EEXIST, EADDRNOTAVAIL, ECONNREFUSED
36 36

  
37 37
from ganeti import logger
38 38
from ganeti import errors
......
786 786
      " output: %s" % (result.cmd, result.fail_reason, result.output))
787 787

  
788 788
  return _ParseIpOutput(result.output)
789

  
790

  
791
def TcpPing(source, target, port, timeout=10, live_port_needed=True):
792
  """Simple ping implementation using TCP connect(2).
793

  
794
  Try to do a TCP connect(2) from the specified source IP to the specified
795
  target IP and the specified target port. If live_port_needed is set to true,
796
  requires the remote end to accept the connection. The timeout is specified
797
  in seconds and defaults to 10 seconds
798

  
799
  """
800
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
801

  
802
  sucess = False
803

  
804
  try:
805
    sock.bind((source, 0))
806
  except socket.error, (errcode, errstring):
807
    if errcode == EADDRNOTAVAIL:
808
      success = False
809

  
810
  sock.settimeout(timeout)
811

  
812
  try:
813
    sock.connect((target, port))
814
    sock.close()
815
    success = True
816
  except socket.timeout:
817
    success = False
818
  except socket.error, (errcode, errstring):
819
    success = (not live_port_needed) and (errcode == ECONNREFUSED)
820

  
821
  return success

Also available in: Unified diff