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      private SftpCheck() {
32          /* main class not instantiated. */
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  }