• 大小: 1.53MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-11-05
  • 语言: Java
  • 标签: java  scp  

资源简介

java scp实现方式

资源截图

代码片段和文件信息

package org.ssh.scp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class CommandRunner
{
    private static final Logger logger = Logger.getLogger(CommandRunner.class);
    
    private CommandRunner()
    {
        
    }
    
    /** 

    * Get remote file through scp 

    * @param host 

    * @param username 

    * @param password 

    * @param remoteFile 

    * @param localDir 

    * @throws IOException 

    */
    
    public static void scpGet(String host String username String password

    String remoteFile String localDir)
        throws IOException
    {
        
        if (logger.isDebugEnabled())
        {
            
            logger.debug(“spc [“ + remoteFile + “] from “ + host + “ to “ + localDir);
            
        }
        
        Connection conn = getOpenedConnection(host username password);
        
        SCPClient client = new SCPClient(conn);
        
        client.get(remoteFile localDir);
        
        conn.close();
        
    }
    
    /** 

    * Put local file to remote machine. 

    * @param host 

    * @param username 

    * @param password 

    * @param localFile 

    * @param remoteDir 

    * @throws IOException 

    */
    
    public static void scpPut(String host String username String password

    String localFile String remoteDir)
        throws IOException
    {
        
        if (logger.isDebugEnabled())
        {
            
            logger.debug(“spc [“ + localFile + “] to “ + host + remoteDir);
            
        }
        
        Connection conn = getOpenedConnection(host username password);
        
        SCPClient client = new SCPClient(conn);
        
        client.put(localFile remoteDir);
        
        conn.close();
        
    }
    
    /** 

    * Run SSH command. 

    * @param host 

    * @param username 

    * @param password 

    * @param cmd 

    * @return exit status 

    * @throws IOException 

    */
    
    public static int runSSH(String host String username String password

    String cmd)
        throws IOException
    {
        
        if (logger.isDebugEnabled())
        {
            
            logger.debug(“running SSH cmd [“ + cmd + “]“);
            
        }
        
        Connection conn = getOpenedConnection(host username password);
        
        Session sess = conn.openSession();
        
        sess.execCommand(cmd);
        
        InputStream stdout = new StreamGobbler(sess.getStdout());
        
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        
        while (true)
 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件         43  2011-11-19 13:50  scp\address.txt

     文件       5483  2011-11-19 13:57  scp\CommandRunner.java

     文件       1857  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\examples\Basic.java

     文件       2295  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\examples\BasicWithHTTPProxy.java

     文件       3409  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\examples\PortForwarding.java

     文件       1588  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\examples\PublicKeyAuthentication.java

     文件       1399  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\examples\SimpleVerifier.java

     文件       4029  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\examples\SingleThreadStdoutStderr.java

     文件       1827  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\examples\StdoutAndStderr.java

     文件      18627  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\examples\SwingShell.java

     文件       1797  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\examples\UsingKnownHosts.java

     文件      16506  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\faq\FAQ.html

     文件     244831  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\ganymed-ssh2-build210.jar

     文件      13588  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\HISTORY.txt

     文件       3104  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\allclasses-frame.html

     文件       2684  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\allclasses-noframe.html

     文件      13600  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\ChannelCondition.html

     文件       5599  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\ChannelCondition.html

     文件       7721  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\Connection.html

     文件       9308  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\ConnectionInfo.html

     文件       7053  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\ConnectionMonitor.html

     文件       6894  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\DHGexParameters.html

     文件       5555  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\HTTPProxyData.html

     文件       5595  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\HTTPProxyException.html

     文件       8097  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\InteractiveCallback.html

     文件       5531  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\KnownHosts.html

     文件       7133  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\LocalPortForwarder.html

     文件       7118  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\LocalStreamForwarder.html

     文件       7702  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\ProxyData.html

     文件       6883  2006-10-10 12:35  scp\ganymed-ssh2-build210\ganymed-ssh2-build210\javadoc\ch\ethz\ssh2\class-use\SCPClient.html

............此处省略209个文件信息

评论

共有 条评论