use of com.jcraft.jsch.Channel in project airavata by apache.
the class SSHUtils method scpTo.
/**
* This will copy a local file to a remote location
*
* @param remoteFile remote location you want to transfer the file, this cannot be a directory, if user pass
* a dirctory we do copy it to that directory but we simply return the directory name
* todo handle the directory name as input and return the proper final output file name
* @param localFile Local file to transfer, this can be a directory
* @return returns the final remote file path, so that users can use the new file location
*/
public static String scpTo(String localFile, String remoteFile, Session session) throws IOException, JSchException, SSHApiException {
FileInputStream fis = null;
String prefix = null;
if (new File(localFile).isDirectory()) {
prefix = localFile + File.separator;
}
boolean ptimestamp = true;
// exec 'scp -t rfile' remotely
String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + remoteFile;
Channel channel = session.openChannel("exec");
StandardOutReader stdOutReader = new StandardOutReader();
((ChannelExec) channel).setErrStream(stdOutReader.getStandardError());
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
if (checkAck(in) != 0) {
String error = "Error Reading input Stream";
log.error(error);
throw new SSHApiException(error);
}
File _lfile = new File(localFile);
if (ptimestamp) {
command = "T" + (_lfile.lastModified() / 1000) + " 0";
// The access time should be sent here,
// but it is not accessible with JavaAPI ;-<
command += (" " + (_lfile.lastModified() / 1000) + " 0\n");
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
String error = "Error Reading input Stream";
log.error(error);
throw new SSHApiException(error);
}
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize = _lfile.length();
command = "C0644 " + filesize + " ";
if (localFile.lastIndexOf('/') > 0) {
command += localFile.substring(localFile.lastIndexOf('/') + 1);
} else {
command += localFile;
}
command += "\n";
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
String error = "Error Reading input Stream";
log.error(error);
throw new SSHApiException(error);
}
// send a content of localFile
fis = new FileInputStream(localFile);
byte[] buf = new byte[1024];
while (true) {
int len = fis.read(buf, 0, buf.length);
if (len <= 0)
break;
// out.flush();
out.write(buf, 0, len);
}
fis.close();
fis = null;
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
if (checkAck(in) != 0) {
String error = "Error Reading input Stream";
log.error(error);
throw new SSHApiException(error);
}
out.close();
stdOutReader.onOutput(channel);
channel.disconnect();
if (stdOutReader.getStdErrorString().contains("scp:")) {
throw new SSHApiException(stdOutReader.getStdErrorString());
}
// since remote file is always a file we just return the file
return remoteFile;
}
use of com.jcraft.jsch.Channel in project airavata by apache.
the class SSHUtils method makeDirectory.
public static void makeDirectory(String path, Session session) throws IOException, JSchException, GFacException {
// exec 'scp -t rfile' remotely
String command = "mkdir -p " + path;
Channel channel = session.openChannel("exec");
StandardOutReader stdOutReader = new StandardOutReader();
((ChannelExec) channel).setCommand(command);
((ChannelExec) channel).setErrStream(stdOutReader.getStandardError());
try {
channel.connect();
} catch (JSchException e) {
channel.disconnect();
// session.disconnect();
log.error("Unable to retrieve command output. Command - " + command + " on server - " + session.getHost() + ":" + session.getPort() + " connecting user name - " + session.getUserName());
throw e;
}
stdOutReader.onOutput(channel);
if (stdOutReader.getStdErrorString().contains("mkdir:")) {
throw new GFacException(stdOutReader.getStdErrorString());
}
channel.disconnect();
}
use of com.jcraft.jsch.Channel in project javautils by jiadongpo.
the class SFTPClient method login.
public void login(String password) throws Exception {
this.password = password;
s.setPassword(this.getPassword());
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
// set compression property
// zlib, none
String compress = getCompression();
if (compress != null) {
config.put(COMPRESSION_S2C, compress);
config.put(COMPRESSION_C2S, compress);
}
s.setConfig(config);
s.connect();
Channel channel = s.openChannel("sftp");
channel.connect();
c = (ChannelSftp) channel;
// 设置一个编辑,控制中文乱码问题
// c.setFilenameEncoding("GBK");
} catch (JSchException e) {
throw new Exception(e);
}
}
use of com.jcraft.jsch.Channel in project keepass2android by PhilippC.
the class SftpStorage method init.
ChannelSftp init(String filename) throws JSchException, UnsupportedEncodingException {
jsch = new JSch();
ConnectionInfo ci = splitStringToConnectionInfo(filename);
Session session = jsch.getSession(ci.username, ci.host, ci.port);
UserInfo ui = new SftpUserInfo(ci.password);
session.setUserInfo(ui);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp c = (ChannelSftp) channel;
logDebug("success: init Sftp");
return c;
}
use of com.jcraft.jsch.Channel in project ant by apache.
the class ScpFromMessage method execute.
/**
* Carry out the transfer.
* @throws IOException on i/o errors
* @throws JSchException on errors detected by scp
*/
@Override
public void execute() throws IOException, JSchException {
String command = "scp -f ";
if (isRecursive) {
command += "-r ";
}
if (getCompressed()) {
command += "-C ";
}
command += remoteFile;
final Channel channel = openExecChannel(command);
try {
// get I/O streams for remote scp
final OutputStream out = channel.getOutputStream();
final InputStream in = channel.getInputStream();
channel.connect();
sendAck(out);
startRemoteCpProtocol(in, out, localFile);
} finally {
if (channel != null) {
channel.disconnect();
}
}
log("done\n");
}
Aggregations