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;
        }
Beispiel #2
0
        private void SFTPDownload(SessionOptions so, string OPFolderName, List <string> IPDir, List <string> OPdir)
        {
            try
            {
                session = new WinSCP.Session();
                session.Open(so);
                for (int i = 0; i < IPDir.Count; i++)
                {
                    string localPath = $"{OPFolderName}{OPdir[i]}";
                    localPath = DeserialiseXMLFromStr(localPath);
                    Directory.CreateDirectory(localPath);
                    string remotePath = IPDir[i];
                    remotePath = DeserialiseXMLFromStr(remotePath);
                    // Enumerate files and directories to download
                    IEnumerable <RemoteFileInfo> fileInfos = session.EnumerateRemoteFiles(remotePath, null, EnumerationOptions.EnumerateDirectories | EnumerationOptions.AllDirectories);

                    foreach (RemoteFileInfo fileInfo in fileInfos)
                    {
                        string localFilePath = session.TranslateRemotePathToLocal(fileInfo.FullName, remotePath, localPath);

                        if (fileInfo.IsDirectory)
                        {
                            // Create local subdirectory, if it does not exist yet
                            Directory.CreateDirectory(localFilePath);
                        }
                        else
                        {
                            WriteToLogBox($"Downloading file {fileInfo.FullName}...");
                            // Download file
                            string remoteFilePath = session.EscapeFileMask(fileInfo.FullName);
                            TransferOperationResult transferResult = session.GetFiles(remoteFilePath, localFilePath);
                            if (!transferResult.IsSuccess)
                            {
                                WriteToLogBox($"Error downloading file {fileInfo.FullName}: {transferResult.Failures[0].Message}");
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                WriteToLogBox($"Error: {e}");
            }
            finally
            {
                session.Close();
                session.Dispose();
                GC.Collect();
            }
        }
Beispiel #3
0
        private ArrayList ListFiles(String HostName, String UserName, String Password, String remotePath)
        {
            //int result = 0;
            Session session = null;
            ArrayList fileNameList = new ArrayList();
            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);



                    RemoteDirectoryInfo dirInfo = session.ListDirectory(remotePath);

                    foreach (RemoteFileInfo file in dirInfo.Files)
                    {

                        if (!file.IsDirectory)
                        {
                            fileNameList.Add(file.Name);
                        }
                        // Console.WriteLine("File Name: {0},LastWriteTime: {1},IsDirectory: {2},File Length: {3},", file.Name, file.LastWriteTime, file.IsDirectory, file.Length);
                    }

                }

                // return 0;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
                //  return 1;
            }
            finally
            {
                if (session != null)
                {
                    session.Dispose();
                }
            }

            return fileNameList;
        }
Beispiel #4
0
        public int removeFTPFiles(String HostName, String UserName, String Password, String remoteFilePath)
        {
            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);

                    // Remove files                 

                    RemovalOperationResult removalResult = null;
                    removalResult = session.RemoveFiles(remoteFilePath);

                    // Throw on any error
                    removalResult.Check();
                    // Print results
                    foreach (RemovalEventArgs removal in removalResult.Removals)
                    {
                        Console.WriteLine("Remove of {0} succeeded", removal.FileName);
                    }

                }

                result = 0;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
                result = 1;
            }
            finally
            {
                if (session != null)
                {
                    session.Dispose();
                }
            }
            return result;
        }
Beispiel #5
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 #6
0
        // End of bExportDataToXML
        // Post data via sftp
        //
        public static Boolean bsFtp(String sFtpSite, String sUserName, String sPassword, String sSshHostKeyFingerprint, String sDatabaseLocation, Boolean bSearchAll = false, Boolean bDebug = false, Boolean bExposeCredentials = false)
        {
            clsLog clsLog_ = new clsLog(msLogFile, msVersionData);
            clsError clsError_ = new clsError();

               // String sAuditRootUCase = "";
               // String sTRAMSFtpAuditRootDir = "";
            // String sTRAMSFtpRootDir = "";
            SynchronizationResult synchronizationResult;

            try
            {

                clsLog_.mLog(Constants.gcInfo, "Posting data to sftp site ...");

                byte[] data = Convert.FromBase64String(sPassword);
                String decodedSitePassword = Encoding.UTF8.GetString(data);

                //clsLog_.mLog(Constants.gcInfo, "");
               // clsLog_.mLog(Constants.gcInfo, "onelan.bsFtp, sftp (winscp) process starting **************");
                if (bDebug) { clsLog_.mLog(Constants.gcInfo, sIndent +"onelan.bsFtp, sftpSite = " + sFtpSite); }
                if (bDebug) { clsLog_.mLog(Constants.gcInfo, sIndent + "onelan.bsFtp, sUserName = "******"************ Credentials *****************");
                    clsLog_.mLog(Constants.gcInfo, sIndent +"onelan.bsFtp, sPassword = "******"onelan.bsFtp, sSshHostKeyFingerprint = " + sSshHostKeyFingerprint);
                    clsLog_.mLog(Constants.gcInfo, "");
                }
                if (bDebug) { clsLog_.mLog(Constants.gcInfo, sIndent + "onelan.bsFtp, local root folder = " + sDatabaseLocation); }
               // clsLog_.mLog(Constants.gcInfo, "onelan.bsFtp, SearchAll = " + bSearchAll);
               // clsLog_.mLog(Constants.gcInfo, "onelan.bsFtp, Debug = " + bDebug);

                // Setup session options
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Sftp,
                    HostName = sFtpSite,
                    UserName = sUserName,
                    Password = decodedSitePassword,
                    SshHostKeyFingerprint = sSshHostKeyFingerprint

                };

                using (Session session = new Session())
                {
                    // Connect
                    if (bDebug) { clsLog_.mLog(Constants.gcInfo, sIndent + "onelan.bsFtp, connecting ..."); }
                    session.Open(sessionOptions);

                    // Upload files
                    if (bDebug) { clsLog_.mLog(Constants.gcInfo, sIndent + "onelan.bsFtp, uploading ..."); }

                    // Get list of directories to transfer

                    // Will continuously report progress of synchronization
                   // if (bDebug) { session.FileTransferred += FileTransferred; }

                    if (bDebug) { clsLog_.mLog(Constants.gcInfo, sIndent + "onelan.bsFtp, Processing sql files ..."); }
                    // Synchronise the xml directory
                    synchronizationResult = session.SynchronizeDirectories(SynchronizationMode.Remote, sDatabaseLocation + "\\data", "onelan/data", true);
                    synchronizationResult.Check();

                    if (bDebug) { clsLog_.mLog(Constants.gcInfo, sIndent + "onelan.bsFtp, Posting images ..."); }
                    synchronizationResult = session.SynchronizeDirectories(SynchronizationMode.Remote, sDatabaseLocation + "\\images", "onelan/images", false);
                    synchronizationResult.Check();

                    // Clean up
                    session.Dispose();
                }

               // clsLog_.mLog(Constants.gcInfo, "********** Completed sftp processing **********");
                return true;
            }
            catch (Exception e)
            {
                clsError_.mLogError("Problem running sftpTransfer", "onelan", "bsFtp", e, msVersionData, msLogFile, false);
                return false;
            }

            finally
            {

            }

            // End of Post data via sftp
            //
        }