use of com.trilead.ssh2.Session in project intellij-community by JetBrains.
the class SshSessionConnection method open.
public void open(IStreamLogger streamLogger) throws AuthenticationException {
SshLogger.debug("opening session");
mySession = mySessionProvider.compute();
// wrapper created, inspection is inapplicable
//noinspection IOResourceOpenedButNotSafelyClosed
myInputStream = new MyInputStreamWrapper(myActivityMonitor, mySession.getStdout());
// wrapper created, inspection is inapplicable
//noinspection IOResourceOpenedButNotSafelyClosed
myOutputStream = new MyOutputStreamWrapper(myActivityMonitor, mySession.getStdin());
myErrorStreamGobbler = new StreamGobbler(mySession.getStderr());
myState = LifeStages.CREATED;
}
use of com.trilead.ssh2.Session in project ACS by ACS-Community.
the class Executor method remoteDownAllPortable.
/**
* Shuts down all ssh-sessions and -connections that may still be active.
*/
private static void remoteDownAllPortable() {
for (Session sess : sessions) {
try {
sess.close();
log.fine("closed " + sess);
} catch (Exception exc) {
log.fine("could not close " + sess);
}
}
for (Connection conn : connections) {
try {
conn.close();
log.fine("closed " + conn);
} catch (Exception exc) {
log.fine("could not close " + conn);
}
}
}
use of com.trilead.ssh2.Session in project pentaho-kettle by pentaho.
the class SSH method processRow.
@Override
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
meta = (SSHMeta) smi;
data = (SSHData) sdi;
Object[] row;
if (meta.isDynamicCommand()) {
row = getRow();
if (row == null) {
setOutputDone();
return false;
}
if (first) {
first = false;
data.outputRowMeta = getInputRowMeta().clone();
data.nrInputFields = data.outputRowMeta.size();
meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
data.nrOutputFields = data.outputRowMeta.size();
// Check if commands field is provided
if (meta.isDynamicCommand()) {
if (Utils.isEmpty(meta.getcommandfieldname())) {
throw new KettleException(BaseMessages.getString(PKG, "SSH.Error.CommandFieldMissing"));
}
// cache the position of the source filename field
data.indexOfCommand = data.outputRowMeta.indexOfValue(meta.getcommandfieldname());
if (data.indexOfCommand < 0) {
// The field is unreachable !
throw new KettleException(BaseMessages.getString(PKG, "SSH.Exception.CouldnotFindField", meta.getcommandfieldname()));
}
}
}
} else {
if (!data.wroteOneRow) {
// empty row
row = new Object[] {};
incrementLinesRead();
data.wroteOneRow = true;
if (first) {
first = false;
data.outputRowMeta = new RowMeta();
data.nrInputFields = 0;
meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
data.nrOutputFields = data.outputRowMeta.size();
data.commands = environmentSubstitute(meta.getCommand());
}
} else {
// signal end to receiver(s)
setOutputDone();
return false;
}
}
RowMetaInterface imeta = getInputRowMeta();
if (imeta == null) {
imeta = new RowMeta();
this.setInputRowMeta(imeta);
}
// Reserve room
Object[] rowData = new Object[data.nrOutputFields];
for (int i = 0; i < data.nrInputFields; i++) {
// no data is changed, clone is not needed here.
rowData[i] = row[i];
}
int index = data.nrInputFields;
Session session = null;
try {
if (meta.isDynamicCommand()) {
// get commands
data.commands = data.outputRowMeta.getString(row, data.indexOfCommand);
if (Utils.isEmpty(data.commands)) {
throw new KettleException(BaseMessages.getString(PKG, "SSH.Error.MessageEmpty"));
}
}
// Open a session
session = data.conn.openSession();
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "SSH.Log.SessionOpened"));
}
// execute commands
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "SSH.Log.RunningCommand", data.commands));
}
session.execCommand(data.commands);
// Read Stdout, Sterr and exitStatus
SessionResult sessionresult = new SessionResult(session);
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "SSH.Log.CommandRunnedCommand", data.commands, sessionresult.getStdOut(), sessionresult.getStdErr()));
}
// Add stdout to output
rowData[index++] = sessionresult.getStd();
if (!Utils.isEmpty(data.stdTypeField)) {
// Add stdtype to output
rowData[index++] = sessionresult.isStdTypeErr();
}
if (log.isRowLevel()) {
logRowlevel(BaseMessages.getString(PKG, "SSH.Log.OutputLine", data.outputRowMeta.getString(rowData)));
}
putRow(data.outputRowMeta, rowData);
if (checkFeedback(getLinesRead())) {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "SSH.LineNumber", "" + getLinesRead()));
}
}
} catch (Exception e) {
boolean sendToErrorRow = false;
String errorMessage = null;
if (getStepMeta().isDoingErrorHandling()) {
sendToErrorRow = true;
errorMessage = e.toString();
} else {
logError(BaseMessages.getString(PKG, "SSH.ErrorInStepRunning") + e.getMessage());
setErrors(1);
stopAll();
// signal end to receiver(s)
setOutputDone();
return false;
}
if (sendToErrorRow) {
// Simply add this row to the error row
putError(getInputRowMeta(), row, 1, errorMessage, null, "SSH001");
}
} finally {
if (session != null) {
session.close();
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "SSH.Log.SessionClosed"));
}
}
}
return true;
}
use of com.trilead.ssh2.Session in project warn-report by saaavsaaa.
the class LinuxExecUtil method execute.
public void execute(final String commandText) {
try {
System.out.println("exec : " + commandText);
Session sess = conn.openSession();
sess.execCommand(commandText);
System.out.println("Here is some information about the remote host:");
InputStream standardOut = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(standardOut));
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
System.out.println("ExitCode: " + sess.getExitStatus());
sess.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(2);
}
}
use of com.trilead.ssh2.Session in project Payara by payara.
the class SSHLauncher method runCommandAsIs.
private int runCommandAsIs(String command, OutputStream os, List<String> stdinLines) throws IOException, InterruptedException {
if (logger.isLoggable(Level.FINER)) {
logger.finer("Running command " + command + " on host: " + this.host);
}
openConnection();
final Session sess = connection.openSession();
int status = exec(sess, command, os, listInputStream(stdinLines));
// XXX: Should we close connection after each command or cache it
// and re-use it?
SSHUtil.unregister(connection);
connection = null;
return status;
}
Aggregations