use of net.schmizz.sshj.connection.channel.direct.Session.Command in project asterixdb by apache.
the class StartDataGeneratorAction method main.
public static void main(String[] args) throws Exception {
SSHClient sshClient = new SSHClient();
sshClient.loadKnownHosts();
sshClient.connect("asterix-1.ics.uci.edu");
sshClient.authPublickey("zheilbro", "/Users/zheilbron/.ssh/id_rsa");
Session session = sshClient.startSession();
Command lsCmd = session.exec("ls");
StringWriter sw = new StringWriter();
IOUtils.copy(lsCmd.getInputStream(), sw);
IOUtils.copy(lsCmd.getErrorStream(), sw);
System.out.println(sw.toString());
session.close();
sw.close();
sshClient.close();
}
use of net.schmizz.sshj.connection.channel.direct.Session.Command in project vcell by virtualcell.
the class CommandServiceSsh_sshj method command.
@Override
public CommandOutput command(String[] commandStrings, int[] allowableReturnCodes) throws ExecutableException {
if (allowableReturnCodes == null) {
throw new IllegalArgumentException("allowableReturnCodes must not be null");
}
long timeMS = System.currentTimeMillis();
SshConnection sshConnection = null;
try {
sshConnection = pool.borrowObject();
try (Session session = sshConnection.client.startSession()) {
String cmd = CommandOutput.concatCommandStrings(commandStrings);
Session.Command command = session.exec(cmd);
// wait up to a minute for return -- will return sooner if command completes
command.join(1, TimeUnit.MINUTES);
String standardOutput = IOUtils.readFully(command.getInputStream()).toString();
String standardError = IOUtils.readFully(command.getErrorStream()).toString();
command.close();
Integer exitStatus = command.getExitStatus();
long elapsedTimeMS = System.currentTimeMillis() - timeMS;
CommandOutput commandOutput = new CommandOutput(commandStrings, standardOutput, standardError, exitStatus, elapsedTimeMS);
if (VCMongoMessage.enabled) {
VCMongoMessage.sendCommandServiceCall(commandOutput);
}
if (commandOutput.getExitStatus() == null) {
lg.error("Command: " + commandOutput.getCommand());
lg.error("Command: exit error message: " + command.getExitErrorMessage());
lg.error("Command: exit signal: " + command.getExitSignal());
lg.error("Command: exit was core dumped: " + command.getExitWasCoreDumped());
} else {
lg.debug("Command: " + commandOutput.getCommand());
lg.trace("Command: stdout = " + commandOutput.getStandardOutput());
lg.trace("Command: stderr = " + commandOutput.getStandardError());
lg.debug("Command: exit = " + commandOutput.getExitStatus());
}
boolean bReturnCodeAllowable = false;
for (int returnCode : allowableReturnCodes) {
if (commandOutput.getExitStatus() != null && returnCode == commandOutput.getExitStatus().intValue()) {
bReturnCodeAllowable = true;
}
}
if (!bReturnCodeAllowable) {
lg.error("Command: " + commandOutput.getCommand());
lg.error("Command: stdout = " + commandOutput.getStandardOutput());
lg.error("Command: stderr = " + commandOutput.getStandardError());
lg.error("Command: exit = " + commandOutput.getExitStatus());
throw new ExecutableException("command exited with return code = " + commandOutput.getExitStatus());
}
return commandOutput;
} catch (Exception e) {
lg.error("command failed", e);
throw new ExecutableException(e.getMessage(), e);
}
} catch (ExecutableException e) {
throw e;
} catch (Exception e) {
lg.error("failed to borrow ssh connection from pool", e);
if (sshConnection != null) {
sshConnection.setFailed();
}
throw new ExecutableException(e.getMessage(), e);
} finally {
if (sshConnection != null) {
try {
pool.returnObject(sshConnection);
} catch (Exception e) {
lg.error("failed to return ssh connection to pool", e);
e.printStackTrace();
}
}
}
}
Aggregations