Skip to content
Snippets Groups Projects
Commit 5f6de754 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

closable

parent 7e84feda
No related branches found
No related tags found
No related merge requests found
package cz.it4i.fiji.scpclient; package cz.it4i.fiji.scpclient;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
...@@ -16,21 +17,22 @@ import com.jcraft.jsch.JSchException; ...@@ -16,21 +17,22 @@ import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session; import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo; import com.jcraft.jsch.UserInfo;
public class ScpClient { public class ScpClient implements Closeable {
public static void main(String[] args) throws JSchException, IOException { public static void main(String[] args) throws JSchException, IOException {
boolean u = new ScpClient("salomon.it4i.cz", "koz01", "/home/koz01/.ssh/it4i_rsa", "nejlepsivyzkum") try (ScpClient scpClient = new ScpClient("salomon.it4i.cz", "koz01", "/home/koz01/.ssh/it4i_rsa",
.upload(Paths.get("/home/koz01/aaa/vecmath.jar"), "/home/koz01/"); "nejlepsivyzkum")) {
boolean u = scpClient.upload(Paths.get("/home/koz01/aaa/vecmath.jar"), "/home/koz01/");
boolean d = new ScpClient("salomon.it4i.cz", "koz01", "/home/koz01/.ssh/it4i_rsa", "nejlepsivyzkum") boolean d = scpClient.download("/home/koz01/proof", Paths.get("/home/koz01/aaa/proof"));
.download("/home/koz01/proof", Paths.get("/home/koz01/aaa/proof")); System.out.println(u);
System.out.println(u); System.out.println(d);
System.out.println(d); }
} }
private String hostName; private String hostName;
private String username; private String username;
private JSch jsch = new JSch(); private JSch jsch = new JSch();
private Session session;
public ScpClient(String hostName, String username, Identity privateKeyFile) throws JSchException { public ScpClient(String hostName, String username, Identity privateKeyFile) throws JSchException {
super(); super();
...@@ -55,10 +57,12 @@ public class ScpClient { ...@@ -55,10 +57,12 @@ public class ScpClient {
public boolean download(String lfile, Path rfile) throws JSchException, IOException { public boolean download(String lfile, Path rfile) throws JSchException, IOException {
Session session = connectionSession(); Session session = connectionSession();
// exec 'scp -f rfile' remotely
String command = "scp -f " + lfile;
Channel channel = session.openChannel("exec");
try { try {
// exec 'scp -f rfile' remotely
String command = "scp -f " + lfile;
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command); ((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp // get I/O streams for remote scp
...@@ -142,7 +146,7 @@ public class ScpClient { ...@@ -142,7 +146,7 @@ public class ScpClient {
} }
} finally { } finally {
session.disconnect(); channel.disconnect();
} }
return true; return true;
} }
...@@ -157,69 +161,71 @@ public class ScpClient { ...@@ -157,69 +161,71 @@ public class ScpClient {
String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + rfile; String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + rfile;
Channel channel = session.openChannel("exec"); Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command); ((ChannelExec) channel).setCommand(command);
try { // get I/O streams for remote scp
// get I/O streams for remote scp try (OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream()) {
try (OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream()) { channel.connect();
channel.connect(); if (checkAck(in) != 0) {
if (checkAck(in) != 0) { return false;
return false; }
}
if (ptimestamp) {
command = "T " + (file.toFile().lastModified() / 1000) + " 0";
// The access time should be sent here,
// but it is not accessible with JavaAPI ;-<
command += (" " + (file.toFile().lastModified() / 1000) + " 0\n");
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
return false;
}
}
// send "C0644 filesize filename", where filename should not include '/' if (ptimestamp) {
long filesize = file.toFile().length(); command = "T " + (file.toFile().lastModified() / 1000) + " 0";
command = "C0644 " + filesize + " "; // The access time should be sent here,
command += file.getFileName().toString(); // but it is not accessible with JavaAPI ;-<
command += "\n"; command += (" " + (file.toFile().lastModified() / 1000) + " 0\n");
out.write(command.getBytes()); out.write(command.getBytes());
out.flush(); out.flush();
if (checkAck(in) != 0) { if (checkAck(in) != 0) {
return false; return false;
} }
byte[] buf = new byte[1024]; }
// send a content of lfile
try (InputStream fis = Files.newInputStream(file)) { // send "C0644 filesize filename", where filename should not include '/'
while (true) { long filesize = file.toFile().length();
int len = fis.read(buf, 0, buf.length); command = "C0644 " + filesize + " ";
if (len <= 0) command += file.getFileName().toString();
break; command += "\n";
out.write(buf, 0, len); // out.flush(); out.write(command.getBytes());
} out.flush();
} if (checkAck(in) != 0) {
// send '\0' return false;
buf[0] = 0; }
out.write(buf, 0, 1); byte[] buf = new byte[1024];
out.flush(); // send a content of lfile
if (checkAck(in) != 0) { try (InputStream fis = Files.newInputStream(file)) {
return false; while (true) {
int len = fis.read(buf, 0, buf.length);
if (len <= 0)
break;
out.write(buf, 0, len); // out.flush();
} }
out.close();
} }
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
if (checkAck(in) != 0) {
return false;
}
out.close();
} finally { } finally {
channel.disconnect(); channel.disconnect();
session.disconnect();
} }
return true; return true;
} }
private Session connectionSession() throws JSchException { private Session connectionSession() throws JSchException {
Session session = jsch.getSession(username, hostName); if (session == null) {
session = jsch.getSession(username, hostName);
UserInfo ui = new P_UserInfo(); UserInfo ui = new P_UserInfo();
session.setUserInfo(ui); session.setUserInfo(ui);
session.connect(); }
if (!session.isConnected()) {
session.connect();
}
return session; return session;
} }
...@@ -283,4 +289,11 @@ public class ScpClient { ...@@ -283,4 +289,11 @@ public class ScpClient {
} }
return b; return b;
} }
@Override
public void close() {
if (session.isConnected()) {
session.disconnect();
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment