Hi, 
 
I am trying to use jsch to send files using sftp protocol to vendor's
sftp server. The tool works everything fine with the vendor's sftp
server except that the server locks out our user accout after we send 10
files over. The reason they quote was there were empty login attempts
(i.e. empty username and password) between each file transfer, which
causing the invalid user login exceed their limit. We try this code in
our testing sftp server but couldn't found any invalid login attempts in
the logs. Here is my code. 
 
Please note that the sftp server in our vendor's side is not a real sftp
server, but rather wraps a sftp font end to support their back end
mainframe system. 
 

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
 
 
 
import java.io.*;
 
public class JCraftSFTPClient {
 private String host;
 private String user;
 private String password;
 private int port;
 private int mode;
 Session session = null;
 ChannelSftp channel = null;
 
 public static final int OVERWRITE=0;
 public static final int RESUME=1;
 public static final int APPEND=2;
 
 public static final String Success = "Success";
 public static final String Failure = "Failure";
 
 public JCraftSFTPClient(String host, String user, String password, int
port) {
  this(host, user, password, port,0);
 }
 
 public JCraftSFTPClient(String host, String user, String password, int
port, int mode) {
  super();
  // TODO Auto-generated constructor stub
  this.host = host;
  this.user = user;
  this.password = password;
  this.port = port;
  this.mode = mode;
  
  
 }
 
 
 
 public ChannelSftp getChannel() {
  return channel;
 }
 
 
 private void connect() throws JSchException{
   
         JSch jsch = new JSch();
         
       
        // ChannelSftp c = null;
         
   try {
   //Create a session sending through our username and password
   session = jsch.getSession(this.user, host, port);
    
   session.setPassword(password);
   
   //Security.addProvider(new com.sun.crypto.provider.SunJCE());
 
   //
   //Setup Strict HostKeyChecking to no so we dont get the 
   //unknown host key exception
   //
   java.util.Properties config = new java.util.Properties();
   config.put("StrictHostKeyChecking", "no");
   session.setConfig(config);
   session.connect();
 
   //
   //Open the SFTP channel
   //
   channel = (ChannelSftp)session.openChannel("sftp");
   channel.connect();
//   return (ChannelSftp)channel;
  } catch (Exception e) {
   // TODO Auto-generated catch block
   throw new JSchException("Unable to connect to SFTP server. "+
e.toString());
  }
 
 }
 
 
          
    public static String sftpPut(String host, String user, String
password, 
      int port, byte sourceData [], String remoteDir, String
destFileName)   
    throws Exception{
     JCraftSFTPClient client = new
JCraftSFTPClient(host,user,password,port,0);
//     ChannelSftp c = null;
     ByteArrayInputStream bis = null;
     try {
   client.connect();
   
//   System.out.println("Changing dir in SFTP server to: " + remoteDir);
//             c.cd(remoteDir);  
          bis = new ByteArrayInputStream(sourceData);
          String dest = remoteDir + "/" + destFileName; 
             client.getChannel()._put(bis, dest, null,0);
             System.out.println("File was sent to SFTP server
successfully. " + dest );
             return Success;
             
  } catch (Exception e) {
   // TODO Auto-generated catch block
    e.printStackTrace();
    System.out.println(e.getMessage());
    return Failure;
   //throw new Exception ("Unable to transfer file to SFTP server. ",
e);   
   
  }finally{
   if ( client.getChannel() != null ){
    try{
     client.getChannel().quit();    
    }catch(Exception exc){
     System.err.println("Unable to disconnect from SFTP server. " +
exc.toString());
    }
   }
   if ( client.getSession() != null ){
    try{     
     client.getSession().disconnect();
    
    }catch(Exception exc){
     System.err.println("Unable to disconnect from SFTP server. " +
exc.toString());
    }
   }
   if ( bis != null ){
    try{     
     bis.close();
    }catch(Exception exc){
     System.err.println(exc.toString());
    }
   }
  }
    }
    public static void main (String args []){
//     String host = "localhost";
     String host = "ddc1hu16.hydroone.com";
     String user = "csftptst";
     String password = "ftptst4cs";
     String message = "This is a testing message.";
     String remoteDir = "sftp";
     String dest = "sfpt_test_June12.txt";
     
 
     
     try {
   String ret = JCraftSFTPClient.sftpPut(host,user,password,22,
     message.getBytes(),remoteDir,dest);
   System.out.println(ret);
   
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     
   
        System.out.println("Process Complete.");
           System.exit(0);
    }
 
 public Session getSession() {
  return session;
 } 
 
 
    } 
  
 
 

I appreciate any comments. 
 
Elaine
-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to