Esempio n. 1
0
def CheckSsh(host, user, port):
    """Test SSH connection to host

  Args:
    host: A string
    user: A string
    port: An integer

  Returns:
    # 0 if success
    # error code otherwise
  """
    ssh_conditions = [
        'GoogleSupportCall\$ ',
        'Host key verification failed.',
        'password: '******'-2 -o StrictHostKeyChecking=yes -p %s' % port
    command = 'ssh %s %s@%s' % (ssh_options, user, host)

    ssh_test = pexpect.spawn(command)
    index = ssh_test.expect(ssh_conditions, timeout=30)

    if index == 0:
        # if connected to support call prompt, gracefully disconnect
        ssh_test.sendline('c')
        end = ssh_test.expect(['goodbye', pexpect.EOF, pexpect.TIMEOUT],
                              timeout=30)
        if end == 0:
            ssh_test.close(wait=1)
        else:
            ssh_test.kill(9)
    # handles SSH errors
    elif index == 1:
        ReportError(
            "SSH error: Support Call server host key verification failed.")
    elif index == 2:
        ReportError("SSH error: Public key authentication with Support \
                Call server failed.")
    elif index == 3:
        ReportError("SSH error: Unexpected EOF from SSH. Public key \
                authentication with Support Call server may have failed.")
    else:
        ReportError(
            "SSH error: timeout while attempting to execute SSH command.")

    return index
Esempio n. 2
0
def EstablishForwards(user, address, src_ports, dest_ports, conditions,
                      msg_out, success, gix):
    """Establish port forwards
  """
    start_time = 0
    ssh_options = '-2 -o StrictHostKeyChecking=yes -p 443'
    command = 'ssh %s' % ssh_options
    msg = 'DGIX %s Ports' % gix
    for i in range(0, len(dest_ports)):
        command = ('%s -R%s:localhost:%d' %
                   (command, dest_ports[i], src_ports[i]))
        msg = ('%s %d,%s' % (msg, src_ports[i], dest_ports[i]))
        os.write(
            msg_out,
            'Port %d forwarded to port %s\n' % (src_ports[i], dest_ports[i]))
    command = ('%s%s' % (command, (' %s@%s' % (user, address))))
    connections = pexpect.spawn(command)
    success = 0
    while success == 0:
        index = connections.expect(conditions, timeout=30)
        if index == 0:
            connections.sendline('yes')
            success = 0
        else:
            success = index
        if success >= 2:
            os.write(msg_out, 'Port forward failed\n')
            break  # break out of for loop
    if success == 1:
        connected = 1
        connections.sendline(msg)
        os.write(msg_out, 'All connections started successfully\n')
        os.write(msg_out, 'complete\n')
        start_time = time.time()
    else:
        connected = 0
        # clean up opened connections
        connections.close()
        os.write(msg_out, 'Start failed %d: ' % success)
        if success == 4:
            os.write(msg_out, 'connection timeout\n')
        else:
            os.write(msg_out, 'server error\n')
        os.write(msg_out, 'Closed all connections\n')
        os.write(msg_out, 'complete\n')
        Terminate()
    return (connected, connections, start_time)
Esempio n. 3
0
def CheckSsh(host, user, port):
  """Test SSH connection to host

  Args:
    host: A string
    user: A string
    port: An integer

  Returns:
    # 0 if success
    # error code otherwise
  """
  ssh_conditions = ['GoogleSupportCall\$ ',
                    'Host key verification failed.',
                    'password: '******'-2 -o StrictHostKeyChecking=yes -p %s' % port
  command = 'ssh %s %s@%s' % (ssh_options, user, host)

  ssh_test = pexpect.spawn(command)
  index = ssh_test.expect(ssh_conditions, timeout=30)

  if index == 0:
    # if connected to support call prompt, gracefully disconnect
    ssh_test.sendline('c')
    end = ssh_test.expect(['goodbye', pexpect.EOF, pexpect.TIMEOUT], timeout=30)
    if end == 0:
      ssh_test.close(wait=1)
    else:
      ssh_test.kill(9)
  # handles SSH errors
  elif index == 1:
    ReportError("SSH error: Support Call server host key verification failed.")
  elif index == 2:
    ReportError("SSH error: Public key authentication with Support \
                Call server failed.")
  elif index == 3:
    ReportError("SSH error: Unexpected EOF from SSH. Public key \
                authentication with Support Call server may have failed.")
  else:
    ReportError("SSH error: timeout while attempting to execute SSH command.")

  return index
Esempio n. 4
0
def EstablishForwards(user, address, src_ports, dest_ports,
    conditions, msg_out, success, gix):
  """Establish port forwards
  """
  start_time = 0
  ssh_options = '-2 -o StrictHostKeyChecking=yes -p 443'
  command = 'ssh %s' % ssh_options
  msg = 'DGIX %s Ports' % gix
  for i in range(0, len(dest_ports)):
    command = ('%s -R%s:localhost:%d' % (command, dest_ports[i], src_ports[i]))
    msg = ('%s %d,%s' % (msg, src_ports[i], dest_ports[i]))
    os.write(msg_out, 'Port %d forwarded to port %s\n' %
             (src_ports[i], dest_ports[i]))
  command = ('%s%s' % (command, (' %s@%s' % (user, address))))
  connections = pexpect.spawn(command)
  success = 0
  while success == 0:
    index = connections.expect(conditions, timeout=30)
    if index == 0:
      connections.sendline('yes')
      success = 0
    else:
      success = index
    if success >= 2:
      os.write(msg_out, 'Port forward failed\n')
      break  # break out of for loop
  if success == 1:
    connected = 1
    connections.sendline(msg)
    os.write(msg_out, 'All connections started successfully\n')
    os.write(msg_out, 'complete\n')
    start_time = time.time()
  else:
    connected = 0
    # clean up opened connections
    connections.close()
    os.write(msg_out, 'Start failed %d: ' % success)
    if success == 4:
      os.write(msg_out, 'connection timeout\n')
    else:
      os.write(msg_out, 'server error\n')
    os.write(msg_out, 'Closed all connections\n')
    os.write(msg_out, 'complete\n')
    Terminate()
  return (connected, connections, start_time)
Esempio n. 5
0
def RequestPorts(user, address, conditions, num_ports):
    """Request ports from Google support

  Args:
    user: A String of appliance ID
    address: A String of support address
    conditions: [ pexpect expect objects, ... ]
    num_ports:  An integer of the number of ports to request

  Returns:
    # ports are port numbers from Google support
    (success, [ports, ...])
  """
    success = 0
    forward_list = []
    ssh_options = '-2 -o StrictHostKeyChecking=yes -p 443'
    command = 'ssh %s %s@%s' % (ssh_options, user, address)
    ask_ports = pexpect.spawn(command)
    while success == 0:
        index = ask_ports.expect(conditions, timeout=30)
        if index == 0:
            ask_ports.sendline('yes')
            success = 0
        elif index >= 1:
            success = index
    if success == 1:
        ask_ports.sendline('%d' % num_ports)
        index = ask_ports.expect(['Avaliable ports', pexpect.TIMEOUT],
                                 timeout=30)
        if index == 0:
            ask_ports.readline()
            for i in range(0, num_ports):
                forward_list.append(ask_ports.readline()[:-2])
        else:
            # cannot get ports
            success = 4
        ask_ports.close()
    return (success, forward_list)
Esempio n. 6
0
def RequestPorts(user, address, conditions, num_ports):
  """Request ports from Google support

  Args:
    user: A String of appliance ID
    address: A String of support address
    conditions: [ pexpect expect objects, ... ]
    num_ports:  An integer of the number of ports to request

  Returns:
    # ports are port numbers from Google support
    (success, [ports, ...])
  """
  success = 0
  forward_list = []
  ssh_options = '-2 -o StrictHostKeyChecking=yes -p 443'
  command = 'ssh %s %s@%s' % (ssh_options, user, address)
  ask_ports = pexpect.spawn(command)
  while success == 0:
    index = ask_ports.expect(conditions, timeout=30)
    if index == 0:
      ask_ports.sendline('yes')
      success = 0
    elif index >= 1:
      success = index
  if success == 1:
    ask_ports.sendline('%d' % num_ports)
    index = ask_ports.expect(['Avaliable ports', pexpect.TIMEOUT], timeout=30)
    if index == 0:
      ask_ports.readline()
      for i in range(0, num_ports):
        forward_list.append(ask_ports.readline()[:-2])
    else:
      # cannot get ports
      success = 4
    ask_ports.close()
  return (success, forward_list)