Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
com.jcraft.jsch.JSch jsch = new com.jcraft.jsch.JSch();
//create SSH connection
String host = "some.ftp.com";
String user = "user";
String password = "password";//sftp password
com.jcraft.jsch.Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
java.util.Properties config=new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println(session.isConnected());
String command = "cd out; cp *.txt ../out_done/; rm *.txt";
try {
com.jcraft.jsch.Channel channel = session.openChannel("exec");
channel.setInputStream(null);
channel.setOutputStream(System.out);
((com.jcraft.jsch.ChannelExec) channel).setCommand(command);
channel.connect();
java.io.InputStream in = null;
byte[] tmp = new byte;
while (true) {
if (channel.isClosed()) {
System.out.println("exit-status:"+channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
}
catch (Exception ee) {
}
}
channel.disconnect();
}
catch (Exception e) {
e.printStackTrace();
}
session.disconnect();