use of org.neo4j.driver.v1.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 org.neo4j.driver.v1.Session in project zm-mailbox by Zimbra.
the class RemoteManager method executeBackground0.
private synchronized void executeBackground0(String command, RemoteBackgroundHandler handler) {
Session s = null;
try {
s = getSession();
if (ZimbraLog.rmgmt.isDebugEnabled())
ZimbraLog.rmgmt.debug("(bg) executing shim command '" + mShimCommand + "' on " + this);
s.execCommand(mShimCommand);
OutputStream os = s.getStdin();
String send = "HOST:" + mHost + " " + command;
if (ZimbraLog.rmgmt.isDebugEnabled())
ZimbraLog.rmgmt.debug("(bg) sending mgmt command '" + send + "' on " + this);
os.write(send.getBytes());
os.close();
InputStream stdout = new StreamGobbler(s.getStdout());
InputStream stderr = new StreamGobbler(s.getStderr());
handler.read(stdout, stderr);
} catch (OutOfMemoryError e) {
Zimbra.halt("out of memory", e);
} catch (Throwable t) {
handler.error(t);
} finally {
if (s != null) {
releaseSession(s);
}
}
}
use of org.neo4j.driver.v1.Session in project zm-mailbox by Zimbra.
the class RemoteManager method execute.
public synchronized RemoteResult execute(String command) throws ServiceException {
Session s = null;
try {
s = getSession();
if (ZimbraLog.rmgmt.isDebugEnabled())
ZimbraLog.rmgmt.debug("executing shim command '" + mShimCommand + "' on " + this);
s.execCommand(mShimCommand);
OutputStream os = s.getStdin();
String send = "HOST:" + mHost + " " + command;
if (ZimbraLog.rmgmt.isDebugEnabled()) {
ZimbraLog.rmgmt.debug("sending mgmt command '%s' on %s", send, this);
}
os.write(send.getBytes());
os.close();
RemoteResult result = new RemoteResult();
InputStream stdout = new StreamGobbler(s.getStdout());
InputStream stderr = new StreamGobbler(s.getStderr());
result.mStdout = ByteUtil.getContent(stdout, -1);
result.mStderr = ByteUtil.getContent(stderr, -1);
if (ZimbraLog.rmgmt.isTraceEnabled()) {
try {
ZimbraLog.rmgmt.trace("stdout content for cmd:\n%s", new String(result.mStdout, "UTF-8"));
ZimbraLog.rmgmt.trace("stderr content for cmd:\n%s", new String(result.mStderr, "UTF-8"));
} catch (Exception ex) {
ZimbraLog.rmgmt.trace("Problem logging stdout or stderr for cmd - probably not UTF-8");
}
}
try {
result.mExitStatus = s.getExitStatus();
} catch (NullPointerException npe) {
// wow this is strange - on hold command we hit NPE here. TODO file a bug against ganymed
}
if (result.mExitStatus != 0) {
throw new IOException("command failed: exit status=" + result.mExitStatus + ", stdout=" + new String(result.mStdout) + ", stderr=" + new String(result.mStderr));
}
result.mExitSignal = s.getExitSignal();
return result;
} catch (IOException ioe) {
throw ServiceException.FAILURE("exception executing command: " + command + " with " + this, ioe);
} finally {
if (s != null) {
releaseSession(s);
}
}
}
use of org.neo4j.driver.v1.Session in project CloudStack-archive by CloudStack-extras.
the class TestClientWithAPI method sshWinTest.
private static String sshWinTest(String host) {
if (host == null) {
s_logger.info("Did not receive a host back from test, ignoring win ssh test");
return null;
}
// We will retry 5 times before quitting
int retry = 1;
while (true) {
try {
if (retry > 0) {
s_logger.info("Retry attempt : " + retry + " ...sleeping 300 seconds before next attempt. Account is " + _account.get());
Thread.sleep(300000);
}
s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry + " for account " + _account.get());
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User " + _account.get() + " ssHed successfully into windows host " + host);
boolean success = false;
boolean isAuthenticated = conn.authenticateWithPassword("Administrator", "password");
if (isAuthenticated == false) {
return "Authentication failed";
} else {
s_logger.info("Authentication is successfull");
}
try {
SCPClient scp = new SCPClient(conn);
scp.put("wget.exe", "wget.exe", "C:\\Users\\Administrator", "0777");
s_logger.info("Successfully put wget.exe file");
} catch (Exception ex) {
s_logger.error("Unable to put wget.exe " + ex);
}
if (conn == null) {
s_logger.error("Connection is null");
}
Session sess = conn.openSession();
s_logger.info("User + " + _account.get() + " executing : wget http://" + downloadUrl);
String downloadCommand = "wget http://" + downloadUrl + " && dir dump.bin";
sess.execCommand(downloadCommand);
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_WIN) {
return "SSH Windows Network test fail for account " + _account.get();
}
}
} catch (Exception e) {
s_logger.error(e);
retry++;
if (retry == MAX_RETRY_WIN) {
return "SSH Windows Network test fail with error " + e.getMessage();
}
}
}
}
use of org.neo4j.driver.v1.Session in project CloudStack-archive by CloudStack-extras.
the class TestClientWithAPI method sshTest.
private static String sshTest(String host, String password, String snapshot_test) {
int i = 0;
if (host == null) {
s_logger.info("Did not receive a host back from test, ignoring ssh test");
return null;
}
if (password == null) {
s_logger.info("Did not receive a password back from test, ignoring ssh test");
return null;
}
// We will retry 5 times before quitting
String result = null;
int retry = 0;
while (true) {
try {
if (retry > 0) {
s_logger.info("Retry attempt : " + retry + " ...sleeping 120 seconds before next attempt. Account is " + _account.get());
Thread.sleep(120000);
}
s_logger.info("Attempting to SSH into linux host " + host + " with retry attempt: " + retry + ". Account is " + _account.get());
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User + " + _account.get() + " 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);
return "Authentication failed";
}
boolean success = false;
String linuxCommand = null;
if (i % 10 == 0)
linuxCommand = "rm -rf *; wget http://" + downloadUrl + " && ls -al dump.bin";
else
linuxCommand = "wget http://" + downloadUrl + " && ls -al dump.bin";
Session sess = conn.openSession();
s_logger.info("User " + _account.get() + " executing : " + linuxCommand);
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.");
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) {
retry++;
if (retry == MAX_RETRY_LINUX) {
result = "SSH Linux Network test fail";
}
}
if (snapshot_test.equals("no"))
return result;
else {
Long sleep = 300000L;
s_logger.info("Sleeping for " + sleep / 1000 / 60 + "minutes before executing next ssh test");
Thread.sleep(sleep);
}
} catch (Exception e) {
retry++;
s_logger.error("SSH Linux Network test fail with error");
if ((retry == MAX_RETRY_LINUX) && (snapshot_test.equals("no"))) {
return "SSH Linux Network test fail with error " + e.getMessage();
}
}
i++;
}
}
Aggregations