Open() public méthode

Opens channel.
Channel has been already opened or already closed. Timeout has occurred while waiting for READY status.
public Open ( ISSHConnection connection, string command, int millisecondsTimeout ) : void
connection ISSHConnection SSH connection object
command string Remote command
millisecondsTimeout int timeout in milliseconds
Résultat void
Exemple #1
0
        /// <summary>
        /// Upload files or directories.
        /// </summary>
        /// <remarks>
        /// <para>Unfortunately, Granados sends a command line in the ASCII encoding.
        /// So the "remotePath" must be an ASCII text.</para>
        /// </remarks>
        /// <param name="localPath">Local path (Windows' path)</param>
        /// <param name="remotePath">Remote path (Unix path)</param>
        /// <param name="recursive">Specifies recursive mode</param>
        /// <param name="preserveTime">Specifies to preserve time of the directory or file.</param>
        /// <param name="cancellation">An object to request the cancellation. Set null if the cancellation is not needed.</param>
        /// <param name="progressDelegate">Delegate to notify progress. Set null if notification is not needed.</param>
        public void Upload(string localPath, string remotePath, bool recursive, bool preserveTime,
                           Cancellation cancellation,
                           SCPFileTransferProgressDelegate progressDelegate)
        {
            if (!IsAscii(remotePath))
            {
                throw new SCPClientException("Remote path must consist of ASCII characters.");
            }

            bool isDirectory = Directory.Exists(localPath);

            if (!File.Exists(localPath) && !isDirectory)
            {
                throw new SCPClientException("File or directory not found: " + localPath);
            }

            if (isDirectory && !recursive)
            {
                throw new SCPClientException("Cannot copy directory in non-recursive mode");
            }

            string absLocalPath = Path.GetFullPath(localPath);

            string command = "scp -t ";

            if (recursive)
            {
                command += "-r ";
            }
            if (preserveTime)
            {
                command += "-p ";
            }
            command += EscapeUnixPath(remotePath);

            using (SCPChannelStream stream = new SCPChannelStream()) {
                stream.Open(_connection, command, _protocolTimeout);
                CheckResponse(stream);

                if (isDirectory)    // implies recursive mode
                {
                    UploadDirectory(absLocalPath, stream, preserveTime, cancellation, progressDelegate);
                }
                else
                {
                    UploadFile(absLocalPath, stream, preserveTime, cancellation, progressDelegate);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Download files or directories.
        /// </summary>
        /// <remarks>
        /// <para>Unfortunately, Granados sends a command line in the ASCII encoding.
        /// So the "remotePath" must be an ASCII text.</para>
        /// </remarks>
        /// <param name="remotePath">Remote path (Unix path)</param>
        /// <param name="localPath">Local path (Windows' path)</param>
        /// <param name="recursive">Specifies recursive mode</param>
        /// <param name="preserveTime">Specifies to preserve time of the directory or file.</param>
        /// <param name="cancellation">An object to request the cancellation. Set null if the cancellation is not needed.</param>
        /// <param name="progressDelegate">Delegate to notify progress. Set null if notification is not needed.</param>
        public void Download(string remotePath, string localPath, bool recursive, bool preserveTime,
                             Cancellation cancellation,
                             SCPFileTransferProgressDelegate progressDelegate)
        {
            if (!IsAscii(remotePath))
            {
                throw new SCPClientException("Remote path must consist of ASCII characters.");
            }

            string absLocalPath = Path.GetFullPath(localPath);

            string command = "scp -f ";

            if (recursive)
            {
                command += "-r ";
            }
            if (preserveTime)
            {
                command += "-p ";
            }
            command += EscapeUnixPath(remotePath);

            string     localBasePath = null; // local directory to store
            SCPModTime modTime       = null;

            Stack <string> localBasePathStack = new Stack <string>();

            using (SCPChannelStream stream = new SCPChannelStream()) {
                stream.Open(_connection, command, _protocolTimeout);
                stream.Write(ZERO);

                while (true)
                {
                    byte[] lineBytes = stream.ReadUntil(LF, _protocolTimeout);
                    if (lineBytes[0] == 1 || lineBytes[0] == 2)
                    {
                        // Warning or Error
                        string message = _encoding.GetString(lineBytes, 1, lineBytes.Length - 2);
                        throw new SCPClientException(message);
                    }

                    if (lineBytes[0] == 0x43 /*'C'*/ || lineBytes[0] == 0x44 /*'D'*/)
                    {
                        SCPEntry entry;
                        try {
                            entry = ParseEntry(lineBytes);
                        }
                        catch (Exception e) {
                            SendError(stream, e.Message);
                            throw;
                        }

                        if (entry.IsDirectory)
                        {
                            string directoryPath = DeterminePathToCreate(localBasePath, absLocalPath, entry);
                            bool   continued     = CreateDirectory(stream, directoryPath, modTime, cancellation, progressDelegate);
                            if (!continued)
                            {
                                break;
                            }
                            modTime = null;
                            localBasePathStack.Push(localBasePath);
                            localBasePath = directoryPath;
                        }
                        else
                        {
                            string filePath  = DeterminePathToCreate(localBasePath, absLocalPath, entry);
                            bool   continued = CreateFile(stream, filePath, entry, modTime, cancellation, progressDelegate);
                            if (!continued)
                            {
                                break;
                            }
                            modTime = null;
                            if (!recursive)
                            {
                                break;
                            }
                        }
                    }
                    else if (lineBytes[0] == 0x54 /*'T'*/)
                    {
                        if (preserveTime)
                        {
                            try {
                                modTime = ParseModTime(lineBytes);
                            }
                            catch (Exception e) {
                                SendError(stream, e.Message);
                                throw;
                            }
                        }
                        stream.Write(ZERO);
                    }
                    else if (lineBytes[0] == 0x45 /*'E'*/)
                    {
                        if (localBasePathStack.Count > 0)
                        {
                            localBasePath = localBasePathStack.Pop();
                            if (localBasePath == null)
                            {
                                break;
                            }
                        }
                        stream.Write(ZERO);
                    }
                    else
                    {
                        SendError(stream, "Invalid control");
                        throw new SCPClientException("Invalid control");
                    }
                }
            }
        }
        /// <summary>
        /// Upload files or directories.
        /// </summary>
        /// <remarks>
        /// <para>Unfortunately, Granados sends a command line in the ASCII encoding.
        /// So the "remotePath" must be an ASCII text.</para>
        /// </remarks>
        /// <param name="localPath">Local path (Windows' path)</param>
        /// <param name="remotePath">Remote path (Unix path)</param>
        /// <param name="recursive">Specifies recursive mode</param>
        /// <param name="preserveTime">Specifies to preserve time of the directory or file.</param>
        /// <param name="cancellation">An object to request the cancellation. Set null if the cancellation is not needed.</param>
        /// <param name="progressDelegate">Delegate to notify progress. Set null if notification is not needed.</param>
        public void Upload(string localPath, string remotePath, bool recursive, bool preserveTime,
                            Cancellation cancellation,
                            SCPFileTransferProgressDelegate progressDelegate)
        {
            if (!IsAscii(remotePath))
                throw new SCPClientException("Remote path must consist of ASCII characters.");

            bool isDirectory = Directory.Exists(localPath);
            if (!File.Exists(localPath) && !isDirectory)
                throw new SCPClientException("File or directory not found: " + localPath);

            if (isDirectory && !recursive)
                throw new SCPClientException("Cannot copy directory in non-recursive mode");

            string absLocalPath = Path.GetFullPath(localPath);

            string command = "scp -t ";
            if (recursive)
                command += "-r ";
            if (preserveTime)
                command += "-p ";
            command += EscapeUnixPath(remotePath);

            using (SCPChannelStream stream = new SCPChannelStream()) {
                stream.Open(_connection, command, _protocolTimeout);
                CheckResponse(stream);

                if (isDirectory) {  // implies recursive mode
                    UploadDirectory(absLocalPath, stream, preserveTime, cancellation, progressDelegate);
                }
                else {
                    UploadFile(absLocalPath, stream, preserveTime, cancellation, progressDelegate);
                }
            }
        }
        /// <summary>
        /// Download files or directories.
        /// </summary>
        /// <remarks>
        /// <para>Unfortunately, Granados sends a command line in the ASCII encoding.
        /// So the "remotePath" must be an ASCII text.</para>
        /// </remarks>
        /// <param name="remotePath">Remote path (Unix path)</param>
        /// <param name="localPath">Local path (Windows' path)</param>
        /// <param name="recursive">Specifies recursive mode</param>
        /// <param name="preserveTime">Specifies to preserve time of the directory or file.</param>
        /// <param name="cancellation">An object to request the cancellation. Set null if the cancellation is not needed.</param>
        /// <param name="progressDelegate">Delegate to notify progress. Set null if notification is not needed.</param>
        public void Download(string remotePath, string localPath, bool recursive, bool preserveTime,
                        Cancellation cancellation,
                        SCPFileTransferProgressDelegate progressDelegate)
        {
            if (!IsAscii(remotePath))
                throw new SCPClientException("Remote path must consist of ASCII characters.");

            string absLocalPath = Path.GetFullPath(localPath);

            string command = "scp -f ";
            if (recursive)
                command += "-r ";
            if (preserveTime)
                command += "-p ";
            command += EscapeUnixPath(remotePath);

            string localBasePath = null;    // local directory to store
            SCPModTime modTime = null;

            Stack<string> localBasePathStack = new Stack<string>();

            using (SCPChannelStream stream = new SCPChannelStream()) {
                stream.Open(_connection, command, _protocolTimeout);
                stream.Write(ZERO);

                while (true) {
                    byte[] lineBytes = stream.ReadUntil(LF, _protocolTimeout);
                    if (lineBytes[0] == 1 || lineBytes[0] == 2) {
                        // Warning or Error
                        string message = _encoding.GetString(lineBytes, 1, lineBytes.Length - 2);
                        throw new SCPClientException(message);
                    }

                    if (lineBytes[0] == 0x43 /*'C'*/ || lineBytes[0] == 0x44 /*'D'*/) {
                        SCPEntry entry;
                        try {
                            entry = ParseEntry(lineBytes);
                        }
                        catch (Exception e) {
                            SendError(stream, e.Message);
                            throw;
                        }

                        if (entry.IsDirectory) {
                            string directoryPath = DeterminePathToCreate(localBasePath, absLocalPath, entry);
                            bool continued = CreateDirectory(stream, directoryPath, modTime, cancellation, progressDelegate);
                            if (!continued)
                                break;
                            modTime = null;
                            localBasePathStack.Push(localBasePath);
                            localBasePath = directoryPath;
                        }
                        else {
                            string filePath = DeterminePathToCreate(localBasePath, absLocalPath, entry);
                            bool continued = CreateFile(stream, filePath, entry, modTime, cancellation, progressDelegate);
                            if (!continued)
                                break;
                            modTime = null;
                            if (!recursive)
                                break;
                        }
                    }
                    else if (lineBytes[0] == 0x54 /*'T'*/) {
                        if (preserveTime) {
                            try {
                                modTime = ParseModTime(lineBytes);
                            }
                            catch (Exception e) {
                                SendError(stream, e.Message);
                                throw;
                            }
                        }
                        stream.Write(ZERO);
                    }
                    else if (lineBytes[0] == 0x45 /*'E'*/) {
                        if (localBasePathStack.Count > 0) {
                            localBasePath = localBasePathStack.Pop();
                            if (localBasePath == null)
                                break;
                        }
                        stream.Write(ZERO);
                    }
                    else {
                        SendError(stream, "Invalid control");
                        throw new SCPClientException("Invalid control");
                    }
                }

            }
        }
 public void Open_AlreadyOpened()
 {
     Assert.Catch <SCPClientInvalidStatusException>(
         () => _stream.Open(null, null, 0)
         );
 }
 public void Open_AlreadyOpened()
 {
     _stream.Open(null, null, 0);
 }