コード例 #1
0
ファイル: SCPClient.cs プロジェクト: hst-bridge/BBS
        /// <summary>
        /// Transfer the remote file or directory to the local machine.
        /// </summary>
        /// <remarks>
        /// When transfering a directory the entire contents are recursively copied from the remote
        /// server. If for example you copy the remote directory "scp" and specify the local
        /// folder "C:\\" then a folder called "C:\\scp" will be created on the local filesystem with all
        /// the contents of the local directory.
        ///
        /// When transfering files, you can either specify the local directory or an alternative
        /// name and path for the file.
        /// </remarks>
        /// <example>
        /// [This example assumes parameter ssh is an authenticated instance of SSHClient]
        /// <code>
        /// // Create an SCPClient from an authenticated SSHClient instance
        /// SCPClient scp = new SCPClient(ssh);
        ///
        /// // Get the contents of a directory to the local users home folder
        /// scp.Get(new FileInfo(SCPClient.GetHomeDirectory()), "docs", true);
        ///
        /// // Get a remote file into a local directory
        /// scp.Get(new FileInfo("C:\\docs"), "docs/readme.txt", false);
        ///
        /// // Get a remote file and rename on the local machine
        /// scp.Get(new FileInfo("C:\\docs\\sometext.txt"), "docs/readme.txt", false);
        /// </code>
        /// </example>
        /// <param name="stream">A stream to copy the local file to.</param>
        /// <param name="remoteFile">The name of the remote file or directory to copy</param>
        /// <exception cref="Maverick.SSH.SSHException"/>
        public Stream Get(Stream stream, String remoteFile)
        {
            try
            {
                String cmd = "scp " + "-f " + remoteFile;
#if DEBUG
                System.Diagnostics.Trace.WriteLine("Creating SCPEngine with command: " + cmd);
#endif
                SCPEngine scp = new SCPEngine(this, cmd, ssh.OpenSessionChannel());

                try
                {
#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Reading file(s) from remote into " + remoteFile);
#endif
                    return(scp.ReadStreamFromRemote());
                }
                catch (Exception ex)
                {
                    throw ProcessException(ex);
                }
                finally
                {
                    scp.Close();
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }
コード例 #2
0
ファイル: SCPClient.cs プロジェクト: hst-bridge/BBS
        /// <summary>
        /// Transfer the local file or directory to the remote SSH server.
        /// </summary>
        /// <remarks>
        /// When transfering a directory the entire contents are recursively copied to the remote
        /// server. If for example you copy the local directory "C:\scp" and specify the remote
        /// folder "." then a folder called scp will be created on the remote filesystem with all
        /// the contents of the local directory.
        ///
        /// When transfering files, you can either specify the remote directory or an alternative
        /// name and path for the file.
        /// </remarks>
        /// <example>
        /// [This example assumes parameter ssh is an authenticated instance of SSHClient]
        /// <code>
        /// // Create an SCPClient from an authenticated SSHClient instance
        /// SCPClient scp = new SCPClient(ssh);
        ///
        /// // Copy the contents of a local folder to a remote folder
        /// scp.Put(new FileInfo("C:\\workspace"), ".");
        ///
        /// // Copy a file from the local machime to a remote folder
        /// scp.Put(new FileInfo("C:\\workspace\\readme.txt"), ".");
        ///
        /// // Copy a file from the local machine and rename on the remote machine
        /// scp.Put(new FileInfo("C:\\workspace\\readme.txt"), "sometext.txt");
        /// </code>
        /// </example>
        /// <param name="localfile">The name of a local file or directory to copy.</param>
        /// <param name="remotefile">The location to place the copied file or directory</param>
        /// <exception cref="Maverick.SSH.SSHException"/>
        public void Put(FileInfo localfile, String remotefile)
        {
            if (!File.Exists(localfile.FullName) && !Directory.Exists(localfile.FullName))
            {
                throw new FileNotFoundException(localfile.FullName + " does not exist as either a file or directory!");
            }

            bool recursive = Directory.Exists(localfile.FullName);

            try
            {
                String cmd = "scp " +
                             (Directory.Exists(localfile.FullName) ? "-d " : "")
                             + "-t "
                             + (recursive ? "-r " : "")
                             + remotefile;
#if DEBUG
                System.Diagnostics.Trace.WriteLine("Creating SCPEngine with command: " + cmd);
#endif
                SCPEngine scp = new SCPEngine(this, cmd, ssh.OpenSessionChannel());

                try
                {
#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Waiting for response to command");
#endif
                    scp.WaitForResponse();

#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Got response, writing file " + localfile.FullName + " to " + remotefile + "");
#endif
                    scp.WriteFileToRemote(localfile, recursive);

#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Write complete");
#endif
                }
                finally
                {
                    scp.Close();
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }
コード例 #3
0
ファイル: SCPClient.cs プロジェクト: hst-bridge/BBS
        /// <summary>
        /// Transfer the local file or directory to the remote SSH server.
        /// </summary>
        /// <remarks>
        /// When transfering a directory the entire contents are recursively copied to the remote
        /// server. If for example you copy the local directory "C:\scp" and specify the remote
        /// folder "." then a folder called scp will be created on the remote filesystem with all
        /// the contents of the local directory.
        ///
        /// When transfering files, you can either specify the remote directory or an alternative
        /// name and path for the file.
        /// </remarks>
        /// <example>
        /// [This example assumes parameter ssh is an authenticated instance of SSHClient]
        /// <code>
        /// // Create an SCPClient from an authenticated SSHClient instance
        /// SCPClient scp = new SCPClient(ssh);
        ///
        /// // Copy the contents of a local folder to a remote folder
        /// scp.Put(new FileInfo("C:\\workspace"), ".");
        ///
        /// // Copy a file from the local machime to a remote folder
        /// scp.Put(new FileInfo("C:\\workspace\\readme.txt"), ".");
        ///
        /// // Copy a file from the local machine and rename on the remote machine
        /// scp.Put(new FileInfo("C:\\workspace\\readme.txt"), "sometext.txt");
        /// </code>
        /// </example>
        /// <param name="stream">A stream that contains the file data; the stream must report its full length in order to be transfered correctly.</param>
        /// <param name="remotefile">The location to place the copied file or directory</param>
        /// <exception cref="Maverick.SSH.SSHException"/>
        public void Put(Stream stream, String remotefile)
        {
            try
            {
                String cmd = "scp "
                             + "-t "
                             + remotefile;
#if DEBUG
                System.Diagnostics.Trace.WriteLine("Creating SCPEngine with command: " + cmd);
#endif
                SCPEngine scp = new SCPEngine(this, cmd, ssh.OpenSessionChannel());

                try
                {
#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Waiting for response to command");
#endif
                    scp.WaitForResponse();

#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Got response, writing file " + remotefile + " to " + remotefile);
#endif
                    scp.WriteStreamToRemote(stream, stream.Length, remotefile);

#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Write complete");
#endif
                }
                finally
                {
                    scp.Close();
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }
コード例 #4
0
ファイル: SCPClient.cs プロジェクト: hst-bridge/BBS
        /// <summary>
        /// Transfer the remote file or directory to the local machine.
        /// </summary>
        /// <remarks>
        /// When transfering a directory the entire contents are recursively copied from the remote
        /// server. If for example you copy the remote directory "scp" and specify the local
        /// folder "C:\\" then a folder called "C:\\scp" will be created on the local filesystem with all
        /// the contents of the local directory.
        ///
        /// When transfering files, you can either specify the local directory or an alternative
        /// name and path for the file.
        /// </remarks>
        /// <example>
        /// [This example assumes parameter ssh is an authenticated instance of SSHClient]
        /// <code>
        /// // Create an SCPClient from an authenticated SSHClient instance
        /// SCPClient scp = new SCPClient(ssh);
        ///
        /// // Get the contents of a directory to the local users home folder
        /// scp.Get(new FileInfo(SCPClient.GetHomeDirectory()), "docs", true);
        ///
        /// // Get a remote file into a local directory
        /// scp.Get(new FileInfo("C:\\docs"), "docs/readme.txt", false);
        ///
        /// // Get a remote file and rename on the local machine
        /// scp.Get(new FileInfo("C:\\docs\\sometext.txt"), "docs/readme.txt", false);
        /// </code>
        /// </example>
        /// <param name="localFile">The name of the local file or directory to copy to.</param>
        /// <param name="remoteFile">The name of the remote file or directory to copy</param>
        /// <param name="recursive">If the remote file is a directory you should pass a true value, otherwise pass false for a file.</param>
        /// <exception cref="Maverick.SSH.SSHException"/>
        public void Get(FileInfo localFile, String remoteFile, bool recursive)
        {
            try
            {
                String cmd = "scp " + "-f "
                             + (recursive ? "-r " : "")
                             + remoteFile;
#if DEBUG
                System.Diagnostics.Trace.WriteLine("Creating SCPEngine with command: " + cmd);
#endif
                SCPEngine scp = new SCPEngine(this, cmd, ssh.OpenSessionChannel());

                try
                {
#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Reading file(s) from remote into " + localFile.FullName + "");
#endif
                    scp.ReadFromRemote(localFile);
#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Completed transfer into " + localFile.FullName + "");
#endif
                }
                catch (Exception ex)
                {
                    throw ProcessException(ex);
                }
                finally
                {
                    scp.Close();
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }
コード例 #5
0
ファイル: SCPClient.cs プロジェクト: hst-bridge/BBS
 internal SCPStream(long size, Stream source, SCPEngine engine)
 {
     this.size   = size;
     this.source = source;
     this.engine = engine;
 }