Exemplo n.º 1
0
        internal static string unquote(string _path)
        {
            byte[] path    = StringAux.getBytesUTF8(_path);
            int    pathlen = path.Length;
            int    i       = 0;

            while (i < pathlen)
            {
                if (path[i] == '\\')
                {
                    if (i + 1 == pathlen)
                    {
                        break;
                    }
                    System.Array.Copy(path, i + 1, path, i, path.Length - (i + 1));
                    pathlen--;
                    continue;
                }
                i++;
            }
            if (pathlen == path.Length)
            {
                return(_path);
            }
            byte[] foo = new byte[pathlen];
            System.Array.Copy(path, 0, foo, 0, pathlen);
            return(StringAux.getString(foo));
        }
Exemplo n.º 2
0
        internal static string[] split(string foo, string split)
        {
            byte[] buf = StringAux.getBytes(foo);
            System.Collections.ArrayList bar = new System.Collections.ArrayList();
            int start = 0;
            int index;

            while (true)
            {
                index = foo.IndexOf(split, start);
                if (index >= 0)
                {
                    bar.Add(StringAux.getString(buf, start, index - start));
                    start = index + 1;
                    continue;
                }
                bar.Add(StringAux.getString(buf, start, buf.Length - start));
                break;
            }
            string[] result = new string[bar.Count];
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = (string)(bar[i]);
            }
            return(result);
        }
Exemplo n.º 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;
            }
        }