Beispiel #1
2
        private int Download(String HostName, String UserName, String Password, String remoteFilePath, String localPath)
        {
            int result = 0;
            Session session = null;

            // Setup session options               
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Ftp,
                HostName = HostName,
                UserName = UserName,
                Password = Password,                
                //HostName =  "119.59.73.48",
                //UserName = "******",
                //Password =  "******",
                //  SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
            };

           
            try
            {

                using (session = new Session())
                {

                    // Connect    
                    session.ReconnectTime = new TimeSpan(0, 0, 30);
                    session.Open(sessionOptions);

                    // Download files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;
                  //  transferOptions.ResumeSupport.State = TransferResumeSupportState.Smart;
                    //transferOptions.ResumeSupport.Threshold = 1000;
                    

                    TransferOperationResult transferResult = null;
                Retry:
                    try
                    {
                        transferResult = session.GetFiles(remoteFilePath, localPath, false, transferOptions);


                        // Throw on any error
                        transferResult.Check();
                        // Print results
                        foreach (TransferEventArgs transfer in transferResult.Transfers)
                        {
                            Console.WriteLine("Download of {0} succeeded", transfer.FileName);
                        }


                        result = 0;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error: {0}", e);
                        goto Retry;

                        // result = 1;
                    }
                }
               
            }
            finally
            {
                if (session != null)
                {
                    session.Dispose();
                }
            }
            return result;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
            //                FolderBrowserDialog ofd = new FolderBrowserDialog();
            //                ofd.ShowDialog();
                string folderToUpload = @"C:\Users\canpy_000\OneDrive\Documents\GitHub\groupVolumeControl(SPIRE)\Installer\Release\*";

                // Setup session options
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Sftp,
                    HostName = "coderguydev.com",
                    UserName = usr,
                    Password = pw,
                    SshHostKeyFingerprint = "ssh-rsa 2048 0d:3c:74:bb:7f:8b:6b:3c:a0:cb:aa:e3:75:a7:30:52"
                };

                using (Session session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);
                    RemoteDirectoryInfo targetServerDir = session.ListDirectory("/opt/mean/public/VolumeControlUtility/CurrentVersion/");
                    Console.WriteLine(targetServerDir.ToString());
                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    TransferOperationResult transferResult;
                    //transferResult = session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);
                    transferResult = session.PutFiles(folderToUpload, "/opt/mean/public/VolumeControlUtility/CurrentVersion/",
                        false, transferOptions);

                    // Throw on any error
                    transferResult.Check();

                    // Print results
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                    {
                        Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
                    }
                }

            }
            catch (Exception err)
            {
                Console.WriteLine("Error: {0}", err);
            }
        }
Beispiel #3
1
        public List<String> GetRSLList(string winSCPPath, string rslFilePath, string windowsOutput)
        {
            List<String> rslList = null;
            String windowsOutputPath = windowsOutput + @"GFT_DiffTool_Output\";

            if (File.Exists(windowsOutputPath + "rsls.txt"))
            {
                rslList = new List<string>();
                string line;

                try
                {
                    using (var reader = File.OpenText(windowsOutputPath + "rsls.txt"))
                    {
                        rslList = new List<String>();
                        while ((line = reader.ReadLine()) != null)
                        {
                            rslList.Add(line);
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(String.Format("Could not open file: {0}\n\n{1}", windowsOutputPath + "rsls.txt", e.Message));
                }
            }
            else
            {
                using (Session session = new Session())
                {
                    try
                    {
                        session.ExecutablePath = winSCPPath;
                        session.Open(winSCPSessionOptions);
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Could not open a WinSCP session!\n\n" + e.Message);
                    }

                    try
                    {
                        DirectoryInfo dir = new DirectoryInfo(windowsOutputPath);
                        if (!dir.Exists) { dir.Create(); }
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not create the following directory: {0}\n\n{1}",
                                            windowsOutputPath, e.Message));
                    }

                    string rslFile = rslFilePath.Split('/')[rslFilePath.Split('/').Length - 1];

                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;
                    TransferOperationResult transferResult;

                    try
                    {
                        transferResult = session.GetFiles(rslFilePath, windowsOutputPath + rslFile, false, transferOptions);
                        transferResult.Check();
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not transfer the file \"{0}\" in Unix server to the Windows output as {1}\n\n{2}",
                                            rslFilePath, windowsOutputPath + rslFile, e.Message));
                    }

                    try
                    {
                        if (File.Exists(windowsOutputPath + "rsls.txt")) { File.Delete(windowsOutputPath + "rsls.txt"); }
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not delete the following file: {0}\n\n{1}",
                                            windowsOutputPath + "rsls.txt", e.Message));
                    }

                    try
                    {
                        string line;
                        using (var reader = File.OpenText(windowsOutputPath + rslFile))
                        using (var sw = new StreamWriter(windowsOutputPath + "rsls.txt"))
                        {
                            rslList = new List<String>();
                            while ((line = reader.ReadLine()) != null)
                            {
                                line = line.Trim();
                                if (!String.IsNullOrEmpty(line) && !line.StartsWith("//"))
                                {
                                    String rsl = line.Split(' ')[0].Split('\t')[0];
                                    if (rsl.EndsWith(".rsl"))
                                    {
                                        rslList.Add(rsl);
                                        sw.WriteLine(rsl);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not create the following file: {0}\n\n{1}",
                                            windowsOutputPath + "rsls.txt", e.Message));
                    }

                    try
                    {
                        File.Delete(windowsOutputPath + rslFile);
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not delete the following file: {0}\n\n{1}",
                                            windowsOutputPath + rslFile, e.Message));
                    }
                }
            }

            return rslList;
        }
Beispiel #4
1
        public List<String> GetHeaders(string winSCPPath, string csvDirectory, string csvFile, string rslReport,
                                       string parameters, string windowsOutputPath)
        {
            List<String> headerList = null;
            String reportHeaderFileName = rslReport + "-headers.txt";

            using (Session session = new Session())
            {
                try
                {
                    session.ExecutablePath = winSCPPath;
                    session.Open(winSCPSessionOptions);
                }
                catch (Exception e)
                {
                    throw new Exception("Could not open a WinSCP session!\n\n" + e.Message);
                }

                try
                {
                    DirectoryInfo dir = new DirectoryInfo(windowsOutputPath);
                    if (!dir.Exists) { dir.Create(); }
                }
                catch (Exception e)
                {
                    throw new Exception(String.Format("Could not create the following directory: {0}\n\n{1}",
                                        windowsOutputPath, e.Message));
                }

                String newCsvFileName = rslReport + ".csv";

                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;
                TransferOperationResult transferResult;

                try
                {
                    transferResult = session.GetFiles(csvDirectory + csvFile, windowsOutputPath + newCsvFileName, false, transferOptions);
                    transferResult.Check();
                }
                catch (Exception e)
                {
                    throw new Exception(String.Format("Could not transfer the file \"{0}\" in Unix server to the Windows output as {1}\n\n{2}",
                                        csvDirectory + csvFile, windowsOutputPath + newCsvFileName, e.Message));
                }

                try
                {
                    session.ExecuteCommand("rm -f " + csvDirectory + csvFile);
                }
                catch (Exception e)
                {
                    throw new Exception(String.Format("Could not execute de command: rm {0}\n\n{1}",
                                        csvDirectory + csvFile, e.Message));
                }

                if (File.Exists(windowsOutputPath + newCsvFileName))
                {
                    headerList = new List<string>();
                    string line;

                    try
                    {
                        using (var reader = File.OpenText(windowsOutputPath + newCsvFileName))
                        {
                            line = reader.ReadLine();
                            if (line != null)
                            {
                                line = line.Replace("\"", "");
                                headerList = line.Split(',').ToList();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not open file: {0}\n\n{1}", windowsOutputPath + newCsvFileName, e.Message));
                    }

                    try
                    {
                        File.Delete(windowsOutputPath + newCsvFileName);
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not delete the following file: {0}\n\n{1}",
                                        windowsOutputPath + newCsvFileName, e.Message));
                    }
                }

                using (var sw = new StreamWriter(windowsOutputPath + reportHeaderFileName))
                {
                    foreach (string s in headerList) { sw.WriteLine(s); }
                }
            }

            return headerList;
        }
Beispiel #5
1
        /// <summary> 
        /// Downloads audio from the robot  
        /// </summary> 
        /// <param name="sender"> object that called the method </param> 
        /// <param name="e"> any additional arguments </param> 
        void bgWorker_DoWork(object sender, DoWorkEventArgs e) 
        { 
            // Setup session options 
            SessionOptions sessionOptions = new SessionOptions 
            { 
                Protocol = Protocol.Sftp, 
                HostName = ipString, 
                UserName = "******", 
                Password = "******", 
                SshHostKeyFingerprint = "ssh‐rsa 20487c:48:34:e3:c0:7a:92:8e:2f:95:79:0e:12:69:79:e7", 
            }; 
 
            using (Session session = new Session()) 
            { 
                // tell library path to the winSCP executable 
                session.ExecutablePath = System.IO.Directory.GetCurrentDirectory() 
+ "\\winSCPexe.exe"; 
       
                // Connect 
                session.Open(sessionOptions); 
 
                //Set up transfer 
                TransferOptions transferOptions = new TransferOptions(); 
                transferOptions.TransferMode = TransferMode.Binary; 
 
                // generate a file based on date and time 
                string time = Convert.ToString(DateTime.UtcNow.ToFileTime()); 
 
                string destination = "C:\\NAOcam\\" + "NAO_Audio_" + time + ".ogg"; 
 
                // download files 
                TransferOperationResult transferResult; 
                transferResult = session.GetFiles("/home/nao/temp.ogg", 
@destination, true, transferOptions); 
 
                // Throw on any error 
                transferResult.Check(); 
            } 
        } 
Beispiel #6
0
    public static int Main()
    {
        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Scp,
                HostName = ConfigurationManager.AppSettings["hostName"],
                PortNumber = Convert.ToInt32(ConfigurationManager.AppSettings["portNumber"]),
                UserName = ConfigurationManager.AppSettings["userName"],
                Password = ConfigurationManager.AppSettings["storedProcedure"],
                SshHostKeyFingerprint = ConfigurationManager.AppSettings["SshHostKeyFingerprint"],
                SshPrivateKeyPath = ConfigurationManager.AppSettings["SshPrivateKeyPath"],
            };

            using (Session session = new Session())
            {
                string fileName = ConfigurationManager.AppSettings["fileName"];
                string localPath = ConfigurationManager.AppSettings["localPath"];
                string remotePath = ConfigurationManager.AppSettings["remotePath"];

                // Connect
                session.Open(sessionOptions);

                Console.WriteLine("## SCP Host Connection Success");

                string getFrom = localPath + fileName;
                string putToo = remotePath;

                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;

                TransferOperationResult transferResult;
                transferResult = session.PutFiles(getFrom, remotePath, false, transferOptions);

                transferResult.Check();

                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
                    Console.WriteLine("Upload of {0} succeeded", transfer.Destination);
                    Console.WriteLine("Response: ", transfer.Error);
                }

                Console.WriteLine("## SCP Host Transfer Success");
                System.Environment.Exit(0);
            }

            return 1;
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e);
            Console.Read();
            return 1;
        }
    }
Beispiel #7
0
        public int Execute()
        {
            try
            {
                // Setup session options
                String hostname = Params.sftpauth.Split('@')[1];
                String username = Params.sftpauth.Split(':')[0];
                String password = Params.sftpauth.Split('@')[0].Split(':')[1];

                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Sftp,
                    HostName = hostname,
                    UserName = username,
                    Password = password,
                    SshHostKeyFingerprint = Params.fingerprint
                };

                //Console.WriteLine("Host {0} user {1} pw {2}", hostname, username, password);

                using (Session session = new Session())
                {
                    session.Open(sessionOptions);

                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    if (Params.RecordInitialFileList.Equals("true", StringComparison.InvariantCultureIgnoreCase))
                    {
                        RecordInitialFileList(Params.DownloadedFiles +"_" +  System.Guid.NewGuid().ToString() + ".txt", ListFiles(session, Params.RemoteDir));
                        session.Close();
                        return 0;
                    }

                    List<FileObject> newfiles = GetNewFileNames(ReadLocalFileNames(Params.DownloadedFiles), ListFiles(session, Params.RemoteDir));
                    foreach (FileObject item in newfiles)
                    {
                        DownLoadFile(session, transferOptions, Params.RemoteDir + item.Name, Params.LocalDir);
                        AppendToFile(Params.DownloadedFiles, item.Name);
                    }

                    session.Close();
                }

                return 0;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
                Logger.Error(e.Message);
                throw e;

            }
        }
Beispiel #8
0
        public static int Main(string[] args)
        {
            try
            {
                // Setup session options
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Sftp,
                    HostName = ConfigurationManager.AppSettings["HostName"].ToString(),
                    UserName = ConfigurationManager.AppSettings["UserName"].ToString(),
                    Password = ConfigurationManager.AppSettings["Password"].ToString(),
                    SshHostKeyFingerprint = ConfigurationManager.AppSettings["SshHostKeyFingerprint"].ToString()
                };
                string LocalPath = ConfigurationManager.AppSettings["LocalPath"].ToString();
                string RemotePath = ConfigurationManager.AppSettings["RemotePath"].ToString();
                using (Session session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);

                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    TransferOperationResult transferResult;
                    transferResult = session.PutFiles(LocalPath + "*", RemotePath, false, transferOptions);

                    // Throw on any error
                    transferResult.Check();

                    // Print results
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                    {
                        Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
                    }
                }

                return 0;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
                Console.ReadKey();
                return 1;
            }
        }
Beispiel #9
0
    // get a remoteFile and put it localDirectory
    public bool getFile(string remoteFilePath, string localDirectory)
    {
        try
        {
            connectIfNotConnected();
            WinSCP.TransferOptions transferOptions = new WinSCP.TransferOptions();
            transferOptions.TransferMode = WinSCP.TransferMode.Binary;

            WinSCP.TransferOperationResult transferResult;
            transferResult = session.GetFiles(remoteFilePath, localDirectory, false, transferOptions);

            // Throw on any error
            transferResult.Check();
        }
        catch (Exception) { return(false); }
        return(true);
    }
 public void ConstructRemoteFolder(Session session, string remotePath, TransferOptions transferOptions)
 {
     // Construct folder with permissions first
     try
     {
         session.PutFiles(
             DeployConfig.DirLocalPathToUpload + @"\*",
             remotePath,
             !TextCollection.Const.IsToRemoveAfterUpload,
             transferOptions);
     }
     catch (InvalidOperationException)
     {
         if (session.Opened)
         {
             Util.IgnoreException();
         }
         else
         {
             throw;
         }
     }
 }
 public TransferOptions SetupTransferOptions()
 {
     var transferOptions = new TransferOptions { TransferMode = TransferMode.Binary };
     var permissions = new FilePermissions { Octal = "644" };
     transferOptions.FilePermissions = permissions;
     return transferOptions;
 }
        public void UploadLocalFile(Session session, string remotePath, TransferOptions transferOptions)
        {
            ConstructRemoteFolder(session, remotePath, transferOptions);
            var transferResult = session.PutFiles(
                DeployConfig.DirLocalPathToUpload + @"\*",
                remotePath,
                !TextCollection.Const.IsToRemoveAfterUpload,
                transferOptions);

            transferResult.Check();
        }
        private void UploadFile(string fileToUpload)
        {
            lbLogs.Items.Add("File to upload: " + fileToUpload);

              bpLoadFile.Value = 0;
              bpLoadFile.DisplayStyle = ProgressBarDisplayText.Percentage;

              SessionOptions sessionOptions = new SessionOptions
              {
            Protocol = Protocol.Ftp,
            HostName = this.cloudFoldersInfo.data.host,
            UserName = this.cloudFoldersInfo.data.user,
            Password = this.cloudFoldersInfo.data.password,
              };

              using (Session session = new Session())
              {
            // Will continuously report progress of transfer
            session.FileTransferProgress += SessionFileTransferProgress;
            // Connect
            session.Open(sessionOptions);
            // Upload files
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Binary;

            var a = session.ListDirectory("/");
            this.remoteUserFolderName = a.Files[2].Name;
            var filePath = "/" + this.remoteUserFolderName + "/" + filename;
            if (!session.FileExists(filePath))
            {
              TransferOperationResult transferResult;
              transferResult = session.PutFiles(fileToUpload, "/" + this.remoteUserFolderName + "/*.*", false, transferOptions);
              transferResult.Check();
              if (transferResult.IsSuccess)
              {
            bpLoadFile.CustomText = "Succeed!";
              }
              else
              {
            bpLoadFile.CustomText = "Failed!";
              }
            }
            else
            {
              bpLoadFile.CustomText = "Succeed!";
              bpLoadFile.Update();
            }
            lbLogs.Items.Add("Package has been uploaded!");
            bpLoadFile.DisplayStyle = ProgressBarDisplayText.CustomText;
              }
        }
Beispiel #14
0
 private void DoTransmission(Database db,EmailNotifications emailSettings)
 {
     sendAttempts++;
    new Log("FTP transmission for \"" + db.GetAlias() + "\" started.");
     try
     {
         using (Session session = new Session())
         {
             session.Open(sessionOptions);
             TransferOptions transferOptions = new TransferOptions();
             transferOptions.TransferMode = TransferMode.Binary;
             TransferOperationResult transferResult;
             transferResult = session.PutFiles(db.GetFile(), "DB_" + db.GetAlias() + "_bu" + DateTime.Now.ToString("yyyyMMddHHmm") + ".sql", false, transferOptions);
             transferResult.Check();
             new Log("FTP transmission for \"" + db.GetAlias() + "\" completed successfully.");
             session.Close();
             emailSettings.NotifyCheck(db.GetAlias(),EmailNotifications.FTP_OPERATION_OK);
         }
     }
     catch (Exception ex)
     {
         if (sendAttempts < 6)
             DoTransmission(db,emailSettings);
         else
         {
             new Log("ERROR: FTP transmission for \"" + db.GetAlias() + "\" failed.");
             emailSettings.NotifyCheck(db.GetAlias(), EmailNotifications.FTP_OPERATION_FAIL);
         }
     }
 }
Beispiel #15
0
        public TransferOperationResult PutFiles(string localPath, string remotePath, bool remove = false, TransferOptions options = null)
        {
            using (Logger.CreateCallstackAndLock())
            {
                if (options == null)
                {
                    options = new TransferOptions();
                }

                CheckOpened();

                WriteCommand(
                    string.Format(CultureInfo.InvariantCulture,
                        "put {0} {1} -- \"{2}\" \"{3}\"",
                        BooleanSwitch(remove, "delete"), options.ToSwitches(),
                        ArgumentEscape(localPath), ArgumentEscape(remotePath)));

                TransferOperationResult result = new TransferOperationResult();

                using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
                using (RegisterOperationResult(result))
                using (CreateProgressHandler())
                {
                    TransferEventArgs args = null;

                    while (groupReader.Read(0))
                    {
                        if (groupReader.IsNonEmptyElement(TransferEventArgs.UploadTag))
                        {
                            if (args != null)
                            {
                                result.AddTransfer(args);
                                RaiseFileTransferredEvent(args);
                            }
                            args = TransferEventArgs.Read(groupReader);
                        }
                        else if (groupReader.IsNonEmptyElement(ChmodEventArgs.Tag))
                        {
                            if (args == null)
                            {
                                throw new InvalidOperationException();
                            }
                            args.Chmod = ChmodEventArgs.Read(groupReader);
                        }
                        else if (groupReader.IsNonEmptyElement(TouchEventArgs.Tag))
                        {
                            if (args == null)
                            {
                                throw new InvalidOperationException();
                            }
                            args.Touch = TouchEventArgs.Read(groupReader);
                        }
                    }

                    if (args != null)
                    {
                        result.AddTransfer(args);
                        RaiseFileTransferredEvent(args);
                    }
                }

                return result;
            }
        }
        private static void UploadFile(string fileToUpload)
        {
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Ftp,
                HostName = CloudshareManager.cloudFoldersInfo.data.host,
                UserName = CloudshareManager.cloudFoldersInfo.data.user,
                Password = CloudshareManager.cloudFoldersInfo.data.password
            };
            using (Session session = new Session())
            {
                // Connect
                session.DisableVersionCheck = true;
                session.Open(sessionOptions);

                // Upload files
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;

                var a = session.ListDirectory("/");
                CloudshareManager.remoteUserFolderName = a.Files[2].Name;

                TransferOperationResult transferResult;
                transferResult = session.PutFiles(fileToUpload, "/" + a.Files[2].Name + "/*.*", false, transferOptions);

                // Throw on any error
                transferResult.Check();
            }
        }
Beispiel #17
0
        private static void UplaodLoadFile(Session session, TransferOptions transferOptions, String localfile, String remotefolder)
        {
            TransferOperationResult transferResult;
            transferResult = session.PutFiles(localfile, remotefolder, false, transferOptions);

            transferResult.Check();

            foreach (TransferEventArgs transfer in transferResult.Transfers)
            {
                Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
            }
        }
        public string RunFtp(string hostIP, string port, string user, string pass, string remoteDirectory, string remoteFilePattern, string localFileDirectory, string pharmacyName, string localFileName, int numHoursGoback, string ram, ref DataTable tblExcel)
        {
            string result = "";
            string localFileDirWithDateTime;
            string filePrefix = "SFTPDownload";

            try
            {
                localFileDirWithDateTime = Helper.CreateDirectory(localFileDirectory, DateTime.Now, pharmacyName);
            }
            catch (Exception ex)
            {
                LogObj.WriteExcelrow("Failure!", enMsgType.enMsgType_Info, filePrefix,
                                     pharmacyName, "", DateTime.Now, "Failed trying to create a local Dir in " + localFileDirectory + ". Ftp for this pharm stopped.", ref tblExcel);
                return("Failure trying to create a directory in " + localFileDirectory + " for pharmacy " + pharmacyName + ". error:" + ex.Message);
            }

            try
            {//------------------------------------------------------------------------------------------------
                WinSCP.SessionOptions sessionOptions = new WinSCP.SessionOptions
                {
                    Protocol   = WinSCP.Protocol.Sftp,
                    HostName   = hostIP,
                    UserName   = user,
                    Password   = pass,
                    PortNumber = Convert.ToInt32(port)

                                 /* use these to connect to FTP, and leave out the fingerPrint.:
                                  *
                                  * _with1.Protocol = Protocol.Ftp;
                                  * _with1.FtpSecure = FtpSecure.Explicit;
                                  *
                                  */
                };
                string fingerprint = null;
                using (WinSCP.Session session = new WinSCP.Session())
                {
                    fingerprint = session.ScanFingerprint(sessionOptions);
                }
                if (fingerprint == null)
                {
                    throw new Exception("Couldnt determine Host Fingerprint");
                }
                else
                {
                    sessionOptions.SshHostKeyFingerprint = fingerprint;
                }


                using (WinSCP.Session session = new WinSCP.Session())
                {
                    sessionOptions.Timeout = TimeSpan.FromMinutes(6);  // 6 min timeout
                    //session.ExecutablePath = @"C:\Software\v11\winscp.exe";
                    session.Open(sessionOptions);

                    WinSCP.TransferOptions transferOptions = new WinSCP.TransferOptions();
                    transferOptions.TransferMode = WinSCP.TransferMode.Binary;

                    WinSCP.TransferOperationResult transferResult = default(WinSCP.TransferOperationResult);
                    transferResult = session.GetFiles(remoteDirectory + remoteFilePattern, localFileDirectory, false, transferOptions);

                    transferResult.Check();
                    //THrows the first error if not successful
                    foreach (WinSCP.TransferEventArgs transfer in transferResult.Transfers)
                    {
                        string downfileName = transfer.FileName;

                        DateTime remoteWriteTime =
                            session.GetFileInfo(downfileName).LastWriteTime;               //get the DateTime of the file.

                        if (remoteWriteTime >= DateTime.Now.AddHours(numHoursGoback * -1)) // ignore if older that the num Hours Goback spec by user
                        {
                            string localFilename = localFileDirWithDateTime + downfileName;
                            //try
                            //{

                            result += downfileName + "\r\n";
                            LogObj.WriteExcelrow("Success", enMsgType.enMsgType_Info, filePrefix,
                                                 pharmacyName, downfileName, remoteWriteTime, "", ref tblExcel);
                            // }
                            //catch (Exception ex)
                            //{
                            //    LogObj.WriteExcelrow("Failure!", enMsgType.enMsgType_Info, filePrefix,
                            //                                    pharmacyName, downfileName, remoteWriteTime, ex.Message, ref tblExcel);

                            //    return "Failure occurred trying to Download SecureFtpWinSCP file " + downfileName + " for HostIp " + hostIP + " directory " + remoteDirectory + " files " + remoteFilePattern + " error:" + ex.Message + "\r\n";
                            //}
                        }
                        else
                        {
                            result += "Should be Ignored" + downfileName + " but still nowloaded." + "\r\n";

                            LogObj.WriteExcelrow("Success", enMsgType.enMsgType_Info, filePrefix,
                                                 pharmacyName, downfileName, remoteWriteTime, " but still nowloaded.", ref tblExcel);

                            //LogObj.WriteExcelrow("Ignored", enMsgType.enMsgType_Info, filePrefix,
                            //                                    pharmacyName, downfileName, file.Attributes.LastWriteTime, "its too old", ref tblExcel);
                        }
                    }
                }
                //-------------------------------------------------------------------------------------------------------------------


                return("Successfully downloaded to local dir " + localFileDirectory + " the following files: " + result);
            }
            catch (WinSCP.SessionRemoteException ex)
            {
                LogObj.WriteExcelrow("Failure!", enMsgType.enMsgType_Info, filePrefix, pharmacyName, "", DateTime.Now, "Check the remote settings. " + ex.Message, ref tblExcel);
                return("Failure occurred trying to connect to download SFtp for HostIp " + hostIP + " directory " + remoteDirectory + remoteFilePattern + " error:" + ex.Message + "\r\n");
            }

            catch (System.TimeoutException ex)
            {
                LogObj.WriteExcelrow("Failure!", enMsgType.enMsgType_Info, filePrefix, pharmacyName, "", DateTime.Now, ex.Message, ref tblExcel);
                return("Failure occurred trying to connect to download SFtp for HostIp " + hostIP + " directory " + remoteDirectory + remoteFilePattern + " error:" + ex.Message + "\r\n");
            }


            catch (Exception ex)
            {
                LogObj.WriteExcelrow("Failure!", enMsgType.enMsgType_Info, filePrefix, pharmacyName, "", DateTime.Now, ex.Message, ref tblExcel);

                return("Failure occurred trying to connect to download SFtp for HostIp " + hostIP + " directory " + remoteDirectory + remoteFilePattern + " error:" + ex.Message + "\r\n");
            }
        }
        public static void synchro(string typeSynchro)
        {
            try
            {
                string repertoireLocal = FSFLauncherCore.cheminARMA3 + @"\@FSF\";
                string repertoireDistant="";

                fenetrePrincipale.textBox11.Text = "Synchronisation procedure " + typeSynchro + " en cours :" + Environment.NewLine;
                fenetrePrincipale.textBox11.Text += "────────────────────────────" + Environment.NewLine;
                fenetrePrincipale.textBox11.Text += Environment.NewLine + "IMPORTANT : " + Environment.NewLine + "Pour stopper la synchronisation en cours, il vous faut Arreter le processus WinSCP.exe en faisant appel à la combinaison de touche" + Environment.NewLine + " CTRL + MAJ + ESC (onglet processus)." + Environment.NewLine;
                // Setup session options
                SessionOptions sessionOptions = new SessionOptions { };
                switch (typeSynchro)
                {
                    case "beta":
                        SessionOptions sessionOptions1 = new SessionOptions
                        {
                            Protocol = Protocol.Ftp,
                            HostName = constCheminFTP,
                            UserName = constLoginFTP,
                            Password = constMdpFTP

                        };
                        repertoireDistant = "/@FSF/";
                        sessionOptions = sessionOptions1;

                        break;
                    case "officielle":
                        SessionOptions sessionOptions2 = new SessionOptions
                        {
                            Protocol = Protocol.Ftp,
                            HostName = constCheminFTP,
                            UserName = constLoginFTP,
                            Password = constMdpFTP

                        };
                        repertoireDistant = "/@FSF_OFFICIELLE/";
                        sessionOptions = sessionOptions2;
                        break;
                }

                using (Session session = new Session())
                {
                    switch (typeSynchro)
                    {
                        default:
                            Directory.CreateDirectory(repertoireLocal + "@TEMPLATE");
                            Directory.CreateDirectory(repertoireLocal + "@CLIENT");
                            Directory.CreateDirectory(repertoireLocal + "@TEST");
                            Directory.CreateDirectory(repertoireLocal + "@UNITS");
                            Directory.CreateDirectory(repertoireLocal + "@MATERIEL");
                            Directory.CreateDirectory(repertoireLocal + "@ISLANDS");
                            Directory.CreateDirectory(repertoireLocal + "@FRAMEWORK");
                            break;
                    }
                    // Will continuously report progress of synchronization
                    session.FileTransferred += FileTransferred;
                    session.FileTransferProgress += FileTransferProgress;

                    // session log
                    session.DebugLogPath = FSFLauncherCore.cheminARMA3 + @"\userconfig\FSF-LauncherA3\log.txt";
                    // Connect
                    session.Open(sessionOptions);
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    SynchronizationResult synchronizationResult;

                        fenetrePrincipale.textBox11.AppendText(Environment.NewLine + "****   SYNCHRO @TEMPLATE     ******" + Environment.NewLine);
                        synchronizationResult =
                            session.SynchronizeDirectories(
                                SynchronizationMode.Local,
                                repertoireLocal + "@TEMPLATE",
                                repertoireDistant + "@TEMPLATE",
                                true,
                                false,
                                SynchronizationCriteria.Size);
                        effaceProgressBar();

                        fenetrePrincipale.textBox11.AppendText(Environment.NewLine + "****   SYNCHRO @FRAMEWORK     ******" + Environment.NewLine);
                        synchronizationResult =
                            session.SynchronizeDirectories(
                                SynchronizationMode.Local,
                                repertoireLocal + "@FRAMEWORK",
                                repertoireDistant + "@FRAMEWORK",
                                true,
                                false,
                                SynchronizationCriteria.Size);
                        effaceProgressBar();

                        fenetrePrincipale.textBox11.AppendText(Environment.NewLine + "****   SYNCHRO @CLIENT     ******" + Environment.NewLine);
                        synchronizationResult =
                            session.SynchronizeDirectories(
                                SynchronizationMode.Local,
                                repertoireLocal + "@CLIENT",
                                repertoireDistant + "@CLIENT",
                                true,
                                false,
                                SynchronizationCriteria.Size);
                        effaceProgressBar();

                        fenetrePrincipale.textBox11.AppendText(Environment.NewLine + "****   SYNCHRO @TEST     ******" + Environment.NewLine);
                        synchronizationResult =
                            session.SynchronizeDirectories(
                                SynchronizationMode.Local,
                                repertoireLocal + "@TEST",
                                repertoireDistant + "@TEST",
                                true,
                                false,
                                SynchronizationCriteria.Size);
                        effaceProgressBar();

                        fenetrePrincipale.textBox11.AppendText(Environment.NewLine + "****   SYNCHRO @UNITS     ******" + Environment.NewLine);
                        synchronizationResult =
                            session.SynchronizeDirectories(
                                SynchronizationMode.Local,
                                repertoireLocal + "@UNITS",
                                repertoireDistant + "@UNITS",
                                true,
                                false,
                                SynchronizationCriteria.Size);
                        effaceProgressBar();

                        fenetrePrincipale.textBox11.AppendText(Environment.NewLine + "****   SYNCHRO @MATERIEL     ******" + Environment.NewLine);
                        synchronizationResult =
                            session.SynchronizeDirectories(
                                SynchronizationMode.Local,
                                repertoireLocal + "@MATERIEL",
                                repertoireDistant + "@MATERIEL",
                                true,
                                false,
                                SynchronizationCriteria.Size);
                        effaceProgressBar();

                        fenetrePrincipale.textBox11.AppendText(Environment.NewLine + "****   SYNCHRO @ISLANDS     ******" + Environment.NewLine);
                        synchronizationResult =
                            session.SynchronizeDirectories(
                                SynchronizationMode.Local,
                                repertoireLocal + "@ISLANDS",
                                repertoireDistant + "@ISLANDS",
                                true,
                                false,
                                SynchronizationCriteria.Size);
                        effaceProgressBar();

                        // Throw on any error
                        synchronizationResult.Check();
                        fenetrePrincipale.textBox11.AppendText(Environment.NewLine + "->fichier " + repertoireLocal + "Organisation.txt mis a jour." + Environment.NewLine);

                        downloadnouvelleVersion("Organisation.txt", FSFLauncherCore.constCheminFTP + repertoireDistant, FSFLauncherCore.constLoginFTP, FSFLauncherCore.constMdpFTP, repertoireLocal);
                        downloadnouvelleVersion("version.xml", FSFLauncherCore.constCheminFTP + repertoireDistant, FSFLauncherCore.constLoginFTP, FSFLauncherCore.constMdpFTP, repertoireLocal);

                }
            }
            catch (Exception z)
            {
                fenetrePrincipale.textBox11.Text += "Error: " + z;
            }
            fenetrePrincipale.textBox11.AppendText(Environment.NewLine + "_______________" + Environment.NewLine);
            fenetrePrincipale.textBox11.Text += "Fin de la synchro";
            Interface.AlerteVersionArma3();
            Interface.AlerteVersionSynchro();
        }
Beispiel #20
0
        public void DownloadCSV(string winSCPPath, string csvDirectory, string serverType, string version,
                                string csvFile, string report, string portfolio, string windowsOutput)
        {
            using (Session session = new Session())
            {
                try
                {
                    // Connect
                    session.ExecutablePath = winSCPPath;
                    session.Open(winSCPSessionOptions);
                }
                catch (Exception e)
                {
                    throw new Exception("Could not open a WinSCP session!\n\n" + e.Message);
                }

                String rslReport = Path.GetFileNameWithoutExtension(report);
                String windowsOutputPath = windowsOutput + @"GFT_DiffTool_Output\"
                    + DateTime.Today.ToString("yyyy-MM-dd") + "-" + version + @"\" + serverType + @"\";

                try
                {
                    DirectoryInfo dir = new DirectoryInfo(windowsOutputPath);
                    if (!dir.Exists) { dir.Create(); }
                }
                catch (Exception e)
                {
                    throw new Exception(String.Format("Could not create the following directory: {0}\n\n{1}",
                                        windowsOutputPath, e.Message));
                }

                portfolio = portfolio.Replace(' ', '_');
                String newCsvFileName = serverType + "." + portfolio + "." + rslReport + "." + version + ".csv";

                #region Clean Directory

                if (File.Exists(windowsOutputPath + newCsvFileName))
                {
                    try
                    {
                        File.Delete(windowsOutputPath + newCsvFileName);
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not delete the following file: {0}\n\n{1}",
                                        windowsOutputPath + newCsvFileName, e.Message));
                    }
                }

                if (File.Exists(windowsOutputPath + "temp_" + newCsvFileName))
                {
                    try
                    {
                        File.Delete(windowsOutputPath + "temp_" + newCsvFileName);
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not delete the following file: {0}\n\n{21}",
                                        windowsOutputPath + "temp_" + newCsvFileName, e.Message));
                    }
                }

                if (File.Exists(windowsOutputPath + "backup_" + newCsvFileName))
                {
                    try
                    {
                        File.Delete(windowsOutputPath + "backup_" + newCsvFileName);
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not delete the following file: {0}\n\n{1}",
                                        windowsOutputPath + "backup_" + newCsvFileName, e.Message));
                    }
                }

                if (File.Exists(windowsOutputPath + "ERRORS_" + newCsvFileName))
                {
                    try
                    {
                        File.Delete(windowsOutputPath + "ERRORS_" + newCsvFileName);
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not delete the following file: {0}\n\n{21}",
                                        windowsOutputPath + "ERRORS_" + newCsvFileName, e.Message));
                    }
                }

                #endregion

                #region Transfer File

                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;
                TransferOperationResult transferResult;

                try
                {
                    transferResult = session.GetFiles(csvDirectory + csvFile, windowsOutputPath + newCsvFileName, false, transferOptions);
                    transferResult.Check();
                }
                catch (Exception e)
                {
                    throw new Exception(String.Format("Could not transfer the file \"{0}\" in Unix server to the Windows output as {1}\n\n{2}",
                                        csvDirectory + csvFile, windowsOutputPath + newCsvFileName, e.Message));
                }

                try
                {
                    session.ExecuteCommand("rm -f " + csvDirectory + csvFile);
                }
                catch (Exception e)
                {
                    throw new Exception(String.Format("Could not execute de command: rm {0}\n\n{1}",
                                        csvDirectory + csvFile, e.Message));
                }

                #endregion

                if (File.Exists(windowsOutputPath + newCsvFileName))
                {
                    string header;
                    string line = "";
                    int errorCount = 0;
                    try
                    {
                        using (var reader = File.OpenText(windowsOutputPath + newCsvFileName))
                        using (var writer = File.CreateText(windowsOutputPath + "temp_" + newCsvFileName))
                        using (var errorWriter = File.CreateText(windowsOutputPath + "ERRORS_" + newCsvFileName))
                        {
                            header = reader.ReadLine();
                            writer.WriteLine(header);
                            errorWriter.WriteLine("\"Error #\",\"Description\"");
                            while (line != null && (line = reader.ReadLine()) != null)
                            {
                                if (line.Contains("\"Error #\""))
                                {
                                    errorCount++;
                                    while ((line = reader.ReadLine()) != null && !header.Equals(line))
                                    {
                                        errorWriter.WriteLine(line);
                                    }
                                }
                                else
                                {
                                    if (!header.Equals(line) && !line.Trim().Equals("") && line.StartsWith("\""))
                                    {
                                        writer.WriteLine(line);
                                    }
                                }
                            }
                        }
                        File.Replace(windowsOutputPath + "temp_" + newCsvFileName,
                                        windowsOutputPath + newCsvFileName,
                                        windowsOutputPath + "backup_" + newCsvFileName);

                        if (File.Exists(windowsOutputPath + "backup_" + newCsvFileName))
                        {
                            File.Delete(windowsOutputPath + "backup_" + newCsvFileName);
                        }
                        if (File.Exists(windowsOutputPath + "ERRORS_" + newCsvFileName) && errorCount == 0)
                        {
                            File.Delete(windowsOutputPath + "ERRORS_" + newCsvFileName);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception(String.Format("Could not open the file: {0}\n\n{1}", newCsvFileName, e.Message));
                    }
                }
            }
        }
Beispiel #21
0
        /// <summary>
        /// SFTP协议上传文件
        /// </summary>
        public void SFTPUploadFiles()
        {
            bool isRemnant = CheckLocalRemainFiles();

            if (isRemnant)
            {

                try
                {
                    if (!IsConnected)
                        Connect();

                    TransferOptions transferOptionsSFTP = new TransferOptions();
                    transferOptionsSFTP.TransferMode = TransferMode.Binary;
                    transferOptionsSFTP.FilePermissions = null;
                    transferOptionsSFTP.PreserveTimestamp = false;
                    transferOptionsSFTP.ResumeSupport.State = TransferResumeSupportState.Off;

                    //当文件传输时发生
                    SessionFTP.FileTransferred += FileTransCatch;

                    //同步开始传输文件
                    TransferOperationResult remoteResult = SessionFTP.PutFiles(SFTPLocalPath, SFTPRemotePath, false, transferOptionsSFTP);

                    if (remoteResult.IsSuccess)
                    {

                        if (stringFileNames.Count > 0)
                        {

                            BackupSuccessFiles();

                            isRemnant = CheckLocalRemainFiles();

                            if (isRemnant)
                            {
                                if (tryCount <= 5)
                                {
                                    SFTPUploadFiles();
                                }
                            }
                        }
                        else
                        {

                            if (tryCount <= 5)
                            {
                                SFTPUploadFiles();
                            }
                        }

                    }

                    //throw exception if any error.
                    remoteResult.Check();

                }
                catch (Exception ex)
                {
                    //如果有任何传输问题导致传输失败,则每5分钟后重新上传文件,直到上传完成
                    System.Threading.Thread.Sleep(new TimeSpan(IntelvalHour,IntelvalMinute,IntelvalSecond));

                    tryCount++;

                    if (tryCount <= 5)
                    {
                        build.AppendLine("File transferring occurs problem:" + ex.Message + ",System is trying to reupload" + tryCount + "...");
                        SFTPUploadFiles();
                    }

                }
                finally
                {
                    Disconnect();

                    foreach (string strlog in sessionFTP.Output)
                    {
                        build.AppendLine(strlog);
                    }

                    build.AppendLine("End at "+System.DateTime.Now.DayOfWeek.ToString().Substring(0,3)+" " + System.DateTime.Now.ToString("MM/dd/yyyy,hh:mm:ss.ff tt"));

                    WriteLog(build.ToString(), StringLogPath);

                    Environment.Exit(0);
                }
            }
        }
Beispiel #22
0
        private int Upload(String HostName, String UserName, String Password, String remotePath, String localFilePath)
        {
            int result = 0;
            Session session = null;
            try
            {
                // Setup session options               
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Ftp,
                    HostName = HostName,
                    UserName = UserName,
                    Password = Password,
                    //HostName =  "119.59.73.48",
                    //UserName = "******",
                    //Password =  "******",
                    //  SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
                };

                using (session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);



                    // upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Ascii;


                    TransferOperationResult transferResult = null;

                    transferResult = session.PutFiles(localFilePath, remotePath, false, transferOptions);


                    // Throw on any error
                    transferResult.Check();
                    // Print results
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                    {
                        Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
                    }

                }

                result = 0;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
                result = 1;
            }
            finally
            {
                if (session != null)
                {
                    session.Dispose();
                }
            }
            return result;
        }
Beispiel #23
0
        public void SyncRemoteFilesWithLocal(bool _remove = false, TransferOptions _transformOptions = null)
        {
            Stopwatch.Restart();

            m_ignoreTree = new Tree<RemoteFileInfo>(m_remoteTree.Data);

            Tree<RemoteFileInfo> tempIgnoreTree = new Tree<RemoteFileInfo>();

            Sync(m_remoteTree, tempIgnoreTree, RemoteRoot, LocalPath, _remove, _transformOptions);

            LOG.DebugFormat("Synced {0} files in {1} ms", m_syncedFiles, Stopwatch.ElapsedMilliseconds);
        }
Beispiel #24
0
        /// <summary>
        /// SFTPCopy
        /// </summary>
        /// <param name="hostname">Name of host to connect to</param>
        /// <param name="username">Username</param>
        /// <param name="lfile">Local file to be copied</param>
        /// <param name="ec2dir">Remote Directory to copy file to</param>
        /// <returns></returns>
        public string SFTPFileCopy(string hostname, string username, string lfile, string ec2dir)
        {
            string toreturn = "";
            try
            {

                // Setup session options
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = WinSCP.Protocol.Sftp,
                    HostName = hostname,
                    UserName = username,
             //                   Password = password,
              //                  SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
                };

                sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = true;// Since we are pulling these names from AWS, assume they are OK. Avoid prompt.

                using (Session session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);

                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    TransferOperationResult transferResult;
                    transferResult = session.PutFiles(lfile, ec2dir, false, transferOptions);

                    // Throw on any error
                    transferResult.Check();

                    // Print results
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                    {
                        toreturn += "\n Upload to " + hostname + " succeeded";

                    }
                }

                return toreturn;
            }
            catch
            {
                toreturn += "/nFailed copy to: " + hostname + ". Is key loaded in Pageant?";
                return toreturn;
            }
        }
Beispiel #25
0
        public SynchronizationResult SynchronizeDirectories(
            SynchronizationMode mode, string localPath, string remotePath,
            bool removeFiles, bool mirror = false, SynchronizationCriteria criteria = SynchronizationCriteria.Time,
            TransferOptions options = null)
        {
            using (Logger.CreateCallstackAndLock())
            {
                if (options == null)
                {
                    options = new TransferOptions();
                }

                CheckOpened();

                if (removeFiles && (mode == SynchronizationMode.Both))
                {
                    throw new ArgumentException("Cannot delete files in synchronization mode Both");
                }

                if (mirror && (mode == SynchronizationMode.Both))
                {
                    throw new ArgumentException("Cannot mirror files in synchronization mode Both");
                }

                if ((criteria != SynchronizationCriteria.Time) && (mode == SynchronizationMode.Both))
                {
                    throw new ArgumentException("Only Time criteria is allowed in synchronization mode Both");
                }

                string modeName;
                switch (mode)
                {
                    case SynchronizationMode.Local:
                        modeName = "local";
                        break;
                    case SynchronizationMode.Remote:
                        modeName = "remote";
                        break;
                    case SynchronizationMode.Both:
                        modeName = "both";
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("mode");
                }

                List<string> criteriaNames = new List<string>();
                if ((criteria & SynchronizationCriteria.Time) == SynchronizationCriteria.Time)
                {
                    criteria -= SynchronizationCriteria.Time;
                    criteriaNames.Add("time");
                }

                if ((criteria & SynchronizationCriteria.Size) == SynchronizationCriteria.Size)
                {
                    criteria -= SynchronizationCriteria.Size;
                    criteriaNames.Add("size");
                }

                if (criteria != 0)
                {
                    throw new ArgumentOutOfRangeException("criteria");
                }

                WriteCommand(
                    string.Format(CultureInfo.InvariantCulture,
                        "synchronize {0} {1} {2} {3} {4} -- \"{5}\" \"{6}\"",
                        modeName,
                        BooleanSwitch(removeFiles, "delete"),
                        BooleanSwitch(mirror, "mirror"),
                        options.ToSwitches(),
                        FormatSwitch("criteria", string.Join(",", criteriaNames.ToArray())),
                        ArgumentEscape(localPath), ArgumentEscape(remotePath)));

                return ReadSynchronizeDirectories();
            }
        }
Beispiel #26
0
        public TransferOperationResult PutFiles(string localPath, string remotePath, bool remove = false, TransferOptions options = null)
        {
            using (Logger.CreateCallstackAndLock())
            {
                if (options == null)
                {
                    options = new TransferOptions();
                }

                CheckOpened();

                WriteCommand(
                    string.Format(CultureInfo.InvariantCulture,
                        "put {0} {1} -- \"{2}\" \"{3}\"",
                        BooleanSwitch(remove, "delete"), options.ToSwitches(),
                        Tools.ArgumentEscape(localPath), Tools.ArgumentEscape(remotePath)));

                TransferOperationResult result = new TransferOperationResult();

                using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
                using (RegisterOperationResult(result))
                using (CreateProgressHandler())
                {
                    TransferEventArgs args = null;
                    bool mkdir = false;

                    while (groupReader.Read(0))
                    {
                        if (groupReader.IsNonEmptyElement(TransferEventArgs.UploadTag))
                        {
                            AddTransfer(result, args);
                            args = TransferEventArgs.Read(groupReader);
                            mkdir = false;
                        }
                        else if (groupReader.IsNonEmptyElement(TransferEventArgs.MkDirTag))
                        {
                            AddTransfer(result, args);
                            args = null;
                            mkdir = true;
                            // For now, silently ignoring results (even errors)
                            // of mkdir operation, including future chmod/touch
                        }
                        else if (groupReader.IsNonEmptyElement(ChmodEventArgs.Tag))
                        {
                            if (!mkdir)
                            {
                                if (args == null)
                                {
                                    throw new InvalidOperationException();
                                }
                                args.Chmod = ChmodEventArgs.Read(groupReader);
                            }
                        }
                        else if (groupReader.IsNonEmptyElement(TouchEventArgs.Tag))
                        {
                            if (!mkdir)
                            {
                                if (args == null)
                                {
                                    throw new InvalidOperationException();
                                }
                                args.Touch = TouchEventArgs.Read(groupReader);
                            }
                        }
                    }

                    AddTransfer(result, args);
                }

                return result;
            }
        }
Beispiel #27
0
        public void Sync(Tree<RemoteFileInfo> _tree, Tree<RemoteFileInfo> _tempIgnoreTree, string _remotePath, string _localPath, bool _remove = false, TransferOptions _transformOptions = null)
        {
            try
            {
                if (_tree.Children.Count != 0)
                {
                    for (int i = 0; i < _tree.Children.Count; i++)
                    {
                        if (_tree.Children[i] != null)
                        {
                            if (_tree.Children[i].Children != null)
                            {
                                string data = _tree.Children[i].Data.ToString();

                                if (data == null)
                                {
                                    data = string.Empty;
                                }

                                Tree<RemoteFileInfo> node = _tree.Children[i];
                                _tempIgnoreTree.AddNode(node);

                                Console.WriteLine("AT -> {0}", node.Data);

                                Sync(_tree.Children[i], _tempIgnoreTree.Children[_tempIgnoreTree.Children.Count - 1], _remotePath + "/" + data, _localPath, _remove, _transformOptions);
                            }
                        }
                    }
                }
                else
                {
                    if (_tree.Data != null)
                    {
                        string data = _tree.Data.ToString();
                        if (data != null)
                        {
                            string source = _remotePath + "/" + data;
                            if (!TestMode)
                            {
                                GetFiles(source, _localPath, _remove, _transformOptions);
                            }
                            else
                            {
                                //Console.WriteLine("Syncing {0} to {1}", source, _localPath);
                            }

                            Console.WriteLine("-------------DUMP--------------");
                            Tree<RemoteFileInfo>.PrintTree(_tempIgnoreTree, 0);
                            Console.WriteLine("--------------END--------------");

                            m_ignoreTree.AddNode(Tree<RemoteFileInfo>.Clone(_tempIgnoreTree));

                            _tempIgnoreTree = null;
                            _tempIgnoreTree = new Tree<RemoteFileInfo>();

                            m_syncedFiles++;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LOG.ErrorFormat("Error: {0}", e);
            }
        }
Beispiel #28
0
        /// <summary>
        /// Upload file through FTP 
        /// </summary>
        /// <param name="localPath"></param>
        /// <param name="remotePath">Remote path needs to be separate with '/'</param>
        /// <returns></returns>
        public List<string> FtpUpload(string localPath, string remotePath, bool preserveTimestamp = true)
        {
            var uploadedFiles = new List<string>();

            using (var session = new Session())
            {
                // Connect
                session.Open(_sessionOptions);

                // Upload files
                var transferOptions = new TransferOptions
                {
                    TransferMode = TransferMode.Binary,
                    PreserveTimestamp = preserveTimestamp
                };

                TransferOperationResult transferResult = session.PutFiles(localPath, remotePath, false, transferOptions);

                // Throw on any error
                transferResult.Check();

                // Print results
                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    uploadedFiles.Add(transfer.FileName);
                }
            }

            return uploadedFiles;
        }
Beispiel #29
0
        private void GetFiles(string _remotePath, string _localPath, bool _remove = false, TransferOptions _transformOptions = null)
        {
            LOG.DebugFormat("Getting files from {0} >> {1}", _remotePath, _localPath);

            try
            {
                // Download files and throw on any error
                m_session.GetFiles(_remotePath, _localPath, _remove, _transformOptions).Check();
            }
            finally
            {
                // Terminate line after the last file (if any)
                if (m_lastFileName != null)
                {
                    LOG.Info("\nGetFiles done!");
                }
            }
        }
 public void GetFiles(string remotePath, string localPath, bool remove, TransferOptions transferOptions)
 {
     TransferOperationResult transferResult;
     //transferResult = session.GetFiles("/home/user/*", "d:\\download\\", false, transferOptions);
     transferResult = session.GetFiles(remotePath, localPath, remove, transferOptions);
 }
Beispiel #31
0
        public void TransferData()
        {
            bool nhsRun;
            string[] fields;
            SessionOptions sessionOptions = new SessionOptions();

            //get run parameters
            string runId = Framework.GetRunIdFromRunInfoXml(Path.Combine(localRunDir, @"RunInfo.xml"));

            //Parse samplesheet
            SampleSheetHeader sampleSheetHeader = new SampleSheetHeader(Path.Combine(localRunDir, @"SampleSheet.csv"));
            sampleSheetHeader.Populate();

            //write variables to log
            Framework.WriteLog(@"Local run directory: " + localRunDir, 0);
            Framework.WriteLog(@"Run identifier: " + runId, 0);
            Framework.WriteLog(@"Local SampleSheet path: " + Path.Combine(localRunDir, @"SampleSheet.csv"), 0);
            Framework.WriteLog(@"Experiment name: " + sampleSheetHeader.getExperimentName, 0);

            //determine institution
            if (sampleSheetHeader.getInvestigatorName == null || sampleSheetHeader.getInvestigatorName == "" || !sampleSheetHeader.getInvestigatorName.Contains('-'))
            {
                throw new InvalidDataException(@"Investigator name field not provided or malformed.");
            }

            //split investigtor name field
            fields = sampleSheetHeader.getInvestigatorName.Split('-');

            if (fields[1].ToUpper() == @"CU")
            {
                nhsRun = false;
                Framework.WriteLog(@"Investigator name: " + fields[0], 0);
                Framework.WriteLog(@"Institution name: Cardiff University", 0);

                sessionOptions.SshHostKeyFingerprint = config.getWotanSshHostKeyFingerprint;
                sessionOptions.HostName = config.getWotanHostName;
                sessionOptions.UserName = config.getWotanSshUserName;
                sessionOptions.SshPrivateKeyPath = config.getWotanSshPrivateKeyPath;
            }
            else if (fields[1].ToUpper() == @"NHS")
            {
                nhsRun = true;
                Framework.WriteLog(@"Investigator name: " + fields[0], 0);
                Framework.WriteLog(@"Institution name: NHS", 0);

                sessionOptions.SshHostKeyFingerprint = config.getCvxGenSshHostKeyFingerprint;
                sessionOptions.HostName = config.getCvxGenHostName;
                sessionOptions.UserName = config.getCvxGenSshUserName;
                sessionOptions.SshPrivateKeyPath = config.getCvxGenSshPrivateKeyPath;
            }
            else
            {
                throw new InvalidDataException(@"Institute not recognised. CU an NHS are only available options.");
            }

            //convert metrics into table
            Framework.WriteLog(@"Writing SAV table ...", 0);
            string savFilePath = Path.Combine(Path.GetTempPath(), runId + @"_SAV.txt");
            Framework.CallSequenceAnalysisViewer(savFilePath, localRunDir, config.getSavExePath);

            //transfer data to cluster
            if (nhsRun)
            {
                Framework.WriteLog(@"Starting transfer to cvx-gen ...", 0);

                using (Session session = new Session())
                {
                    string remotePath;
                    if (config.getIsMiSeqHost)
                    {
                        remotePath = config.getCvxGenRemoteArchivePath + @"/miseq/" + runId;
                    }
                    else
                    {
                        remotePath = config.getCvxGenRemoteArchivePath + @"/hiseq/" + runId;
                    }

                    session.Open(sessionOptions);
                    Framework.WriteLog(@"Connected!", 0);

                    TransferOperationResult transferResult;
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    //make remote project directory
                    Framework.WriteLog(@"Creating remote directory: " + remotePath, 0);
                    session.CreateDirectory(remotePath);

                    //transfer run folder to archive
                    Framework.WriteLog(@"Tranfer started ...", 0);
                    transferResult = session.PutFiles(localRunDir, remotePath, false, transferOptions);
                    transferResult.Check(); // Throw on any error

                    //transfer SAV file
                    transferResult = session.PutFiles(savFilePath, config.getCvxGenRemoteSAVPath, false, transferOptions);
                    transferResult.Check(); // Throw on any error

                    //execute remote function
                    Framework.WriteLog(@"Execuing post-run command ...", 0);
                    session.ExecuteCommand(@"bash " + config.getCvxGenRemoteScriptPath + ' ' + remotePath + ' ' + config.getCvxGenRemoteResultsPath + '/' + runId + @" >> ~/runPipeline.log");
                }
            }
            else
            {
                //TODO transfer to Wotan
            }

            //write run transfer completion
            using (StreamWriter file = new StreamWriter(Path.Combine(localRunDir, @"TransferComplete.txt")))
            {
                file.WriteLine(@"NGSTransferComplete," + Program.programVersion);
                file.Close();
            }
        }
Beispiel #32
-1
        public static void SyncFile()
        {
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = Param.SqlCeConfig["sshServer"].ToString(),
                UserName = Param.SqlCeConfig["sshUsername"].ToString(),
                SshPrivateKeyPath = Param.SqlCeConfig["sshKeyPath"].ToString(),
                PortNumber = int.Parse(Param.SqlCeConfig["sshPort"].ToString()),
                SshHostKeyFingerprint = Param.SqlCeConfig["sshHostKey"].ToString()
            };

            if (CanConnectInternet())
            {
                using (Session session = new Session())
                {
                    session.Open(sessionOptions);

                    RemoteFileInfo fileInfo = session.GetFileInfo(Param.ScpSoftwarePath + "/Updater.exe");
                    FileInfo fi = new FileInfo("Updater.exe");
                    if (DateTimeToUnixTimestamp(fi.LastWriteTime) != DateTimeToUnixTimestamp(fileInfo.LastWriteTime))
                    {
                        try
                        {
                            TransferOptions transferOptions = new TransferOptions();
                            transferOptions.TransferMode = TransferMode.Automatic;
                            TransferOperationResult transferResult;
                            transferResult = session.GetFiles(Param.ScpSoftwarePath + "/Updater.exe", "Updater.exe", false, transferOptions);
                            transferResult.Check();
                            foreach (TransferEventArgs transfer in transferResult.Transfers)
                            {
                                WriteLog(string.Format("Download of {0} : {1} succeeded", transfer.FileName, transfer.Destination));
                            }
                        }
                        catch (Exception ex)
                        {
                            WriteErrorLog(string.Format("SCP Download Error : {0}", ex.Message));
                        }
                    }

                    //session.FileTransferred += FileTransferred;
                    if (Directory.Exists(Param.ApplicationDataPath + @"\log"))
                    {
                        try
                        {
                            TransferOptions transferOptions = new TransferOptions();
                            transferOptions.TransferMode = TransferMode.Automatic;

                            SynchronizationResult synchronizationResult;
                            synchronizationResult = session.SynchronizeDirectories(SynchronizationMode.Remote,
                                Param.ApplicationDataPath + @"\log", Param.ScpLogPath, false, false, SynchronizationCriteria.Time, transferOptions);
                            synchronizationResult.Check();
                        }
                        catch (Exception ex)
                        {
                            WriteErrorLog(string.Format("SCP SynchronizeDirectories Error : {0}", ex.Message));
                        }
                    }

                    session.Close();
                }
            }

        }