use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.
the class Scp method getExecChannel.
/**
* @return ChannelExec
* @throws JSchException if something goes wrong
*/
private ChannelExec getExecChannel() throws JSchException {
ChannelExec channel;
channel = (ChannelExec) session.openChannel("exec");
return channel;
}
use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.
the class SshRepository method checkExistence.
/**
* check for existence of file or dir on target system
*
* @param filePath
* to the object to check
* @param session
* to use
* @return true: object exists, false otherwise
*/
private boolean checkExistence(String filePath, Session session) throws IOException {
Message.debug("SShRepository: checkExistence called: " + filePath);
ChannelExec channel = null;
channel = getExecChannel(session);
String fullCmd = replaceArgument(existCommand, filePath);
channel.setCommand(fullCmd);
StringBuilder stdOut = new StringBuilder();
StringBuilder stdErr = new StringBuilder();
readSessionOutput(channel, stdOut, stdErr);
return channel.getExitStatus() == 0;
}
use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.
the class SshRepository method list.
/*
* (non-Javadoc)
*
* @see org.apache.ivy.repository.Repository#list(java.lang.String)
*/
public List<String> list(String parent) throws IOException {
Message.debug("SShRepository:list called: " + parent);
List<String> result = new ArrayList<>();
Session session = null;
ChannelExec channel = null;
session = getSession(parent);
channel = getExecChannel(session);
URI parentUri = null;
try {
parentUri = new URI(parent);
} catch (URISyntaxException e) {
throw new IOException("The uri '" + parent + "' is not valid!", e);
}
String fullCmd = replaceArgument(listCommand, parentUri.getPath());
channel.setCommand(fullCmd);
StringBuilder stdOut = new StringBuilder();
StringBuilder stdErr = new StringBuilder();
readSessionOutput(channel, stdOut, stdErr);
if (channel.getExitStatus() != 0) {
Message.error("Ssh ListCommand exited with status != 0");
Message.error(stdErr.toString());
return null;
} else {
BufferedReader br = new BufferedReader(new StringReader(stdOut.toString()));
String line = null;
while ((line = br.readLine()) != null) {
result.add(line);
}
}
return result;
}
use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.
the class SshRepository method makePath.
/**
* Tries to create a directory path on the target system
*
* @param path
* to create
* @param session
* to use
*/
private void makePath(String path, Session session) throws IOException {
ChannelExec channel = null;
String trimmed = path;
try {
while (trimmed.length() > 0 && trimmed.charAt(trimmed.length() - 1) == fileSeparator) {
trimmed = trimmed.substring(0, trimmed.length() - 1);
}
if (trimmed.length() == 0 || checkExistence(trimmed, session)) {
return;
}
int nextSlash = trimmed.lastIndexOf(fileSeparator);
if (nextSlash > 0) {
String parent = trimmed.substring(0, nextSlash);
makePath(parent, session);
}
channel = getExecChannel(session);
String mkdir = replaceArgument(createDirCommand, trimmed);
Message.debug("SShRepository: trying to create path: " + mkdir);
channel.setCommand(mkdir);
StringBuilder stdOut = new StringBuilder();
StringBuilder stdErr = new StringBuilder();
readSessionOutput(channel, stdOut, stdErr);
} finally {
if (channel != null) {
channel.disconnect();
}
}
}
use of com.jcraft.jsch.ChannelExec in project nimbus by nimbus-org.
the class SCPClientImpl method mget.
public File[] mget(String remote, String localDir) throws SCPException {
if (jsch == null) {
throw new SCPException(scpClientFactoryServiceName, "Connection is not established!");
}
if (session == null) {
throw new SCPException(scpClientFactoryServiceName, "It is not authenticated!");
}
if (remote == null || remote.length() == 0) {
throw new SCPException(scpClientFactoryServiceName, "Path is null.");
}
if (localDir == null) {
localDir = ".";
}
File localDirFile = new File(localDir);
if (homeDir != null && !localDirFile.isAbsolute()) {
localDirFile = new File(homeDir, localDir);
}
List<File> localFiles = new ArrayList<File>();
Channel channel = null;
final String cmd = "scp -f " + remote;
File localFile = null;
try {
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(cmd);
OutputStream os = channel.getOutputStream();
InputStream is = channel.getInputStream();
channel.connect();
byte[] buf = new byte[1024];
os.write(0);
os.flush();
while (true) {
int c = checkAck(is);
if (c == -1) {
if (localFiles.size() == 0) {
throw new IOException("Remote SCP terminated unexpectedly.");
}
break;
}
if (c != 'C') {
break;
}
is.read(buf, 0, 5);
long fileSize = 0L;
while (true) {
if (is.read(buf, 0, 1) < 0) {
throw new SCPException(scpClientFactoryServiceName, "Unexpected EOF.");
}
if (buf[0] == ' ') {
break;
}
fileSize = fileSize * 10L + (long) (buf[0] - '0');
}
String fileName = null;
for (int i = 0; ; i++) {
is.read(buf, i, 1);
if (buf[i] == (byte) 0x0a) {
fileName = new String(buf, 0, i);
break;
}
}
buf[0] = 0;
os.write(buf, 0, 1);
os.flush();
localFile = new File(localDirFile, fileName);
localFiles.add(localFile);
FileOutputStream fos = new FileOutputStream(localFile);
try {
int readLen = 0;
while (true) {
if (buf.length < fileSize) {
readLen = buf.length;
} else {
readLen = (int) fileSize;
}
readLen = is.read(buf, 0, readLen);
if (readLen < 0) {
throw new SCPException(scpClientFactoryServiceName, "Unexpected EOF.");
}
fos.write(buf, 0, readLen);
fileSize -= readLen;
if (fileSize == 0L) {
break;
}
}
} finally {
fos.close();
fos = null;
}
checkAck(is);
localFile = null;
buf[0] = 0;
os.write(buf, 0, 1);
os.flush();
}
} catch (IOException e) {
throw new SCPException(scpClientFactoryServiceName, "It failed to mget! from=" + remote + ", to=" + localFile, e);
} catch (JSchException e) {
throw new SCPException(scpClientFactoryServiceName, "It failed to mget! from=" + remote + ", to=" + localFile, e);
} finally {
if (channel != null) {
channel.disconnect();
}
}
return localFiles.toArray(new File[localFiles.size()]);
}
Aggregations