Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. import java.io.File;
  2. import java.util.Properties;
  3. import java.util.Vector;
  4.  
  5. import com.jcraft.jsch.ChannelSftp;
  6. import com.jcraft.jsch.ChannelSftp.LsEntry;
  7. import com.jcraft.jsch.JSch;
  8. import com.jcraft.jsch.Session;
  9.  
  10. public class ConnectWithKey {
  11.  
  12.     public static void main(String[] args) throws Exception {
  13.        
  14.         if (args.length < 3) {
  15.             throw new Exception("not enough arguments");
  16.         }
  17.        
  18.         String serverUrl = args[0];
  19.         String userName = args[1];
  20.         String password = args[2];
  21.        
  22.         //File privateKeyFile = new File(args[2]);
  23. //      String passphrase = null;
  24. //     
  25. //      if (args.length > 3) {
  26. //          passphrase = args[3];
  27. //      }
  28.        
  29.         JSch jsch = new JSch();
  30.         //jsch.addIdentity(privateKeyFile.getAbsolutePath(), passphrase);
  31.        
  32.         Properties config = new Properties();
  33.         config.put("StrictHostKeyChecking", "no");
  34.         config.put("compression.s2c", "zlib,none");
  35.         config.put("compression.c2s", "zlib,none");
  36.        
  37.         Session session = jsch.getSession(userName, serverUrl);
  38.         session.setConfig(config);
  39.         session.setPort(2200);
  40.         session.setPassword(password);
  41.         session.connect();
  42.        
  43.         ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
  44.         channel.connect();
  45.  
  46.         @SuppressWarnings("unchecked")
  47.         final Vector<LsEntry> files = channel.ls(".");
  48.         for (LsEntry obj : files) {
  49.             System.out.println(obj.toString());
  50.         }  
  51.        
  52.         channel.disconnect();
  53.         session.disconnect();
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement