Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.util.Properties;
- import java.util.Vector;
- import com.jcraft.jsch.ChannelSftp;
- import com.jcraft.jsch.ChannelSftp.LsEntry;
- import com.jcraft.jsch.JSch;
- import com.jcraft.jsch.Session;
- public class ConnectWithKey {
- public static void main(String[] args) throws Exception {
- if (args.length < 3) {
- throw new Exception("not enough arguments");
- }
- String serverUrl = args[0];
- String userName = args[1];
- String password = args[2];
- //File privateKeyFile = new File(args[2]);
- // String passphrase = null;
- //
- // if (args.length > 3) {
- // passphrase = args[3];
- // }
- JSch jsch = new JSch();
- //jsch.addIdentity(privateKeyFile.getAbsolutePath(), passphrase);
- Properties config = new Properties();
- config.put("StrictHostKeyChecking", "no");
- config.put("compression.s2c", "zlib,none");
- config.put("compression.c2s", "zlib,none");
- Session session = jsch.getSession(userName, serverUrl);
- session.setConfig(config);
- session.setPort(2200);
- session.setPassword(password);
- session.connect();
- ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
- channel.connect();
- @SuppressWarnings("unchecked")
- final Vector<LsEntry> files = channel.ls(".");
- for (LsEntry obj : files) {
- System.out.println(obj.toString());
- }
- channel.disconnect();
- session.disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement