View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Basic check for SFTP.
29   */
30  public final class SftpCheck {
31  
32      private static final int ARG_COUNT = 4;
33  
34      /**
35       * Invokes this example from the command line.
36       *
37       * @param args Arguments TODO
38       * @throws Exception If anything goes wrong.
39       */
40      public static void main(final String[] args) throws Exception {
41          if (args.length != ARG_COUNT) {
42              throw new IllegalArgumentException("Usage: SftpCheck user pass host dir");
43          }
44          final String user = args[0];
45          final String pass = args[1];
46          final String host = args[2];
47          final String dir = args[3];
48  
49          final Properties props = new Properties();
50          props.setProperty("StrictHostKeyChecking", "false");
51          final JSch jsch = new JSch();
52          final Session session = jsch.getSession(user, host, 22);
53          session.setUserInfo(new UserInfo() {
54              @Override
55              public String getPassphrase() {
56                  return null;
57              }
58  
59              @Override
60              public String getPassword() {
61                  return null;
62              }
63  
64              @Override
65              public boolean promptPassphrase(final String string) {
66                  return false;
67              }
68  
69              @Override
70              public boolean promptPassword(final String string) {
71                  return false;
72              }
73  
74              @Override
75              public boolean promptYesNo(final String string) {
76                  return true;
77              }
78  
79              @Override
80              public void showMessage(final String string) {
81              }
82          });
83          session.setPassword(pass);
84          session.connect();
85          final ChannelSftp chan = (ChannelSftp) session.openChannel("sftp");
86          chan.connect();
87          final Vector<?> list = chan.ls(dir);
88          list.forEach(System.err::println);
89          System.err.println("done");
90          chan.disconnect();
91          session.disconnect();
92      }
93  
94      private SftpCheck() {
95          /* main class not instantiated. */
96      }
97  }