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.Iterator; 020import java.util.Properties; 021import java.util.Vector; 022 023import com.jcraft.jsch.ChannelSftp; 024import com.jcraft.jsch.JSch; 025import com.jcraft.jsch.Session; 026import com.jcraft.jsch.UserInfo; 027 028/** 029 * Basic check for SFTP. 030 */ 031public final class SftpCheck { 032 private SftpCheck() { 033 /* main class not instantiated. */ 034 } 035 036 public static void main(final String[] args) throws Exception { 037 if (args.length != 4) { 038 throw new IllegalArgumentException("Usage: SftpCheck user pass host dir"); 039 } 040 final String user = args[0]; 041 final String pass = args[1]; 042 final String host = args[2]; 043 final String dir = args[3]; 044 045 final Properties props = new Properties(); 046 props.setProperty("StrictHostKeyChecking", "false"); 047 final JSch jsch = new JSch(); 048 final Session session = jsch.getSession(user, host, 22); 049 session.setUserInfo(new UserInfo() { 050 @Override 051 public String getPassphrase() { 052 return null; 053 } 054 055 @Override 056 public String getPassword() { 057 return null; 058 } 059 060 @Override 061 public boolean promptPassword(final String string) { 062 return false; 063 } 064 065 @Override 066 public boolean promptPassphrase(final String string) { 067 return false; 068 } 069 070 @Override 071 public boolean promptYesNo(final String string) { 072 return true; 073 } 074 075 @Override 076 public void showMessage(final String string) { 077 } 078 }); 079 session.setPassword(pass); 080 session.connect(); 081 final ChannelSftp chan = (ChannelSftp) session.openChannel("sftp"); 082 chan.connect(); 083 final Vector<?> list = chan.ls(dir); 084 final Iterator<?> iterList = list.iterator(); 085 while (iterList.hasNext()) { 086 System.err.println(iterList.next()); 087 } 088 System.err.println("done"); 089 chan.disconnect(); 090 session.disconnect(); 091 } 092}