Skip to content

Instantly share code, notes, and snippets.

@ldclakmal
Last active February 14, 2017 20:43
Show Gist options
  • Save ldclakmal/0b5c0abbe238775aef4d05b1d5d34ded to your computer and use it in GitHub Desktop.
Save ldclakmal/0b5c0abbe238775aef4d05b1d5d34ded to your computer and use it in GitHub Desktop.
public static void main(String[] arg) throws IOException, JSchException {
String remoteA = "/tmp/scp/remote-a/";
String remoteB = "/tmp/scp/remote-b/";
String local = "/tmp/scp/local/";
String fileName = "abc.txt";
String user = "chanaka";
String host = "localhost";
int port = 22;
String keyFilePath = "/home/chanaka/.ssh/id_rsa";
String keyPassword = null;
Session session = createSession(user, host, port, keyFilePath, keyPassword);
copyLocalToRemote(session, remoteA, local, fileName);
copyLocalToRemote(session, local, remoteB, fileName);
}
private static Session createSession(String user, String host, int port, String keyFilePath, String keyPassword) {
try {
JSch jsch = new JSch();
if (keyFilePath != null) {
if (keyPassword != null) {
jsch.addIdentity(keyFilePath, keyPassword);
} else {
jsch.addIdentity(keyFilePath);
}
}
Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
Session session = jsch.getSession(user, host, port);
session.setConfig(config);
session.connect();
return session;
} catch (JSchException e) {
System.out.println(e);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment