Example #1
0
        /// <summary>
        /// Copies a file from local machine to a remote SSH machine.
        /// </summary>
        /// <param name="localPath">The local file path.</param>
        /// <param name="remotePath">The path of the remote file.</param>
        public void To(string localPath, string remotePath, bool _recursive)
        {
            SCP_CheckConnectivity();

            AChannel channel = null;
            Stream   server  = null;

            m_cancelled = false;

            try {
                //if we are sending a single file
                if (File.Exists(localPath))
                {
                    SCP_ConnectTo(out channel, out server, remotePath, _recursive);
                    SCP_SendFile(server, localPath, remotePath);
                    channel.disconnect();
                }
                //else, if we are sending a local directory
                else if (Directory.Exists(localPath))
                {
                    if (!_recursive)
                    {
                        throw new SshTransferException(Path.GetFileName("'" + localPath) + "' is a directory, you should use recursive transfer.");
                    }
                    SCP_ConnectTo(out channel, out server, remotePath, true);
                    ToRecursive(server, localPath, remotePath);
                    channel.disconnect();
                }
                else
                {
                    throw new SshTransferException("File not found: " + localPath);
                }
            }
            catch (Exception e) {
                if (Verbos)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
                //SendEndMessage(remoteFile, localFile, filesize,filesize, "Transfer ended with an error.");
                try {
                    channel.disconnect();
                }
                catch {
                }
                throw e;
            }
        }
Example #2
0
        /// <summary>
        /// Creates a directory on the remot server
        /// </summary>
        /// <param name="dir">The new directory</param>
        public override void Mkdir(string dir)
        {
            SCP_CheckConnectivity();

            AChannel channel = null;
            Stream   server  = null;

            m_cancelled = false;

            SCP_ConnectTo(out channel, out server, dir, true);
            SCP_EnterIntoDir(server, dir);
            channel.disconnect();
        }
Example #3
0
        /// <summary>
        /// Copies a file from a remote SSH machine to the local machine using SCP.
        /// </summary>
        /// <param name="remoteFile">The remmote file name</param>
        /// <param name="localPath">The local destination path</param>
        /// <param name="recursive">Value indicating whether a recursive transfer should take place</param>
        public void From(string remoteFile, string localPath, bool _recursive)
        {
            SCP_CheckConnectivity();

            AChannel channel = null;
            Stream   server  = null;

            m_cancelled = false;
            int    filesize = 0;
            String filename = null;
            string cmd      = null;

            try {
                String dir = null;
                if (Directory.Exists(localPath))
                {
                    dir = Path.GetFullPath(localPath);
                }

                SCP_ConnectFrom(out channel, out server, remoteFile, _recursive);

                byte[] buf = new byte[1024];

                // send '\0'
                SCP_SendAck(server);
                int c = SCP_CheckAck(server);

                //parse scp commands
                while ((c == 'D') || (c == 'C') || (c == 'E'))
                {
                    if (m_cancelled)
                    {
                        break;
                    }

                    cmd = "" + (char)c;
                    if (c == 'E')
                    {
                        c   = SCP_CheckAck(server);
                        dir = Path.GetDirectoryName(dir);
                        if (Verbos)
                        {
                            Console.WriteLine("E");
                        }
                        //send '\0'
                        SCP_SendAck(server);
                        c = (char)SCP_CheckAck(server);
                        continue;
                    }

                    // read '0644 ' or '0755 '
                    server.Read(buf, 0, 5);
                    for (int i = 0; i < 5; i++)
                    {
                        cmd += (char)buf[i];
                    }

                    //reading file size
                    filesize = 0;
                    while (true)
                    {
                        server.Read(buf, 0, 1);
                        if (buf[0] == ' ')
                        {
                            break;
                        }
                        filesize = filesize * 10 + (buf[0] - '0');
                    }

                    //reading file name
                    for (int i = 0; ; i++)
                    {
                        server.Read(buf, i, 1);
                        if (buf[i] == (byte)0x0a)
                        {
                            filename = StringAux.getString(buf, 0, i);
                            break;
                        }
                    }
                    cmd += " " + filesize + " " + filename;
                    // send '\0'
                    SCP_SendAck(server);

                    //Receive file
                    if (c == 'C')
                    {
                        if (Verbos)
                        {
                            Console.WriteLine("Sending file modes: " + cmd);
                        }
                        SCP_ReceiveFile(server, remoteFile,
                                        dir == null ? localPath : dir + "/" + filename,
                                        filesize);

                        if (m_cancelled)
                        {
                            break;
                        }

                        // send '\0'
                        SCP_SendAck(server);
                    }
                    //Enter directory
                    else if (c == 'D')
                    {
                        if (dir == null)
                        {
                            if (File.Exists(localPath))
                            {
                                throw new SshTransferException("'" + localPath + "' is not a directory");
                            }
                            dir = localPath;
                            Directory.CreateDirectory(dir);
                        }
                        if (Verbos)
                        {
                            Console.WriteLine("Entering directory: " + cmd);
                        }
                        dir += "/" + filename;
                        Directory.CreateDirectory(dir);
                    }

                    c = SCP_CheckAck(server);
                }
                channel.disconnect();
            }
            catch (Exception e) {
                if (Verbos)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
                try {
                    channel.disconnect();
                }
                catch {
                }
                throw e;
            }
        }