use of com.jcraft.jsch.Channel in project linuxtools by eclipse.
the class ScpClient method transfer.
public void transfer(String fromFile, String toFile) throws IOException, JSchException {
String rfile = toFile;
String lfile = fromFile;
// $NON-NLS-1$
String command = "scp -t " + rfile;
// $NON-NLS-1$
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
try (OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream()) {
channel.connect();
if (checkAck(in) != 0) {
// $NON-NLS-1$
System.out.println("err");
}
// send "C0644 filesize filename", where filename should not include
// '/'
long filesize = (new File(lfile)).length();
// $NON-NLS-1$ //$NON-NLS-2$
command = "C0644 " + filesize + " ";
if (lfile.lastIndexOf('/') > 0) {
command += lfile.substring(lfile.lastIndexOf('/') + 1);
} else {
command += lfile;
}
// $NON-NLS-1$
command += "\n";
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
// $NON-NLS-1$
System.out.println("err");
}
// send a content of lfile
byte[] buf = new byte[1024];
try (FileInputStream fis = new FileInputStream(lfile)) {
while (true) {
int len = fis.read(buf, 0, buf.length);
if (len <= 0) {
break;
}
out.write(buf, 0, len);
}
}
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
if (checkAck(in) != 0) {
// $NON-NLS-1$
System.out.println("err");
}
}
channel.disconnect();
session.disconnect();
}
use of com.jcraft.jsch.Channel in project OsmAnd-tools by osmandapp.
the class IndexUploader method uploadToSSH.
public void uploadToSSH(File f, String description, String size, String date, UploadSSHCredentials cred) throws IOException, JSchException {
log.info("Uploading file " + f.getName() + " " + size + " MB " + date + " of " + description);
// Upload to ftp
JSch jSch = new JSch();
boolean knownHosts = false;
if (cred.knownHosts != null) {
jSch.setKnownHosts(cred.knownHosts);
knownHosts = true;
}
if (cred.privateKey != null) {
jSch.addIdentity(cred.privateKey);
}
String serverName = cred.url;
if (serverName.startsWith("ssh://")) {
serverName = serverName.substring("ssh://".length());
}
Session session = jSch.getSession(cred.user, serverName);
if (cred.password != null) {
session.setPassword(cred.password);
}
if (!knownHosts) {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
}
String rfile = cred.path + "/" + f.getName();
String lfile = f.getAbsolutePath();
session.connect();
// exec 'scp -t rfile' remotely
String command = "scp -p -t \"" + rfile + "\"";
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();
if (checkAck(in) != 0) {
channel.disconnect();
session.disconnect();
return;
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize = (new File(lfile)).length();
command = "C0644 " + filesize + " ";
if (lfile.lastIndexOf('/') > 0) {
command += lfile.substring(lfile.lastIndexOf('/') + 1);
} else {
command += lfile;
}
command += "\n";
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
channel.disconnect();
session.disconnect();
return;
}
// send a content of lfile
FileInputStream fis = new FileInputStream(lfile);
byte[] buf = new byte[1024];
try {
int len;
while ((len = fis.read(buf, 0, buf.length)) > 0) {
// out.flush();
out.write(buf, 0, len);
}
} finally {
fis.close();
}
fis = null;
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
if (checkAck(in) != 0) {
channel.disconnect();
session.disconnect();
return;
}
out.close();
channel.disconnect();
session.disconnect();
log.info("Finish uploading file index");
}
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;
} catch (JSchException e) {
throw new Exception(e);
}
}
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;
} catch (JSchException e) {
throw new Exception(e);
}
}
use of com.jcraft.jsch.Channel 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();
}
Aggregations