⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssh2simplesftpshell.java

📁 一个非常好的ssh客户端实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			    }			}			handle.asyncWait();			linereader.println("");			file.close();			sftp.close(handle);		    } else if(cmd.equals("put")) {			if(argv.length < 2) {			    doHelp();			    continue;			}			String remoteFile  = argv[1]; // Default to same name			String localFile = expandLocal(remoteFile);			remoteFile = remoteFile.replace(File.separatorChar,							'/');			if(argv.length > 2) {			    remoteFile = argv[2];			}			remoteFile = expandRemote(remoteFile);			handle = sftp.open(remoteFile,					   SSH2SFTP.SSH_FXF_WRITE |					   SSH2SFTP.SSH_FXF_TRUNC |					   SSH2SFTP.SSH_FXF_CREAT,					   new SSH2SFTP.FileAttributes());			f = new File(localFile);			FileInputStream fin = new FileInputStream(f);			linereader.println("upload to remote '" +					   remoteFile + "',");			linereader.println("       from local '" +					   localFile + "', " + f.length() +					   " bytes");			ProgressBar bar = new ProgressBar(linereader);			handle.addAsyncListener(bar);			bar.start((int)f.length());			sftp.writeFully(handle, fin);			/* !!! Can't interrupt now !!!			    if(linereader.ctrlCPressed()) {				handle.asyncClose();			    }			*/			linereader.println("");			fin.close();		    		    } else if(cmd.equals("pwd")) {			linereader.println("local dir:\t" + localDir);			linereader.println("remote dir:\t" + remoteDir);		    } else if(cmd.equals("rm")) {			if(argv.length < 2) {			    doHelp();			    continue;			}			sftp.remove(expandRemote(argv[1]));		    } else if(cmd.startsWith("ren")) {			if(argv.length < 3) {			    doHelp();			    continue;			}			sftp.rename(expandRemote(argv[1]),				    expandRemote(argv[2]));		    } else if(cmd.startsWith("lren")) {			if(argv.length < 3) {			    doHelp();			    continue;			}			f = new File(expandLocal(argv[1]));			f.renameTo(new File(expandLocal(argv[2])));		    } else if(cmd.equals("lrm") || cmd.equals("lrmdir")) {			if(argv.length < 2) {			    doHelp();			    continue;			}			f = new File(expandLocal(argv[1]));			f.delete();		    } else if(cmd.equals("rmdir")) {			if(argv.length < 2) {			    doHelp();			    continue;			}			sftp.rmdir(expandRemote(argv[1]));		    } else if(cmd.equals("mkdir")) {			if(argv.length < 2) {			    doHelp();			    continue;			}			sftp.mkdir(expandRemote(argv[1]),				   new SSH2SFTP.FileAttributes());		    } else if(cmd.equals("lmkdir")) {			if(argv.length < 2) {			    doHelp();			    continue;			}			f = new File(expandLocal(argv[1]));			f.mkdir();		    } else if(cmd.equals("q") || cmd.equals("quit")) {			linereader.println("exiting...");			keepRunning = false;		    } else {			doHelp();		    }		} catch (SSH2SFTP.SFTPNoSuchFileException e) {		    linereader.println("No such file or directory");		} catch (SSH2SFTP.SFTPPermissionDeniedException e) {		    linereader.println("Permission denied");		} catch (SSH2SFTP.SFTPAsyncAbortException e) {		    linereader.println("");		    linereader.println("Async transfer aborted");		}	    }	} catch (SSH2SFTP.SFTPDisconnectException e) {	    /* Exit because session was closed */	} catch (SSH2SFTP.SFTPException e) {	    String msg = e.getMessage();	    linereader.println("sftp error: " + ((msg != null &&						  msg.length() > 0) ? msg :						 e.toString()));	} catch (LineReaderTerminal.ExternalMessageException e) {	    /* Exit on window close */	    frame.dispose();	    return;	} catch (Exception e) {	    System.out.println("Fatal error in SFTP:");	    e.printStackTrace();	    linereader.println("fatal error: " + e);	}	try {	    linereader.promptLine("\n\rPress <return> to exit sftp shell", null,				  false);	} catch (LineReaderTerminal.ExternalMessageException e) {	}	if(sftp != null) {	    sftp.terminate();	}	frame.dispose();    }    /**     * If needed, expand the given filename to include full path on     * the remote side.     *     * @param name Name to expand     *     * @return A full path.     */    public String expandRemote(String name) {	if(name.charAt(0) != '/')	    name = remoteDir + "/" + name;	return name;    }    /**     * If needed, expand the given filename to include full path on     * the local side.     *     * @param name Name to expand     *     * @return A full path.     */    public String expandLocal(String name) {	if(name.length() > 1 &&	   name.charAt(0) != File.separatorChar &&	   name.charAt(1) != ':') {	    name = localDir + File.separator + name;	}	return name;    }    /**     * Extract the next argument from a space-separated list of arguments.     *     * @param args List of arguments. This list is modified in the process.     *     * @return The fisr argument in the list.     */    public String getNextArg(String args) {	int i = args.indexOf(' ');	if(i > -1)	    args = args.substring(0, i);	return args;    }    /**     * Split a command line into arguments. This will split the given     * command line into individual arguments. The arguments are     * assumed to be separated by spaces. There is no quote handling     * or escape mechanism.     *     * @param cmdLine Command line to extract arguments from.     *     * @return An ordered array of arguments.     */    public String[] makeArgv(String cmdLine) {	String[] argv = new String[32];	String[] argvRet;	int n = 0, i;	while(cmdLine != null) {	    argv[n++] = getNextArg(cmdLine);	    i = cmdLine.indexOf(' ');	    if(i > -1) {		cmdLine = cmdLine.substring(i);		cmdLine = cmdLine.trim();	    } else		cmdLine = null;	}	argvRet = new String[n];	System.arraycopy(argv, 0, argvRet, 0, n);	return argvRet;    }    /**     * Print help text.     */    public void doHelp() {	linereader.println("");	linereader.println("The following commands are available:");	linereader.println("");	linereader.println("pwd\t\t\t\t\tshow current local/remote directory");	linereader.println("cd <dir>\t\t\t\tchange current directory (remote)");	linereader.println("lcd <dir>\t\t\t\tchange current directory (local)");	linereader.println("ls\t\t\t\t\tlist current directory (remote)");	linereader.println("lls\t\t\t\t\tlist current directory (local)");	linereader.println("get <remote-file> [<local-file>]\tdownload file from remote host");	linereader.println("put <local-file> [<remote-file>]\tupload file to remote host");	linereader.println("ren <from-file> <to-file>\t\trename remote file");	linereader.println("lren <from-file> <to-file>\t\trename local file");	linereader.println("rm <remote-file>\t\t\tremove remote file");	linereader.println("lrm <local-file>\t\t\tremove local file");	linereader.println("mkdir <remote-dir>\t\t\tcreate remote directory");	linereader.println("lmkdir <local-dir>\t\t\tcreate local directory");	linereader.println("rmdir <remote-dir>\t\t\tremove remote directory");	linereader.println("lrmdir <local-dir>\t\t\tremove local directory");	linereader.println("quit\t\t\t\t\tquit this sftp session");	linereader.println("");    }    public TerminalWin getTerminal() {	return terminal;    }    /*	    SSH2SFTPClient sftp = new SSH2SFTPClient(connection);	    SSH2SFTP.FileHandle handle = sftp.opendir(".");	    SSH2SFTP.FileAttributes[] list = sftp.readdir(handle);	    System.out.println("length: " + list.length);	    for(i = 0; i < list.length; i++) {		System.out.println(list[i].lname);	    }	    SSH2SFTP.FileAttributes attrs = sftp.stat(".");	    System.out.println("attrs: " + attrs);	    attrs = sftp.realpath(".");	    System.out.println("lname-rp: " + attrs.lname);	    System.out.println("attrs-rp: " + attrs);	    handle = sftp.open("/tmp/hostkey", SSH2SFTP.SSH_FXF_READ,			       new SSH2SFTP.FileAttributes());	    java.io.FileOutputStream fout = new java.io.FileOutputStream("/tmp/keycopy");	    byte[] buf = new byte[1024];	    long foffs = 0;	    int len;	    while((len = sftp.read(handle, foffs, buf, 0, 1024)) > 0) {		System.out.println("read: " + len);		fout.write(buf, 0, len);		foffs += len;	    }	    sftp.close(handle);	    fout.close();	    handle = sftp.open("/tmp/foobar", SSH2SFTP.SSH_FXF_WRITE |			       SSH2SFTP.SSH_FXF_CREAT,			       new SSH2SFTP.FileAttributes());	    FileInputStream fin = new FileInputStream("/tmp/keycopy");	    buf = new byte[1024];	    foffs = 0;	    while((len = fin.read(buf, 0, 1024)) > 0) {		System.out.println("wrote: " + len);		sftp.write(handle, foffs, buf, 0, len);		foffs += len;	    }	    sftp.close(handle);	    fin.close();	    sftp.terminate();    */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -