use of iaik.pkcs.pkcs11.Session in project cloudstack by apache.
the class SshTest 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();
if (arg.equals("-h")) {
host = iter.next();
}
if (arg.equals("-p")) {
password = iter.next();
}
if (arg.equals("-u")) {
url = iter.next();
}
}
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);
}
try {
s_logger.info("Attempting to SSH into host " + host);
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User + ssHed successfully into host " + host);
boolean isAuthenticated = conn.authenticateWithPassword("root", password);
if (isAuthenticated == false) {
s_logger.info("Authentication failed for root with password" + password);
System.exit(2);
}
String linuxCommand = "wget " + url;
Session sess = conn.openSession();
sess.execCommand(linuxCommand);
sess.close();
conn.close();
} catch (Exception e) {
s_logger.error("SSH test fail with error", e);
System.exit(2);
}
}
use of iaik.pkcs.pkcs11.Session in project cloudstack by apache.
the class SSHCmdHelper method sshExecuteCmdOneShotWithExitCode.
public static int sshExecuteCmdOneShotWithExitCode(com.trilead.ssh2.Connection sshConnection, String cmd) throws SshException {
s_logger.debug("Executing cmd: " + cmd);
Session sshSession = null;
try {
sshSession = sshConnection.openSession();
// There is a bug in Trilead library, wait a second before
// starting a shell and executing commands, from http://spci.st.ewi.tudelft.nl/chiron/xref/nl/tudelft/swerl/util/SSHConnection.html
Thread.sleep(1000);
if (sshSession == null) {
throw new SshException("Cannot open ssh session");
}
sshSession.execCommand(cmd);
InputStream stdout = sshSession.getStdout();
InputStream stderr = sshSession.getStderr();
byte[] buffer = new byte[8192];
StringBuffer sbResult = new StringBuffer();
int currentReadBytes = 0;
while (true) {
if (stdout == null || stderr == null) {
throw new SshException("stdout or stderr of ssh session is null");
}
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sshSession.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, 120000);
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
String msg = "Timed out in waiting SSH execution result";
s_logger.error(msg);
throw new Exception(msg);
}
if ((conditions & ChannelCondition.EXIT_STATUS) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
if ((conditions & ChannelCondition.EOF) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
}
while (stdout.available() > 0) {
currentReadBytes = stdout.read(buffer);
sbResult.append(new String(buffer, 0, currentReadBytes));
}
while (stderr.available() > 0) {
currentReadBytes = stderr.read(buffer);
sbResult.append(new String(buffer, 0, currentReadBytes));
}
}
String result = sbResult.toString();
if (result != null && !result.isEmpty())
s_logger.debug(cmd + " output:" + result);
// exit status delivery might get delayed
for (int i = 0; i < 10; i++) {
Integer status = sshSession.getExitStatus();
if (status != null) {
return status;
}
Thread.sleep(100);
}
return -1;
} catch (Exception e) {
s_logger.debug("Ssh executed failed", e);
throw new SshException("Ssh executed failed " + e.getMessage());
} finally {
if (sshSession != null)
sshSession.close();
}
}
use of iaik.pkcs.pkcs11.Session in project jstorm by alibaba.
the class MyScpClient method get.
/**
* Download a set of files from the remote server to a local directory.
*
* @param remoteFiles Paths and names of the remote files.
* @param localTargetDirectory Local directory to put the downloaded files.
* @throws IOException
*/
public void get(String[] remoteFiles, String localTargetDirectory) throws IOException {
Session sess = null;
if ((remoteFiles == null) || (localTargetDirectory == null))
throw new IllegalArgumentException("Null argument.");
if (remoteFiles.length == 0)
return;
// String cmd = "scp -f -r";
String cmd = "scp -f ";
for (int i = 0; i < remoteFiles.length; i++) {
if (remoteFiles[i] == null)
throw new IllegalArgumentException("Cannot accept null filename.");
cmd += (" " + remoteFiles[i]);
}
try {
sess = conn.openSession();
sess.execCommand(cmd);
receiveFiles(sess, remoteFiles, localTargetDirectory);
sess.close();
} catch (IOException e) {
if (sess != null)
sess.close();
throw (IOException) new IOException("Error during SCP transfer.").initCause(e);
}
}
use of iaik.pkcs.pkcs11.Session in project jstorm by alibaba.
the class MyScpClient method put.
/**
* Create a remote file and copy the contents of the passed byte array into it.
* The method use the specified mode when creating the file on the remote side.
*
* @param data the data to be copied into the remote file.
* @param remoteFileName The name of the file which will be created in the remote target directory.
* @param remoteTargetDirectory Remote target directory.
* @param mode a four digit string (e.g., 0644, see "man chmod", "man open")
* @throws IOException
*/
public void put(byte[] data, String remoteFileName, String remoteTargetDirectory, String mode) throws IOException {
Session sess = null;
if ((remoteFileName == null) || (remoteTargetDirectory == null) || (mode == null))
throw new IllegalArgumentException("Null argument.");
if (mode.length() != 4)
throw new IllegalArgumentException("Invalid mode.");
for (int i = 0; i < mode.length(); i++) if (Character.isDigit(mode.charAt(i)) == false)
throw new IllegalArgumentException("Invalid mode.");
String cmd = "scp -t -d " + remoteTargetDirectory;
try {
sess = conn.openSession();
sess.execCommand(cmd);
sendBytes(sess, data, remoteFileName, mode);
sess.close();
} catch (IOException e) {
if (sess != null)
sess.close();
throw (IOException) new IOException("Error during SCP transfer.").initCause(e);
}
}
use of iaik.pkcs.pkcs11.Session in project jstorm by alibaba.
the class MyScpClient method put.
/**
* Copy a set of local files to a remote directory, uses the specified mode
* when creating the files on the remote side.
*
* @param localFiles Paths and names of the local files.
* @param remoteTargetDirectory Remote target directory.
* @param mode a four digit string (e.g., 0644, see "man chmod", "man open")
* @throws IOException
*/
public void put(String[] localFiles, String remoteTargetDirectory, String mode) throws IOException {
Session sess = null;
if ((localFiles == null) || (remoteTargetDirectory == null) || (mode == null))
throw new IllegalArgumentException("Null argument.");
if (mode.length() != 4)
throw new IllegalArgumentException("Invalid mode.");
for (int i = 0; i < mode.length(); i++) if (Character.isDigit(mode.charAt(i)) == false)
throw new IllegalArgumentException("Invalid mode.");
if (localFiles.length == 0)
return;
String cmd = "scp -t -d " + remoteTargetDirectory;
for (int i = 0; i < localFiles.length; i++) {
if (localFiles[i] == null)
throw new IllegalArgumentException("Cannot accept null filename.");
}
try {
sess = conn.openSession();
sess.execCommand(cmd);
sendFiles(sess, localFiles, mode);
sess.close();
} catch (IOException e) {
if (sess != null)
sess.close();
throw (IOException) new IOException("Error during SCP transfer.").initCause(e);
}
}
Aggregations