1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.libcheck;
18
19 import java.util.Properties;
20 import java.util.Vector;
21
22 import com.jcraft.jsch.ChannelSftp;
23 import com.jcraft.jsch.JSch;
24 import com.jcraft.jsch.Session;
25 import com.jcraft.jsch.UserInfo;
26
27
28
29
30 public final class SftpCheck {
31 private SftpCheck() {
32
33 }
34
35 public static void main(final String[] args) throws Exception {
36 if (args.length != 4) {
37 throw new IllegalArgumentException("Usage: SftpCheck user pass host dir");
38 }
39 final String user = args[0];
40 final String pass = args[1];
41 final String host = args[2];
42 final String dir = args[3];
43
44 final Properties props = new Properties();
45 props.setProperty("StrictHostKeyChecking", "false");
46 final JSch jsch = new JSch();
47 final Session session = jsch.getSession(user, host, 22);
48 session.setUserInfo(new UserInfo() {
49 @Override
50 public String getPassphrase() {
51 return null;
52 }
53
54 @Override
55 public String getPassword() {
56 return null;
57 }
58
59 @Override
60 public boolean promptPassword(final String string) {
61 return false;
62 }
63
64 @Override
65 public boolean promptPassphrase(final String string) {
66 return false;
67 }
68
69 @Override
70 public boolean promptYesNo(final String string) {
71 return true;
72 }
73
74 @Override
75 public void showMessage(final String string) {
76 }
77 });
78 session.setPassword(pass);
79 session.connect();
80 final ChannelSftp chan = (ChannelSftp) session.openChannel("sftp");
81 chan.connect();
82 final Vector<?> list = chan.ls(dir);
83 list.forEach(System.err::println);
84 System.err.println("done");
85 chan.disconnect();
86 session.disconnect();
87 }
88 }