Example #1
0
        private bool DownloadFile(string local_path_folder, string remote_path_file, string local_file_name = null, string remote_filename = null, bool bLog = true)
        {
            //LinuxTreeViewItem.ReconnectServer();
            //LinuxTreeViewItem.ReConnect();
            if (!IsConnected)
            {
                return(false);
            }

            try
            {
                string local;
                if (local_file_name != null)
                {
                    local = local_path_folder + "\\" + local_file_name;
                }
                else
                {
                    if (remote_filename == null)
                    {
                        string[] split = remote_path_file.Split('/');
                        remote_filename = split[split.Length - 1];
                    }
                    local = local_path_folder + "\\" + remote_filename;
                }

                if (!FileContoller.CreateDirectory(local_path_folder))
                {
                    return(false);
                }

                if (sftp.Exists(remote_path_file))
                {
                    FileStream fs = new FileStream(local, FileMode.Create);
                    sftp.DownloadFile(remote_path_file, fs);
                    Log.PrintLog(remote_path_file + " => " + local, "Classes.SSHManager.DownloadFile");
                    fs.Close();
                }
                else
                {
                    Log.PrintLog("Not found file " + remote_path_file, "Classes.SSHManager.DownloadFile");
                    return(false);
                }
            }
            catch (Exception e)
            {
                if (bLog)
                {
                    Log.ErrorIntoUI(e.Message, "downloadFile", Status.current.richTextBox_status);
                }
                Log.PrintError(e.Message, "Classes.SSHManager.DownloadFile");
                return(false);
            }
            return(true);
        }
Example #2
0
        private bool DownloadDirectory(string local_folder_path, string remote_directory_path, Regex filter_file = null, Regex filter_except_dir = null)
        {
            //LinuxTreeViewItem.ReconnectServer();
            //LinuxTreeViewItem.ReConnect();
            if (!IsConnected)
            {
                return(false);
            }

            try
            {
                if (!FileContoller.CreateDirectory(local_folder_path))
                {
                    return(false);
                }

                SftpFile[] files = PullListInDirectory(remote_directory_path);
                if (files == null)
                {
                    return(false);
                }
                for (int i = 0; i < files.Length; i++)
                {
                    if (files[i].Name == "." || files[i].Name == "..")
                    {
                        continue;
                    }

                    if (files[i].IsDirectory &&
                        (filter_except_dir == null || !filter_except_dir.IsMatch(files[i].Name)))
                    {
                        string re_local_folder_path = local_folder_path + files[i].Name + @"\";
                        DownloadDirectory(re_local_folder_path, files[i].FullName, filter_file, filter_except_dir);
                        continue;
                    }

                    if (filter_file != null && !filter_file.IsMatch(files[i].Name))
                    {
                        continue;
                    }

                    DownloadFile(local_folder_path, files[i].FullName, files[i].Name, files[i].Name);
                }
            }
            catch (Exception e)
            {
                Log.ErrorIntoUI(e.Message, "DownloadDirectory", Status.current.richTextBox_status);
                Log.PrintError(e.Message, "Classes.SSHManager.DownloadDirectory");
                return(false);
            }
            return(true);
        }
Example #3
0
        public static bool Write(string path, string str)
        {
            if (path == null || str == null)
            {
                return(false);
            }
            try
            {
                // 경로에 디렉토리가 없으면 생성
                string dir = path;
                if (path[path.Length - 1] != '\\')
                {
                    dir = path.Substring(0, path.LastIndexOf('\\') + 1);
                }
                if (!FileContoller.CreateDirectory(dir))
                {
                    return(false);
                }

                // 경로에 파일 쓰기.
                FileStream fs = new FileStream(path, FileMode.Create);

                byte[] buffer /* = new byte[MAX_BUFFER]*/;
                int    size_write = 0;

                buffer     = Encoding.UTF8.GetBytes(str);
                size_write = buffer.Length;

                fs.Write(buffer, 0, size_write);

                fs.Close();
                return(true);
            }
            catch (Exception e)
            {
                Log.PrintError(e.Message + "<path = " + path + ">", "Classes.FileContoller.Write");
            }
            return(false);
        }
Example #4
0
        private string UploadFile(string local_path, string remote_directory, string remote_backup_dir = null)
        {
            //LinuxTreeViewItem.ReconnectServer();
            //LinuxTreeViewItem.ReConnect();
            if (!IsConnected)
            {
                return(null);
            }


            string remote_file_path = null;

            try
            {
                FileInfo fi = new FileInfo(local_path);
                if (fi.Exists)
                {
                    //FileStream fs = File.Open(local_path, FileMode.Open, FileAccess.Read);
                    remote_file_path = remote_directory + "/" + fi.Name;
                    //if(isOverride)
                    //{
                    //	sftp.UploadFile(fs, remote_file_path);
                    //	Log.PrintConsole(fi.Name + " => " + remote_file_path, "upload file"/*, test4.m_wnd.richTextBox_status*/);
                    //}

                    if (CreateRemoteDirectory(remote_directory))
                    {
                        if (remote_backup_dir != null && sftp.Exists(remote_file_path))
                        {
                            if (CreateRemoteDirectory(remote_backup_dir))
                            {
                                DateTime dt;
                                //dt = DateTime.Now;

                                // 원래는 서버시간으로 생성해야함.
                                // 서버마다 시간을 알수있는 함수가 다를수 있으므로 sftp를 사용
                                // 위 if 문의 sftp.Exists(remote_file_path) 에서 엑세스한 시간을 가져옴.
                                dt = sftp.GetLastAccessTime(remote_file_path);

                                // '파일 명'.'연도'.'달'.'날짜'.'시간'.'분'.'초'.backup 형식으로 백업파일 생성
                                string remote_backup_file = remote_backup_dir + fi.Name + dt.ToString(".yyyy.MM.dd.hh.mm.ss") + ".backup";
                                string com = @"cp '" + remote_file_path + "' '" + remote_backup_file + "'";
                                ssh.RunCommand(com);
                                //SendCommand(com);
                            }
                            else
                            {
                                //fs.Close();
                                Log.PrintError("Create Directory Error", "Classes.SSHManager.UploadFile");
                                return(null);
                            }
                        }

                        //sftp.UploadFile(fs, remote_file_path, true);
                        string str  = FileContoller.Read(local_path);
                        string str1 = "echo \"" + str.Replace("\r", "").Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("$", "\\$") + "\" > \"" + remote_file_path + "\"";
                        ssh.RunCommand(str1);
                        //SendCommand(str1);

                        Log.PrintLog(fi.Name + " => " + remote_file_path, "Classes.SSHManager.UploadFile");
                    }
                    else
                    {
                        remote_file_path = null;
                    }
                    //fs.Close();
                }
                else
                {
                    Log.ErrorIntoUI("Not Exist File [" + local_path + "]", "upload file", Status.current.richTextBox_status);
                    Log.PrintError("Not Exist File [" + local_path + "]", "Classes.SSHManager.UploadFile");
                    return(null);
                }
            }
            catch (Exception e)
            {
                Log.ErrorIntoUI(e.Message + "/ " + local_path + " -> " + remote_directory, "UploadFile", Status.current.richTextBox_status);
                Log.PrintError(e.Message + "/ " + local_path + " -> " + remote_directory, "Classes.SSHManager.UploadFile");
                return(null);
            }
            return(remote_file_path);
        }