/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ import com.jcraft.jsch.*; import java.awt.*; import javax.swing.*; public class Sftp{ public static void main(String[] arg){ try{ JSch jsch=new JSch(); String host=null; if(arg.length>0){ host=arg[0]; } else{ host=JOptionPane.showInputDialog("Enter username@hostname", System.getProperty("user.name")+ "@localhost"); } String user=host.substring(0, host.indexOf('@')); host=host.substring(host.indexOf('@')+1); int port=22; Session session=jsch.getSession(user, host, port); // username and password will be given via UserInfo interface. UserInfo ui=new MyUserInfo(); session.setUserInfo(ui); session.connect(); Channel channel=session.openChannel("sftp"); channel.connect(); ChannelSftp c=(ChannelSftp)channel; java.io.InputStream in=System.in; java.io.PrintStream out=System.out; java.util.Vector cmds=new java.util.Vector(); byte[] buf=new byte[1024]; int i; String str; int level=0; while(true){ out.print("sftp> "); cmds.removeAllElements(); i=in.read(buf, 0, 1024); if(i<=0)break; i--; if(i>0 && buf[i-1]==0x0d)i--; //str=new String(buf, 0, i); //System.out.println("|"+str+"|"); int s=0; for(int ii=0; ii0){ cmds.addElement(new String(buf, s, ii-s)); } while(ii'7'){foo=-1; break;} foo<<=3; foo|=(k-'0'); } if(foo==-1)continue; } else{ try{foo=Integer.parseInt((String)cmds.elementAt(1));} catch(Exception e){continue;} } try{ if(cmd.equals("chgrp")){ c.chgrp(foo, path); } else if(cmd.equals("chown")){ c.chown(foo, path); } else if(cmd.equals("chmod")){ c.chmod(foo, path); } } catch(SftpException e){ System.out.println(e.toString()); } continue; } if(cmd.equals("pwd") || cmd.equals("lpwd")){ str=(cmd.equals("pwd")?"Remote":"Local"); str+=" working directory: "; if(cmd.equals("pwd")) str+=c.pwd(); else str+=c.lpwd(); out.println(str); continue; } if(cmd.equals("ls") || cmd.equals("dir")){ String path="."; if(cmds.size()==2) path=(String)cmds.elementAt(1); try{ java.util.Vector vv=c.ls(path); if(vv!=null){ for(int ii=0; ii=this.count*100/max){ return true; } percent=this.count*100/max; monitor.setNote("Completed "+this.count+"("+percent+"%) out of "+max+"."); monitor.setProgress((int)this.count); return !(monitor.isCanceled()); } public void end(){ monitor.close(); } } private static String help = " Available commands:\n"+ " * means unimplemented command.\n"+ "cd path Change remote directory to 'path'\n"+ "lcd path Change local directory to 'path'\n"+ "chgrp grp path Change group of file 'path' to 'grp'\n"+ "chmod mode path Change permissions of file 'path' to 'mode'\n"+ "chown own path Change owner of file 'path' to 'own'\n"+ "help Display this help text\n"+ "get remote-path [local-path] Download file\n"+ "get-resume remote-path [local-path] Resume to download file.\n"+ "get-append remote-path [local-path] Append remote file to local file\n"+ "*lls [ls-options [path]] Display local directory listing\n"+ "ln oldpath newpath Symlink remote file\n"+ "*lmkdir path Create local directory\n"+ "lpwd Print local working directory\n"+ "ls [path] Display remote directory listing\n"+ "*lumask umask Set local umask to 'umask'\n"+ "mkdir path Create remote directory\n"+ "put local-path [remote-path] Upload file\n"+ "put-resume local-path [remote-path] Resume to upload file\n"+ "put-append local-path [remote-path] Append local file to remote file.\n"+ "pwd Display remote working directory\n"+ "stat path Display info about path\n"+ "exit Quit sftp\n"+ "quit Quit sftp\n"+ "rename oldpath newpath Rename remote file\n"+ "rmdir path Remove remote directory\n"+ "rm path Delete remote file\n"+ "symlink oldpath newpath Symlink remote file\n"+ "readlink path Check the target of a symbolic link\n"+ "realpath path Canonicalize the path\n"+ "rekey Key re-exchanging\n"+ "compression level Packet compression will be enabled\n"+ "version Show SFTP version\n"+ "? Synonym for help"; }