001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.libcheck; 018 019import java.util.Properties; 020import java.util.Vector; 021 022import com.jcraft.jsch.ChannelSftp; 023import com.jcraft.jsch.JSch; 024import com.jcraft.jsch.Session; 025import com.jcraft.jsch.UserInfo; 026 027/** 028 * Basic check for SFTP. 029 */ 030public final class SftpCheck { 031 032 private static final int ARG_COUNT = 4; 033 034 /** 035 * Invokes this example from the command line. 036 * 037 * @param args Arguments TODO 038 * @throws Exception If anything goes wrong. 039 */ 040 public static void main(final String[] args) throws Exception { 041 if (args.length != ARG_COUNT) { 042 throw new IllegalArgumentException("Usage: SftpCheck user pass host dir"); 043 } 044 final String user = args[0]; 045 final String pass = args[1]; 046 final String host = args[2]; 047 final String dir = args[3]; 048 049 final Properties props = new Properties(); 050 props.setProperty("StrictHostKeyChecking", "false"); 051 final JSch jsch = new JSch(); 052 final Session session = jsch.getSession(user, host, 22); 053 session.setUserInfo(new UserInfo() { 054 @Override 055 public String getPassphrase() { 056 return null; 057 } 058 059 @Override 060 public String getPassword() { 061 return null; 062 } 063 064 @Override 065 public boolean promptPassphrase(final String string) { 066 return false; 067 } 068 069 @Override 070 public boolean promptPassword(final String string) { 071 return false; 072 } 073 074 @Override 075 public boolean promptYesNo(final String string) { 076 return true; 077 } 078 079 @Override 080 public void showMessage(final String string) { 081 } 082 }); 083 session.setPassword(pass); 084 session.connect(); 085 final ChannelSftp chan = (ChannelSftp) session.openChannel("sftp"); 086 chan.connect(); 087 final Vector<?> list = chan.ls(dir); 088 list.forEach(System.err::println); 089 System.err.println("done"); 090 chan.disconnect(); 091 session.disconnect(); 092 } 093 094 private SftpCheck() { 095 /* main class not instantiated. */ 096 } 097}