use of com.jcraft.jsch.ChannelExec in project cdap by caskdata.
the class DefaultSSHSession method execute.
@Override
public SSHProcess execute(List<String> commands) throws IOException {
try {
Channel channel = session.openChannel("exec");
try {
ChannelExec channelExec = (ChannelExec) channel;
// Should get the stream before connecting.
// Otherwise JSch will write the output to some default stream, causing data missing from
// the InputStream that acquired later.
SSHProcess process = new DefaultSSHProcess(channelExec, channelExec.getOutputStream(), channelExec.getInputStream(), channelExec.getErrStream());
channelExec.setCommand(String.join(";", commands));
channelExec.connect();
return process;
} catch (Exception e) {
channel.disconnect();
throw e;
}
} catch (JSchException e) {
throw new IOException(e);
}
}
use of com.jcraft.jsch.ChannelExec in project CommandHelper by EngineHub.
the class SSHWrapper method getRemoteMD5.
private static String getRemoteMD5(String remoteFile, Session session) throws JSchException, IOException {
ChannelExec channel = null;
final StringBuilder sb = new StringBuilder();
try {
channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("openssl md5 " + remoteFile);
channel.setInputStream(null);
channel.setOutputStream(null);
channel.setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
sb.append(new String(tmp, 0, i));
}
if (channel.isClosed()) {
if (in.available() > 0) {
continue;
}
if (channel.getExitStatus() != 0) {
// Something went wrong, fail the comparison
return "invalidMD5sum";
}
break;
}
try {
Thread.sleep(1);
} catch (Exception ee) {
}
}
} finally {
if (channel != null) {
channel.disconnect();
}
}
if ("".equals(sb.toString())) {
// or if the file simply doesn't exist.
return "invalidMD5sum";
}
String opensslReturn = sb.toString();
// This will be something like MD5(/path/to/file)= 798ffb41da648e405ca160fb547e3a09
// and we just need 798ffb41da648e405ca160fb547e3a09
opensslReturn = opensslReturn.replaceAll("MD5\\(.*\\)= ", "");
return opensslReturn.replaceAll("\n|\r", "");
}
use of com.jcraft.jsch.ChannelExec in project CommandHelper by EngineHub.
the class SSHWrapper method SCPFrom.
private static void SCPFrom(String remote, File local, Session session) throws IOException, JSchException {
// exec 'scp -f rfile' remotely
String command = "scp -f " + remote;
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
byte[] buf = new byte[1024];
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
while (true) {
int c = checkAckFrom(in);
if (c != 'C') {
break;
}
// read '0644 '
in.read(buf, 0, 5);
long filesize = 0L;
while (true) {
if (in.read(buf, 0, 1) < 0) {
// error
break;
}
if (buf[0] == ' ') {
break;
}
filesize = filesize * 10L + (long) (buf[0] - '0');
}
String file = null;
for (int i = 0; ; i++) {
in.read(buf, i, 1);
if (buf[i] == (byte) 0x0a) {
file = new String(buf, 0, i);
break;
}
}
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
String prefix = null;
if (local.isDirectory()) {
prefix = local.getPath() + File.separator;
}
// read a content of lfile
FileOutputStream fos = new FileOutputStream(prefix == null ? local.getPath() : prefix + file);
int foo;
while (true) {
if (buf.length < filesize) {
foo = buf.length;
} else {
foo = (int) filesize;
}
foo = in.read(buf, 0, foo);
if (foo < 0) {
// error
break;
}
fos.write(buf, 0, foo);
filesize -= foo;
if (filesize == 0L) {
break;
}
}
fos.close();
fos = null;
checkAckFrom(in);
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
}
channel.disconnect();
}
use of com.jcraft.jsch.ChannelExec in project compss by bsc-wdc.
the class AbstractSSHConnector method tryToExecuteCommand.
private boolean tryToExecuteCommand(String workerIP, String user, boolean setPassword, String passwordOrKeyPair, String command) throws ConnectorException {
Session session = null;
ChannelExec exec = null;
InputStream inputStream = null;
try {
// Create session
session = getSession(workerIP, user, setPassword, passwordOrKeyPair);
// Configure session
exec = (ChannelExec) session.openChannel("exec");
exec.setCommand(command);
// Waits the command to be executed
inputStream = exec.getErrStream();
InputStream stdOut = exec.getInputStream();
// Execute command
exec.connect(SERVER_TIMEOUT);
if (LOGGER.isDebugEnabled()) {
String output = readInputStream(stdOut);
LOGGER.debug("Command output: " + output);
}
int exitStatus = -1;
while (exitStatus < 0) {
exitStatus = exec.getExitStatus();
if (exitStatus == 0) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Command successfully executed: " + command);
}
return true;
} else if (exitStatus > 0) {
String output = readInputStream(inputStream);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(ERROR_COMMAND_EXEC + command + " in " + workerIP + ".\nReturned std error: " + output);
}
throw new ConnectorException(ERROR_COMMAND_EXEC + command + " in " + workerIP + " (exit status:" + exitStatus + ")");
}
LOGGER.debug("Command still on execution");
try {
Thread.sleep(RETRY_TIME * S_TO_MS);
} catch (InterruptedException e) {
LOGGER.debug("Sleep interrupted");
Thread.currentThread().interrupt();
}
}
} catch (ConnectorException | IOException | JSchException e) {
throw new ConnectorException(ERROR_EXCEPTION_EXEC_COMMAND + user + "@" + workerIP, e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
LOGGER.warn(WARN_INPUTSTREAM_CLOSE);
}
}
if (exec != null && exec.isConnected()) {
LOGGER.debug("Disconnecting exec channel");
exec.disconnect();
}
if (session != null && session.isConnected()) {
LOGGER.debug("Disconnecting session");
session.disconnect();
}
}
return false;
}
use of com.jcraft.jsch.ChannelExec in project ASAP by salmant.
the class TTS1 method How_many_existing_servers.
// ///////////////////////////////////////////
public static int How_many_existing_servers() {
int i = 0;
String haproy_ip = "194.249.1.110";
String haproy_host_user = "root";
String haproy_host_password = "****************";
try {
String command = "cat /etc/haproxy/haproxy.cfg";
JSch jsch = new JSch();
com.jcraft.jsch.Session session = jsch.getSession(haproy_host_user, haproy_ip, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
;
session.setPassword(haproy_host_password);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream input = channel.getInputStream();
channel.connect();
try {
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(inputReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("-www"))
i++;
// System.out.println(line);
}
bufferedReader.close();
inputReader.close();
} catch (IOException ex) {
ex.printStackTrace();
return i;
}
channel.disconnect();
session.disconnect();
return i;
} catch (Exception ex) {
ex.printStackTrace();
return i;
}
}
Aggregations