use of iaik.pkcs.pkcs11.Session in project cloudstack by apache.
the class TestClient method sshTest.
private static String sshTest(String host) {
if (host == null) {
s_logger.info("Did not receive a host back from test, ignoring ssh test");
return null;
}
// We will retry 5 times before quitting
int retry = 0;
while (true) {
try {
if (retry > 0) {
s_logger.info("Retry attempt : " + retry + " ...sleeping 120 seconds before next attempt");
Thread.sleep(120000);
}
s_logger.info("Attempting to SSH into linux host " + host + " with retry attempt: " + retry);
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("SSHed successfully into linux host " + host);
boolean isAuthenticated = conn.authenticateWithPassword("root", "password");
if (isAuthenticated == false) {
return "Authentication failed";
}
boolean success = false;
Session sess = conn.openSession();
s_logger.info("Executing : wget http://172.16.0.220/dump.bin");
sess.execCommand("wget http://172.16.0.220/dump.bin && ls -al dump.bin");
InputStream stdout = sess.getStdout();
InputStream stderr = sess.getStderr();
byte[] buffer = new byte[8192];
while (true) {
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
s_logger.info("Timeout while waiting for data from peer.");
return null;
}
if ((conditions & ChannelCondition.EOF) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
}
while (stdout.available() > 0) {
success = true;
int len = stdout.read(buffer);
if (// this check is somewhat paranoid
len > 0)
s_logger.info(new String(buffer, 0, len));
}
while (stderr.available() > 0) {
int len = stderr.read(buffer);
}
}
sess.close();
conn.close();
if (success) {
return null;
} else {
retry++;
if (retry == MAX_RETRY_LINUX) {
return "SSH Linux Network test fail";
}
}
} catch (Exception e) {
retry++;
if (retry == MAX_RETRY_LINUX) {
return "SSH Linux Network test fail with error " + e.getMessage();
}
}
}
}
use of iaik.pkcs.pkcs11.Session in project cloudstack by apache.
the class WgetTest method main.
public static void main(String[] args) {
// Parameters
List<String> argsList = Arrays.asList(args);
Iterator<String> iter = argsList.iterator();
while (iter.hasNext()) {
String arg = iter.next();
// host
if (arg.equals("-h")) {
host = iter.next();
}
if (arg.equals("-p")) {
password = iter.next();
}
}
int i = 0;
if (host == null || host.equals("")) {
s_logger.info("Did not receive a host back from test, ignoring ssh test");
System.exit(2);
}
if (password == null) {
s_logger.info("Did not receive a password back from test, ignoring ssh test");
System.exit(2);
}
int retry = 0;
try {
if (retry > 0) {
s_logger.info("Retry attempt : " + retry + " ...sleeping 120 seconds before next attempt");
Thread.sleep(120000);
}
s_logger.info("Attempting to SSH into linux host " + host + " with retry attempt: " + retry);
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User + ssHed successfully into linux host " + host);
boolean isAuthenticated = conn.authenticateWithPassword("root", password);
if (isAuthenticated == false) {
s_logger.info("Authentication failed for root with password" + password);
System.exit(2);
}
boolean success = false;
String linuxCommand = null;
if (i % 10 == 0)
linuxCommand = "rm -rf *; wget http://192.168.1.250/dump.bin && ls -al dump.bin";
else
linuxCommand = "wget http://192.168.1.250/dump.bin && ls -al dump.bin";
Session sess = conn.openSession();
sess.execCommand(linuxCommand);
InputStream stdout = sess.getStdout();
InputStream stderr = sess.getStderr();
byte[] buffer = new byte[8192];
while (true) {
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
s_logger.info("Timeout while waiting for data from peer.");
System.exit(2);
}
if ((conditions & ChannelCondition.EOF) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
}
while (stdout.available() > 0) {
success = true;
int len = stdout.read(buffer);
if (// this check is somewhat paranoid
len > 0)
s_logger.info(new String(buffer, 0, len));
}
while (stderr.available() > 0) {
/* int len = */
stderr.read(buffer);
}
}
sess.close();
conn.close();
if (!success) {
retry++;
if (retry == MAX_RETRY_LINUX) {
System.exit(2);
}
}
} catch (Exception e) {
retry++;
s_logger.error("SSH Linux Network test fail with error");
if (retry == MAX_RETRY_LINUX) {
s_logger.error("Ssh test failed");
System.exit(2);
}
}
}
use of iaik.pkcs.pkcs11.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 iaik.pkcs.pkcs11.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 iaik.pkcs.pkcs11.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