Revision b15d625f lib/utils.py

b/lib/utils.py
909 909
  return ' '.join([ShellQuote(i) for i in args])
910 910

  
911 911

  
912

  
913
def TcpPing(source, target, port, timeout=10, live_port_needed=False):
912
def TcpPing(target, port, timeout=10, live_port_needed=False, source=None):
914 913
  """Simple ping implementation using TCP connect(2).
915 914

  
916
  Try to do a TCP connect(2) from the specified source IP to the specified
917
  target IP and the specified target port. If live_port_needed is set to true,
918
  requires the remote end to accept the connection. The timeout is specified
919
  in seconds and defaults to 10 seconds
915
  Try to do a TCP connect(2) from an optional source IP to the
916
  specified target IP and the specified target port. If the optional
917
  parameter live_port_needed is set to true, requires the remote end
918
  to accept the connection. The timeout is specified in seconds and
919
  defaults to 10 seconds. If the source optional argument is not
920
  passed, the source address selection is left to the kernel,
921
  otherwise we try to connect using the passed address (failures to
922
  bind other than EADDRNOTAVAIL will be ignored).
920 923

  
921 924
  """
922 925
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
923 926

  
924 927
  sucess = False
925 928

  
926
  try:
927
    sock.bind((source, 0))
928
  except socket.error, (errcode, errstring):
929
    if errcode == errno.EADDRNOTAVAIL:
930
      success = False
929
  if source is not None:
930
    try:
931
      sock.bind((source, 0))
932
    except socket.error, (errcode, errstring):
933
      if errcode == errno.EADDRNOTAVAIL:
934
        success = False
931 935

  
932 936
  sock.settimeout(timeout)
933 937

  

Also available in: Unified diff