Package org.platformlayer.ops.ssh

Examples of org.platformlayer.ops.ssh.SshException


      ChannelSession clientChannel = BugFixChannelExec.createExecChannel(sshClientSession, command,
          useAgentForwarding);
      return clientChannel;
    } catch (Exception e) {
      ExceptionUtils.handleInterrupted(e);
      throw new SshException("Error creating channel", e);
    }
  }
View Full Code Here


      System.out.println("New SSH connection to " + connectionInfo.getHost());

      ConnectFuture connect = client.connect(connectionInfo.getSocketAddress());
      if (!connect.await(connectTimeout.getTotalMilliseconds())) {
        connect.cancel();
        throw new SshException("Timeout while waiting for SSH connection to " + connectionInfo.getHost());
      }

      this.sshClientSession = connect.getSession();

      if (this.sshClientSession == null) {
        throw new IllegalStateException();
      }

      this.state = SshConnectionState.Connected;
    } catch (Exception e) {
      ExceptionUtils.handleInterrupted(e);
      throw new SshException("Error connecting to SSH server @" + connectionInfo.getSocketAddress(), e);
    }
  }
View Full Code Here

    if ((sshSessionStateCode & ClientSession.AUTHED) == 0) {
      try {
        sshClientSession.authPublicKey(user, key);
      } catch (IOException e) {
        throw new SshException("I/O error while authenticating " + this, e);
      } catch (IllegalStateException e) {
        throw new SshException("Error authenticating " + this, e);
      }

      sshSessionStateCode = sshClientSession.waitFor(ClientSession.CLOSED | ClientSession.AUTHED
          | ClientSession.WAIT_AUTH, authenticationTimeout.getTotalMilliseconds());
    }

    if ((sshSessionStateCode & ClientSession.WAIT_AUTH) != 0) {
      throw new SshException("SSH authentication failed (trying " + connectionInfo + ")");
    }

    if ((sshSessionStateCode & ClientSession.CLOSED) != 0) {
      this.sshClientSession = null;
      state = SshConnectionState.Closed;
      throw new SshException("SSH connection closed while trying to login");
    }

    // if (serverKeyVerifier != null) {
    // byte[] serverKey = getServerKey(sshClientSession);
    // SocketAddress remoteAddress = new InetSocketAddress(connectionInfo.getHost(), connectionInfo.getPort());
    // if (!serverKeyVerifier.verifyServerKey(remoteAddress, serverKey)) {
    // sshClientSession.close(true);
    // throw new SshException("SSH key verification failed");
    // }
    // }

    if ((sshSessionStateCode & ClientSession.AUTHED) != 0) {
      state = SshConnectionState.Authenticated;
      return true;
    }

    if (sshSessionStateCode == ClientSession.TIMEOUT) {
      throw new SshException("Timeout while waiting for session");
    }
    throw new SshException("Unexpected state; not authenticated; not closed");
  }
View Full Code Here

        if (isAuthenticated == false) {
          // This happens when we upload the wrong public_key...
          // double check if you hit this!
          // This also happens if we're running a command that isn't
          // valid
          throw new SshException("Authentication failed.  Tried to connect to " + getUser() + "@"
              + sshConnection.getConnectionInfo().getHost());
        } else {
          log.debug("SSH authentication succeeded");
        }
      }
View Full Code Here

      ProcessExecution processExecution = new ProcessExecution(command, exitCode, stdoutBinary.toByteArray(),
          stderrBinary.toByteArray());
      return processExecution;
    } catch (RuntimeSshException e) {
      throw new SshException("Unexpected SSH error", e);
    }
  }
View Full Code Here

      sshChannel.setErr(stderr);
      try {
        sshChannel.open().await(DEFAULT_SSH_CONNECT_TIMEOUT.getTotalMilliseconds());
      } catch (Exception e) {
        ExceptionUtils.handleInterrupted(e);
        throw new SshException("Ssh error opening channel", e);
      }

      if (listener != null) {
        throw new UnsupportedOperationException();
        // listener.startedProcess(sshChannel.getStdin());
      }

      if (timeout == null) {
        timeout = TimeSpan.ZERO;
      }

      // Wait for everything to finish
      int flags = sshChannel.waitFor(ClientChannel.EOF | ClientChannel.CLOSED, timeout.getTotalMilliseconds());
      if ((flags & ClientChannel.TIMEOUT) != 0) {
        closeAndRemoveFromPool();
        throw new SshException("Timeout while waiting for SSH task to complete.  Timeout was " + timeout);
      }

      flags = sshChannel.waitFor(ClientChannel.EXIT_STATUS, 30000);
      if ((flags & ClientChannel.TIMEOUT) != 0) {
        closeAndRemoveFromPool();
        throw new SshException("Timeout while waiting for exit code.  Timeout was " + timeout);
      }

      Integer exitCode = getExitStatus(sshChannel);

      if (exitCode == null) {
        closeAndRemoveFromPool();
        throw new SshException("No exit code returned");
      }

      return exitCode;
    } finally {
      if (sshChannel != null) {
View Full Code Here

    try {
      TimeSpan timeout = TimeSpan.FIVE_MINUTES;
      scp.put(fileData, dataLength, filename, remoteDir, mode, timeout, sudo);
    } catch (IOException ioException) {
      throw new SshException("Cannot doing scp on file", ioException);
    } catch (RuntimeSshException e) {
      throw new SshException("Error doing scp on file", e);
    }

  }
View Full Code Here

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      TimeSpan timeout = TimeSpan.FIVE_MINUTES;
      scp.get(remoteFile, baos, timeout, sudo);
      return baos.toByteArray();
    } catch (IOException ioException) {
      throw new SshException("Cannot read file", ioException);
    } catch (SshException sshException) {
      String message = sshException.getMessage();
      if (message != null && message.endsWith(": No such file or directory")) {
        return null;
      }
      throw sshException;
    } catch (RuntimeSshException e) {
      throw new SshException("Error reading file", e);
    }
  }
View Full Code Here

      try {
        log.debug("SCP Opening connection");
        channel.open().await(connectTimeout.getTotalMilliseconds());
      } catch (Exception e) {
        ExceptionUtils.handleInterrupted(e);
        throw new SshException("SSH error opening channel", e);
      }
    }
View Full Code Here

          // Cmmmm <length> <filename>
          // a single file copy, mmmmm is mode. Example: C0644 299 group

          String[] tokens = nextLine.data.split(" ");
          if (tokens.length != 3) {
            throw new SshException("Invalid SCP line: " + nextLine);
          }
          fileInfo = new FileInfo(tokens[0].substring(1), Long.parseLong(tokens[1]), tokens[2]);
          break;
        }
        throw new SshException("Unexpected SCP status: " + nextLine);
      }

      toStdin.write(0x0);
      toStdin.flush();

      try {
        long remain = fileInfo.length;

        while (remain > 0) {
          int readSize = (int) Math.min(buffer.length, remain);

          int actuallyRead = fromStdout.read(buffer, 0, readSize);

          if (actuallyRead < 0) {
            throw new EOFException();
          }

          destOutputStream.write(buffer, 0, actuallyRead);

          remain -= actuallyRead;
        }
      } finally {
        IoUtils.safeClose(destOutputStream);
      }

      parseFinalLine(readResponseLine());

      toStdin.write(0x0);
      toStdin.flush();

      // Wait for everything to finish
      int flags = channel.waitFor(ClientChannel.EOF, timeout.getTotalMilliseconds());
      if ((flags & ClientChannel.TIMEOUT) != 0) {
        throw new SshException("Timeout while waiting for SSH task to complete");
      }
    }
View Full Code Here

TOP

Related Classes of org.platformlayer.ops.ssh.SshException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.