One Star

tFTPGet with SFTP.

Hi everyone,
This isn't so much a question as it is sharing how I solved the problem with tFTPDelete and tFTPPut with SFTP support using a password as an authentication method. I was tasked with getting files from an SFTP location then moving a copy of the file to another location within the SFTP host and removing the original file. For some reason I was able to use tFTPGet to actually get the files but then it would error out on either tFTPDelete or tFTPPut (with SFTP). The error would always say File Not Found or something along those lines. However the file was in fact there!!!
At any rate I ended with a job like this:
tFTPGet -----on sub job ok---> tJava

In tFTPGet I did what I needed to get the file from the SFTP location.
In tJava I used JSch as follows:
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();

Hope someone else finds this useful/helpful.
1 REPLY
Community Manager

Re: tFTPGet with SFTP.

Hi
Thanks for your sharing!Smiley Wink
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business