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.Iterator;
20  import java.util.Properties;
21  import java.util.Vector;
22  
23  import com.jcraft.jsch.ChannelSftp;
24  import com.jcraft.jsch.JSch;
25  import com.jcraft.jsch.Session;
26  import com.jcraft.jsch.UserInfo;
27  
28  /**
29   * Basic check for SFTP.
30   */
31  public final class SftpCheck {
32      private SftpCheck() {
33          /* main class not instantiated. */
34      }
35  
36      public static void main(final String[] args) throws Exception {
37          if (args.length != 4) {
38              throw new IllegalArgumentException("Usage: SftpCheck user pass host dir");
39          }
40          final String user = args[0];
41          final String pass = args[1];
42          final String host = args[2];
43          final String dir = args[3];
44  
45          final Properties props = new Properties();
46          props.setProperty("StrictHostKeyChecking", "false");
47          final JSch jsch = new JSch();
48          final Session session = jsch.getSession(user, host, 22);
49          session.setUserInfo(new UserInfo() {
50              @Override
51              public String getPassphrase() {
52                  return null;
53              }
54  
55              @Override
56              public String getPassword() {
57                  return null;
58              }
59  
60              @Override
61              public boolean promptPassword(final String string) {
62                  return false;
63              }
64  
65              @Override
66              public boolean promptPassphrase(final String string) {
67                  return false;
68              }
69  
70              @Override
71              public boolean promptYesNo(final String string) {
72                  return true;
73              }
74  
75              @Override
76              public void showMessage(final String string) {
77              }
78          });
79          session.setPassword(pass);
80          session.connect();
81          final ChannelSftp chan = (ChannelSftp) session.openChannel("sftp");
82          chan.connect();
83          final Vector<?> list = chan.ls(dir);
84          final Iterator<?> iterList = list.iterator();
85          while (iterList.hasNext()) {
86              System.err.println(iterList.next());
87          }
88          System.err.println("done");
89          chan.disconnect();
90          session.disconnect();
91      }
92  }