Ejemplo n.º 1
0
        private SessionOptions GetSessionOptions()
        {
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol                = Protocol.Sftp,
                HostName                = this.ServerAddress,
                UserName                = this.UserName,
                Password                = this.Password,
                SshPrivateKeyPath       = this.PrivateKeyFilePath,
                SshPrivateKeyPassphrase = this.PrivateKeyPassword,
                GiveUpSecurityAndAcceptAnySshHostKey = this.AcceptAnySSHServerHostKey,
                SshHostKeyFingerprint = this.SSHServerHostKey
            };

            if (this.EncryptCipher.Equals(EncryptionCipher.Auto))
            {
                sessionOptions.AddRawSettings("Cipher", DefaultEncryptionCipher);
            }
            else if (this.EncryptCipher.Equals(EncryptionCipher.TripleDes))
            {
                sessionOptions.AddRawSettings("Cipher", "3des,WARN");
            }
            else
            {
                sessionOptions.AddRawSettings("Cipher", this.EncryptCipher.ToString().ToLower() + ",WARN");
            }

            return(sessionOptions);
        }
Ejemplo n.º 2
0
        public static string DownloadInstituteMappingFileFromSftpServerIfNewer(string userName, string password, bool isTest, string localSaveDirPath, DateTime?downloadedRemoteFileLastWriteDate, out DateTime?remoteFileLastWriteDate, Uri proxyUri = null, string proxyUserName = null, string proxyPassword = null)
        {
            remoteFileLastWriteDate = null;
            var remoteFileName = InstituteMappingTestFileName;

            if (!isTest)
            {
                remoteFileName = InstituteMappingProdFileName;
            }

            var localSaveFilePath = Path.Combine(localSaveDirPath, remoteFileName);

            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = SftpSchufaFileServerHostUrl,
                UserName = userName,
                Password = password,
                GiveUpSecurityAndAcceptAnySshHostKey = true
            };

            if (proxyUri != null && !String.IsNullOrWhiteSpace(proxyUserName) && !String.IsNullOrWhiteSpace(proxyPassword))
            {
                //3 is Http Proxy Method s. https://winscp.net/eng/docs/rawsettings
                sessionOptions.AddRawSettings("ProxyMethod", "3");
                sessionOptions.AddRawSettings("ProxyHost", proxyUri.Host);
                sessionOptions.AddRawSettings("ProxyPort", proxyUri.Port.ToString());
                sessionOptions.AddRawSettings("ProxyUsername", proxyUserName);
                sessionOptions.AddRawSettings("ProxyPassword", proxyPassword);
            }

            using (Session session = new Session())
            {
                // Connect - Open throws an internal exception which can be ignored
                session.Open(sessionOptions);
                var remoteFilePath = "/" + remoteFileName;

                if (session.FileExists(remoteFilePath))
                {
                    remoteFileLastWriteDate = session.GetFileInfo(remoteFilePath).LastWriteTime.Date;
                }

                if (!downloadedRemoteFileLastWriteDate.HasValue || remoteFileLastWriteDate.HasValue && remoteFileLastWriteDate.Value > downloadedRemoteFileLastWriteDate)
                {
                    session.GetFiles(remoteFilePath, localSaveFilePath).Check();
                }
            }

            return(localSaveFilePath);
        }
Ejemplo n.º 3
0
        public override void Configure(IConfigSectionNode node)
        {
            m_Options = new SessionOptions();

            var serverUrlAttr = node.AttrByName(CONFIG_SERVER_URL_ATTR);

            if (serverUrlAttr.Exists)
            {
                var serverUrl = serverUrlAttr.ValueAsString();
                if (serverUrl.IsNotNullOrWhiteSpace())
                {
                    m_Options.ParseUrl(serverUrl);
                }
            }

            var protocolAttr = node.AttrByName(CONFIG_PROTOCOL_ATTR);

            if (protocolAttr.Exists)
            {
                m_Options.Protocol = protocolAttr.ValueAsEnum(Protocol.Sftp);
            }

            base.Configure(node);

            var rawSettings = node[CONFIG_RAW_SETTINGS_SECTION];

            if (rawSettings.Exists)
            {
                foreach (var attr in rawSettings.Attributes)
                {
                    m_Options.AddRawSettings(attr.Name, attr.ValueAsString());
                }
            }
        }
Ejemplo n.º 4
0
        public void SynchronizeDirectories(SessionOptions options,
                                           string remoteFolder,
                                           string localFolder,
                                           bool removeFiles         = false,
                                           SynchronizationMode mode = SynchronizationMode.Remote,
                                           bool mirror = false,
                                           SynchronizationCriteria criteria = SynchronizationCriteria.Time,
                                           TransferOptions transferOptions  = null)
        {
            options.AddRawSettings("LocalDirectory", _context.Environment.WorkingDirectory.FullPath);

            using (var session = new Session())
            {
                session.FileTransferred += OnFileTransferred;
                session.Open(options);
                if (!session.FileExists(remoteFolder))
                {
                    session.CreateDirectory(remoteFolder);
                }
                var result = session.SynchronizeDirectories(mode, localFolder, remoteFolder, removeFiles, mirror, criteria, transferOptions);
                result.Check();

                Logger.Log($"{localFolder} and {remoteFolder} were synchronized");
            }
        }
Ejemplo n.º 5
0
        private void timer3_Tick(object sender, EventArgs e)
        {
            List <DateClass> dateList = new List <DateClass>();
            var uri = new Uri("ftp://" + confList["Server"]);
            /* Create Object Instance */
            ftp ftpClient = new ftp(uri.ToString(), confList["Username"], confList["Password"]);

            var dirStr = ftpClient.directoryListDetailed(confList["Fpath"]);

            if (dirStr.Count() > 3)
            {
                foreach (var item in dirStr)
                {
                    if (item != "")
                    {
                        var cdate = creationDate(item);
                        dateList.Add(cdate);
                    }
                }

                dateList.Sort(delegate(DateClass x, DateClass y)
                {
                    return(y.FullDate.CompareTo(x.FullDate));
                });

                //Using WinSCP for deleting directories
                string bareServer = confList["Server"].Split(':')[0];

                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Ftp,
                    HostName = bareServer,
                    UserName = confList["Username"],
                    Password = confList["Password"],
                };
                sessionOptions.AddRawSettings("ProxyPort", "0");
                int sizeKeep = 1;
                sizeKeep = Convert.ToInt32(confList["Keep"]);

                for (int i = sizeKeep; i < dateList.Count; i++)
                {
                    using (Session session = new Session())
                    {
                        // Connect
                        session.Open(sessionOptions);

                        // Delete folder
                        session.RemoveFiles(confList["Fpath"] + "/" + dateList[i].Name).Check();
                    }
                    logs += "Directory:--" + dateList[i].Name + "-- was removed" + Environment.NewLine;
                    saveLog(logs);
                    textBox8.Text += logs;
                    logs           = "";
                }

                updateTreeView();
            }
        }
Ejemplo n.º 6
0
        private static void ResetServersWork(SessionOptions sessionOptions, string sshPrivateKeyPath)
        {
            Session session = new Session();

            try
            {
                sessionOptions.SshPrivateKeyPath = sshPrivateKeyPath;
                sessionOptions.AddRawSettings("AuthGSSAPI", "1");
                sessionOptions.AddRawSettings("TcpNoDelay", "1");

                session.ExecutablePath      = Path.Combine(_applicationDataPath, _winSCPExePath);
                session.DisableVersionCheck = true;

                // Connect
                EditorUtility.DisplayProgressBar("RESETTING", "Opening session with: " + sessionOptions.HostName, IncreaseProgress(0.05f)); // 0.05
                session.Open(sessionOptions);

                // Terminate active session
                var killCmdText = "screen -S server -X quit";
                EditorUtility.DisplayProgressBar("RESETTING", "Terminating previous server screen with command: " + killCmdText, IncreaseProgress(0.15f)); // 0.2
                ExecuteCommand(session, killCmdText, "Terminate screen command.", "Terminated active sessions successfully.");

                // Start screen
                var newScreenCmd = "screen -S server -dm";
                EditorUtility.DisplayProgressBar("RESETTING", "Starting new server screen with command:" + newScreenCmd, IncreaseProgress(0.4f)); // 0.9
                ExecuteCommand(session, newScreenCmd, "Start screen command error", "Start screen command successful");

                // Run server in screen
                var filepath       = GetServerFilePath();
                var filename       = Path.GetFileName(filepath);
                var startServerCmd = string.Concat("screen -S server -p 0 -X stuff './", filename, " -batchmode -nographics -headlessmode\n'");
                EditorUtility.DisplayProgressBar("RESETTING", "Starting server in new screen with command: " + startServerCmd, IncreaseProgress(0.05f)); // 0.95
                ExecuteCommand(session, startServerCmd, "Start server command error", "Start server command succesful");
            }
            catch (Exception e)
            {
                Debug.LogError("DEPLOY:: WINSCP Session (" + sessionOptions.HostName + ") exception: " + e.ToString());
            }
            finally
            {
                session.Dispose();
            }
        }
Ejemplo n.º 7
0
        public IEnumerable <RemoteFileInfo> GetFileList(SessionOptions options, string remoteFolder)
        {
            options.AddRawSettings("LocalDirectory", _context.Environment.WorkingDirectory.FullPath);

            using (var session = new Session())
            {
                session.Open(options);
                var result = session.ListDirectory(remoteFolder);

                Logger.Log($"{remoteFolder} list was queried");
                return(result.Files);
            }
        }
Ejemplo n.º 8
0
        public IEnumerable <ComparisonDifference> CompareDirectories(SessionOptions options,
                                                                     string remoteFolder,
                                                                     string localFolder,
                                                                     bool logDifferences      = false,
                                                                     bool removeFiles         = false,
                                                                     SynchronizationMode mode = SynchronizationMode.Remote,
                                                                     bool mirror = false,
                                                                     SynchronizationCriteria criteria = SynchronizationCriteria.Time,
                                                                     TransferOptions transferOptions  = null)
        {
            options.AddRawSettings("LocalDirectory", _context.Environment.WorkingDirectory.FullPath);

            using (var session = new Session())
            {
                session.Open(options);
                Logger.Log($"{localFolder} and {remoteFolder} are being compared");
                var result = session.CompareDirectories(mode, localFolder, remoteFolder, removeFiles, mirror, criteria, transferOptions) as IEnumerable <ComparisonDifference>;
                if (logDifferences)
                {
                    if (result == null)
                    {
                        Logger.Log("Compare Result was Null");
                        return(result);
                    }
                    Logger.Log(String.Format("|{0,16}|{1,10}|{2,20}|{3,20}|", "Action".PadRight(16), "Directory".PadRight(10), "Local File Name".PadRight(20), "Remote File Name".PadRight(20)));
                    foreach (var diff in result)
                    {
                        var localFile = diff?.Local?.FileName;
                        if (String.IsNullOrEmpty(localFile))
                        {
                            localFile = Path.GetFileName(localFile);
                        }

                        var remoteFile = diff?.Remote?.FileName;
                        if (!String.IsNullOrEmpty(remoteFile))
                        {
                            remoteFile = Path.GetFileName(remoteFile);
                        }

                        Logger.Log(String.Format("|{0,16}|{1,10}|{2,20}|{3,20}|",
                                                 diff?.Action.ToString().PadRight(16),
                                                 diff?.IsDirectory.ToString().PadRight(10),
                                                 localFile?.PadRight(20),
                                                 remoteFile?.PadRight(20)));
                    }
                }
                return(result);
            }
        }
Ejemplo n.º 9
0
        public void GetFiles(SessionOptions options,
                             string remoteFolder,
                             string localFolder,
                             bool removeFiles = false,
                             TransferOptions transferOptions = null)
        {
            options.AddRawSettings("LocalDirectory", _context.Environment.WorkingDirectory.FullPath);

            using (var session = new Session())
            {
                session.Open(options);
                var result = session.GetFiles(remoteFolder, localFolder, removeFiles, transferOptions);
                result.Check();
                Logger.Log($"{remoteFolder} was downloaded to {localFolder}");
            }
        }
Ejemplo n.º 10
0
        public void downloadNacks()
        {
            try
            {
                // using (FileStream fs = File.Create(localPath))
                // {
                //     Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                //     // Add some information to the file.
                //     fs.Write(info, 0, info.Length);
                // }

                // // Open the stream and read it back.
                // using (StreamReader sr = File.OpenText(localPath))
                // {
                //     string s = "";
                //     while ((s = sr.ReadLine()) != null)
                //     {
                //         Console.WriteLine(s);
                //     }
                // }

                sessionOptions.AddRawSettings("FtpUseMlsd", "1");

                using (Session session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;
                    // transferOptions.FilePermissions = null;
                    transferOptions.PreserveTimestamp = false;
                    // transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;

                    // Your code
                    TransferOperationResult transfer;
                    transfer = session.GetFiles("placeholder", localPath, false, transferOptions);
                    transfer.Check();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Synchronizes directories.
        /// </summary>
        /// <param name="url">Session URL (https://winscp.net/eng/docs/session_url).</param>
        /// <param name="remoteFolder">Full path to remote directory.</param>
        /// <param name="localFolder">Full path to local directory.</param>
        /// <param name="removeFiles">When set to true, deletes obsolete files.</param>
        public void SynchronizeDirectories(
            string url,
            string remoteFolder,
            string localFolder,
            bool removeFiles = false)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                throw new ArgumentException(nameof(url));
            }

            if (string.IsNullOrWhiteSpace(remoteFolder))
            {
                throw new ArgumentException(nameof(remoteFolder));
            }

            if (string.IsNullOrWhiteSpace(localFolder))
            {
                throw new ArgumentException(nameof(localFolder));
            }

            if (!Directory.Exists(localFolder))
            {
                throw new DirectoryNotFoundException($"{localFolder} is not found");
            }

            var sessionOptions = new SessionOptions();

            sessionOptions.ParseUrl(url);
            sessionOptions.AddRawSettings("LocalDirectory", _context.Environment.WorkingDirectory.FullPath);

            using (var session = new Session())
            {
                Logger.Log($"Synchronize directories on {url}");

                session.FileTransferred += OnFileTransferred;
                session.Open(sessionOptions);
                var result = session.SynchronizeDirectories(SynchronizationMode.Remote, localFolder, remoteFolder, removeFiles);
                result.Check();

                Logger.Log($"{localFolder} and {remoteFolder} were synchronized");
            }
        }
Ejemplo n.º 12
0
        public void PutFiles(SessionOptions options,
                             string remoteFolder,
                             string localFolder,
                             bool removeFiles = false,
                             TransferOptions transferOptions = null)
        {
            options.AddRawSettings("LocalDirectory", _context.Environment.WorkingDirectory.FullPath);

            using (var session = new Session())
            {
                session.FileTransferred += OnFileTransferred;
                session.Open(options);
                session.FileExists(remoteFolder);
                session.CreateDirectory(remoteFolder);
                var result = session.PutFiles(localFolder, remoteFolder, removeFiles, transferOptions);
                result.Check();

                Logger.Log($"{localFolder} was pushed to {remoteFolder}");
            }
        }
Ejemplo n.º 13
0
        private void button7_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                //Using WinSCP for deleting directories
                string bareServer = confList["Server"].Split(':')[0];

                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Ftp,
                    HostName = bareServer,
                    UserName = confList["Username"],
                    Password = confList["Password"],
                };
                sessionOptions.AddRawSettings("ProxyPort", "0");

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

                    string servName = radioButton1.Checked ? "LabZ_Server_Upper" : "LabZ_Server_Lower";
                    // Transfer files
                    session.GetFiles(confList["Fpath"] + "/" + listBox1.SelectedItem, installPath + '\\' + servName).Check();
                    saveLog("Directory:--" + listBox1.SelectedItem + "--Downloaded to:--" + installPath + "--as:--" + servName);
                    MessageBox.Show("All files were download to:--" + installPath, "Download", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                logs = ex.Message;
                saveLog(logs);
                logs = "";
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
            }
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            // local path to which data files will be downloaded
            var localPath = ConfigurationManager.AppSettings["localPath"];
            // default extension of data files
            var dataFileExtension = ConfigurationManager.AppSettings["dataFileExtension"];
            // if provided, downloaded this day's files from controller
            // if not provided, default to yesterday's date
            var overrideDate = ConfigurationManager.AppSettings["overrideDate"];
            // whether or not to skip the ftp process
            var skipFtp = Convert.ToBoolean(ConfigurationManager.AppSettings["skipFtp"]);
            // whether or not to skip the database insert process
            var skipDatabaseInserts = Convert.ToBoolean(ConfigurationManager.AppSettings["skipDatabaseInserts"]);
            // number of insert commands to bundle up before performing them in the database
            var bulkCommandCount = Convert.ToInt32(ConfigurationManager.AppSettings["bulkCommandCount"]);

            var now = DateTime.Now;
            var currentDateTimeForFileName = $"{now.Year}{now.Month:D2}{now.Day:D2}{now.Hour:D2}{now.Minute:D2}{now.Second:D2}{now.Millisecond}";

            using (var logFile = File.AppendText($"{localPath}\\logs\\ftpLog.{currentDateTimeForFileName}.txt"))
            {
                logFile.WriteLine($"Started FTP process at {DateTime.Now}");

                // Get the list of controller from which to FTP files
                var config = ControllerSection.GetConfig();

                if (!skipFtp)
                {
                    foreach (ControllerElement controller in config.Controllers)
                    {
                        Session        session        = new Session();
                        SessionOptions sessionOptions = new SessionOptions();

                        var pPath         = $"{localPath}\\PrivateKeys.ppk";
                        var numberRetries = 0;
                        var connected     = false;

                        // Will retry FTP'ing 3 times in case of connection issues
                        while ((++numberRetries <= 3) && !connected)
                        {
                            try
                            {
                                // Set up session options
                                if (controller.Protocol == "ftp")
                                {
                                    sessionOptions = new SessionOptions
                                    {
                                        Protocol   = Protocol.Ftp,
                                        HostName   = controller.IpAddress,
                                        PortNumber = 21,
                                        UserName   = controller.User,
                                        Password   = controller.Pwd,
                                    };
                                }
                                else
                                {
                                    sessionOptions = new SessionOptions
                                    {
                                        Protocol = Protocol.Sftp,
                                        HostName = controller.IpAddress,
                                        UserName = controller.User,
                                        Password = controller.Pwd,
                                        SshHostKeyFingerprint = controller.SshFingerPrint,
                                        SshPrivateKeyPath     = pPath,
                                    };

                                    sessionOptions.AddRawSettings("FSProtocol", "2");
                                }

                                // Connect
                                session.Open(sessionOptions);
                                connected = true;
                            }
                            catch (Exception ex)
                            {
                                logFile.WriteLine($"Error connecting to {controller.Name} on attempt {numberRetries}: {ex.Message}");
                            }

                            if (connected)
                            {
                                try
                                {
                                    using (session)
                                    {
                                        if (session.FileExists(controller.RemotePath))
                                        {
                                            // Enumerate files and directories to download
                                            IEnumerable <RemoteFileInfo> remoteFiles = session.EnumerateRemoteFiles(controller.RemotePath, null, EnumerationOptions.EnumerateDirectories | EnumerationOptions.AllDirectories);

                                            var yesterday = DateTime.Today.AddDays(-1);
                                            var fileDate  = (string.IsNullOrEmpty(overrideDate))
                                                ? $"{yesterday.Year}_{yesterday.Month:D2}_{yesterday.Day:D2}"
                                                : overrideDate;

                                            foreach (RemoteFileInfo file in remoteFiles)
                                            {
                                                string localFilePath     = session.TranslateRemotePathToLocal(file.FullName, controller.RemotePath, $"{localPath}\\");
                                                long   fileLength        = 0;
                                                var    numberFileRetries = 0;
                                                var    filePath          = $"{localPath}\\{file.Name}";

                                                // Will retry to download each individual file if we get back a 0 byte file
                                                while (++numberFileRetries <= 3 && fileLength == 0)
                                                {
                                                    if (file.IsDirectory || !file.Name.StartsWith($"{controller.FilePrefix}{fileDate}") ||
                                                        !file.Name.EndsWith(dataFileExtension) ||
                                                        (File.Exists(filePath)))
                                                    {
                                                        if (!file.Name.StartsWith($"{controller.FilePrefix}{fileDate}"))
                                                        {
                                                            numberFileRetries = 3;
                                                        }
                                                        else
                                                        {
                                                            logFile.WriteLine($"Skipping {filePath}");
                                                        }
                                                        continue;
                                                    }

                                                    // Download file
                                                    TransferOperationResult transferResult = session.GetFiles(session.EscapeFileMask(file.FullName), localFilePath);

                                                    // Did the download succeeded?
                                                    if (!transferResult.IsSuccess)
                                                    {
                                                        // Print error (but continue with other files)
                                                        logFile.WriteLine("Error downloading file {0}: {1}", file.Name, transferResult.Failures[0].Message);
                                                    }
                                                    var localFile = new FileInfo(filePath);
                                                    fileLength = localFile.Length;
                                                    logFile.WriteLine($"Transferred {filePath} - length {fileLength}");

                                                    if (fileLength == 0)
                                                    {
                                                        logFile.WriteLine($"Deleting zero length file {filePath}");
                                                        localFile.Delete();
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    logFile.WriteLine($"Error on {controller.Name}: {ex.Message}");
                                    connected = false;
                                }
                            }
                        }
                        logFile.WriteLine("Finished with FTP process.");
                    }
                }
                else
                {
                    logFile.WriteLine("Skipping the FTP process.");
                }

                if (!skipDatabaseInserts)
                {
                    logFile.WriteLine($"Started database inserts at {DateTime.Now}");

                    // Execute the external perfLogTranslate.exe file to convert each .dat file to a .csv file
                    var perfLogTranslate = new Process
                    {
                        StartInfo =
                        {
                            FileName         = $"{localPath}\\perfLogTranslate.exe",
                            Arguments        = " -i *.dat",
                            WorkingDirectory = localPath
                        }
                    };
                    perfLogTranslate.Start();
                    perfLogTranslate.WaitForExit();

                    Directory.SetCurrentDirectory(localPath);
                    var datDir   = new DirectoryInfo(localPath);
                    var datFiles = datDir.GetFiles("*.dat");

                    foreach (var file in datFiles)
                    {
                        // If .dat file exists in old folder, replace with this one
                        var filePath = $"{localPath}\\old\\{file.Name}";
                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }

                        file.MoveTo(filePath);
                    }

                    Directory.SetCurrentDirectory(localPath);
                    var csvDir   = new DirectoryInfo(localPath);
                    var csvFiles = csvDir.GetFiles("*.csv");

                    foreach (var file in csvFiles)
                    {
                        DataTable table = new DataTable();
                        table.Columns.Add("SignalId");
                        table.Columns.Add("Timestamp");
                        table.Columns.Add("EventCode");
                        table.Columns.Add("EventParam");

                        try
                        {
                            var reader = new StreamReader(file.Name);

                            string line;
                            if ((line = reader.ReadLine()) != null)
                            {
                                var parms = line.Split(',');

                                var connectedToDb = true;
                                var conn          = new SqlConnection
                                {
                                    ConnectionString = ConfigurationManager.ConnectionStrings["MOE"].ToString()
                                };

                                try
                                {
                                    conn.Open();
                                }
                                catch (Exception ex)
                                {
                                    logFile.WriteLine($"Error connecting to database: {ex.Message}");
                                    connectedToDb = false;
                                }

                                if (connectedToDb)
                                {
                                    var cmd = new SqlCommand
                                    {
                                        Connection  = conn,
                                        CommandText = $"select SignalID from Signals where IPAddress = '{parms[0]}'",
                                        CommandType = CommandType.Text
                                    };
                                    var signalId = cmd.ExecuteScalar();
                                    cmd.Dispose();

                                    while ((line = reader.ReadLine()) != null)
                                    {
                                        parms = line.Split(',');

                                        DataRow row = table.NewRow();
                                        row.SetField(0, signalId);
                                        row.SetField(1, parms[0]);
                                        row.SetField(2, parms[1]);
                                        row.SetField(3, parms[2]);
                                        table.Rows.Add(row);
                                    }

                                    logFile.WriteLine($"Preparing to bulk copy {table.Rows.Count} rows for signal {signalId}");
                                    try
                                    {
                                        SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, null);
                                        bulkCopy.DestinationTableName = "Controller_Event_Log";
                                        bulkCopy.WriteToServer(table);
                                        logFile.WriteLine($"Bulk copy for {signalId} successful");
                                    }
                                    catch (Exception e)
                                    {
                                        logFile.WriteLine($"Error while executing bulk copy for {signalId}");
                                    }

                                    table.Dispose();

                                    reader.Close();
                                    reader.Dispose();

                                    logFile.WriteLine($"Done with {file.Name} at {DateTime.Now}");
                                    // If .csv file exists in old folder, replace with this one
                                    var filePath = $"{localPath}\\old\\{file.Name}";
                                    if (File.Exists(filePath))
                                    {
                                        File.Delete(filePath);
                                    }
                                    file.MoveTo(filePath);
                                }
                                else
                                {
                                    logFile.WriteLine($"Leaving {file.Name} in folder unprocessed.");
                                }

                                try
                                {
                                    conn.Close();
                                    conn.Dispose();
                                }
                                catch (Exception ex)
                                {
                                    logFile.WriteLine($"Error closing or disposing connection.");
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            logFile.WriteLine($"Error: {ex.Message}");
                        }
                    }

                    logFile.WriteLine("Finished with database inserts.");
                }
                else
                {
                    logFile.WriteLine("Skipping database inserts.");
                }

                logFile.WriteLine($"Finished at {DateTime.Now}");
            }
        }
Ejemplo n.º 15
0
        static void Main(string[] args)
        {
            Console.WriteLine("UFV Bulk File Downloader");
            Console.Write("Enter Camera UUID:");
            string UUID = Console.ReadLine();

            Console.WriteLine("Note: Dates are in UTC (7PM EST = 12AM UTC)");
            Console.Write("Enter year:");
            string Year = Console.ReadLine();

            Console.Write("Enter Month:");
            string Month = Console.ReadLine();

            Console.Write("Enter Day:");
            string Day = Console.ReadLine();

            Console.WriteLine("Note: Times are in Local time zone in UNIX format. https://www.epochconverter.com/");
            Console.Write("Start Time:");
            long StartUnix;

            if (long.TryParse(Console.ReadLine(), out long x))
            {
                StartUnix = x;
            }
            else
            {
                Console.WriteLine("Couldn't parse input.");
                return;
            }

            Console.Write("End Time:");
            long EndUnix;

            if (long.TryParse(Console.ReadLine(), out x))
            {
                EndUnix = x;
            }
            else
            {
                Console.WriteLine("Couldn't parse input.");
                return;
            }

            try
            {
                // Setup session options
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Scp,
                    HostName = args[0],
                    UserName = args[1],
                    Password = args[2],
                    SshHostKeyFingerprint = args[3] + " " + args[4] + " " + args[5],
                };
                sessionOptions.AddRawSettings("Shell", "sudo%20-s");
                using (Session session = new Session())
                {
                    Console.WriteLine("Connecting...");
                    // Connect
                    session.Open(sessionOptions);

                    Console.WriteLine("Connected.");
                    // Example: /mnt/raid0/recordings/735682ef-c7d2-31c0-b217-5fae42fe52a4/2019/12/07/
                    RemoteDirectoryInfo directory = session.ListDirectory(args[6] + "/" + UUID + "/" + Year + "/" + Month + "/" + Day + "/");
                    long TotalBytes = 0;

                    foreach (RemoteFileInfo fileInfo in directory.Files)
                    {
                        System.DateTime StartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                        System.DateTime EndTime   = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                        if (fileInfo.LastWriteTime > StartTime.AddSeconds(StartUnix - 2).ToLocalTime() && fileInfo.LastWriteTime < EndTime.AddSeconds(EndUnix).ToLocalTime() && fileInfo.FileType == '-')
                        {
                            TotalBytes += fileInfo.Length;
                            DownloadList.Add(fileInfo);
                        }
                    }
                    //Sort list:
                    DownloadList.Sort((z, y) => z.LastWriteTime.CompareTo(y.LastWriteTime));
                    Console.WriteLine("Total of " + DownloadList.Count + " files for a total time of about " + DownloadList.Count / 30 + " minutes.");
                    Console.WriteLine("Combined file size: " + TotalBytes + " Bytes.");

                    Console.Write("Would you like to continue with the download? [y/n] ");
                    string key = Console.ReadLine();
                    if (key != "y" && key != "Y")
                    {
                        Console.WriteLine("Exiting.");
                        return;
                    }
                    Console.Write("\nEnter Output:");
                    string output = Console.ReadLine();
                    //Downloader:
                    Console.WriteLine("Initializing Downloads...");
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;
                    TransferOperationResult transferResult;
                    Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                    Console.WriteLine("Downloading...");
                    string FolderName = Path.GetTempPath() + @"VideoFiles" + unixTimestamp + @"\";
                    System.IO.Directory.CreateDirectory(FolderName);
                    Console.WriteLine(FolderName);
                    int DownloadCount = 0;

                    //list for splicing:
                    List <string> fileLocations = new List <string>();
                    foreach (RemoteFileInfo fileInfo in DownloadList)
                    {
                        transferResult = session.GetFiles(fileInfo.FullName, FolderName, false, transferOptions);
                        transferResult.Check();
                        fileLocations.Add(fileInfo.FullName.Substring(fileInfo.FullName.LastIndexOf('/') + 1));
                        DownloadCount++;
                        Console.Write("\rDownloaded: {0} Files.", DownloadCount);
                    }
                    Console.WriteLine("");
                    //Complile videos:
                    VideoProcessing processor = new VideoProcessing(FolderName, FolderName, fileLocations);
                    Console.WriteLine("Splicing files...");
                    //add true for XABE.FFmpeg testing.
                    processor.Concatnate(output);
                    Console.WriteLine("Complete.");
                }

                return;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
                return;
            }
        }
Ejemplo n.º 16
0
        private static void DeployServersWork(SessionOptions sessionOptions, string sshPrivateKeyPath)
        {
            Session session = new Session();

            try
            {
                sessionOptions.SshPrivateKeyPath = sshPrivateKeyPath;
                sessionOptions.AddRawSettings("AuthGSSAPI", "1");
                sessionOptions.AddRawSettings("TcpNoDelay", "1");

                //Debug.Log("DEPLOY:: Opening session with server at == " + sessionOptions.HostName);

                session.ExecutablePath      = Path.Combine(_applicationDataPath, _winSCPExePath);
                session.DisableVersionCheck = true;

                // Connect
                EditorUtility.DisplayProgressBar("DEPLOYING", "Opening session with: " + sessionOptions.HostName, IncreaseProgress(0.05f)); // 0.05
                session.Open(sessionOptions);

                // Terminate active session
                var killCmdText = "screen -S server -X quit";
                EditorUtility.DisplayProgressBar("DEPLOYING", "Terminating previous server screen with command: " + killCmdText, IncreaseProgress(0.15f)); // 0.2
                ExecuteCommand(session, killCmdText, "Terminate screen command.", "Terminated active sessions successfully.");

                // Upload server build
                var localPath  = GetServerDirectoryPath();
                var remotePath = _remotePath;
                var filepath   = GetServerFilePath();
                //Debug.Log("DEPLOY:: Uploading files from local == " + localPath + " to remote == " + remotePath);

                // Upload actual executable file
                EditorUtility.DisplayProgressBar("DEPLOYING", "Uploading executable file from: " + filepath, IncreaseProgress(0.05f)); // 0.25
                UploadFiles(session, filepath, remotePath, new TransferOptions()
                {
                    OverwriteMode     = OverwriteMode.Overwrite,
                    PreserveTimestamp = false,
                    FilePermissions   = new FilePermissions()
                    {
                        GroupExecute = true, OtherExecute = true, UserExecute = true
                    }
                });

                // Upload Server_Data folder & contents
                var dataFolder = Path.Combine(localPath, BuildScripts.serverBuildName + "_Data");
                EditorUtility.DisplayProgressBar("DEPLOYING", "Uploading Data folder from: " + dataFolder, IncreaseProgress(0.25f)); // 0.5
                UploadFiles(session, dataFolder, remotePath);

                // Start screen
                var newScreenCmd = "screen -S server -dm";
                EditorUtility.DisplayProgressBar("DEPLOYING", "Starting new server screen with command:" + newScreenCmd, IncreaseProgress(0.4f)); // 0.9
                ExecuteCommand(session, newScreenCmd, "Start screen command error", "Start screen command successful");

                // Run server in screen
                var filename       = Path.GetFileName(filepath);
                var startServerCmd = string.Concat("screen -S server -p 0 -X stuff './", filename, " -batchmode -nographics -headlessmode\n'");
                EditorUtility.DisplayProgressBar("DEPLOYING", "Starting server in new screen with command: " + startServerCmd, IncreaseProgress(0.05f)); // 0.95
                ExecuteCommand(session, startServerCmd, "Start server command error", "Start server command succesful");

                //Debug.Log("DEPLOY:: Finished uploading and starting server at hostname == " + sessionOptions.HostName);
            }
            catch (Exception e)
            {
                Debug.LogError("DEPLOY:: WINSCP Session (" + sessionOptions.HostName + ") exception: " + e.ToString());
            }
            finally
            {
                session.Dispose();
            }
        }
Ejemplo n.º 17
0
        public int Connect()
        {
            // Set up session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = _protocol,
                HostName = _host,
                UserName = _username,
                Password = _password,
                SshHostKeyFingerprint = _fingerprint
            };

            if (_keyPath != "" && File.Exists(_keyPath))
            {
                sessionOptions.SshPrivateKeyPath    = _keyPath;
                sessionOptions.PrivateKeyPassphrase = _password;
                sessionOptions.Password             = null;
            }

            sessionOptions.AddRawSettings("PingIntervalSecs", "10");
            sessionOptions.AddRawSettings("TcpNoDelay", "1");

            _session = new Session();
            try
            {
                _session.Open(sessionOptions);
            }
            catch (Exception ex)
            {
                string key = ex.Message.Substring(ex.Message.IndexOf("!") + 1);
                key = key.Substring(key.IndexOf(":") + 1);
                key = key.Substring(0, key.IndexOf("."));
                if (key.Contains("Host key fingerprint is"))
                {
                    key = key.Substring(key.IndexOf("Host key fingerprint is") + "Host key fingerprint is".Length);
                }
                key = key.Trim();

                if (sessionOptions.SshHostKeyFingerprint == "ssh-ed25519 256 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00")
                {
                    sessionOptions.SshHostKeyFingerprint = key;
                    Dbh.updateSshFingerPrint(_connection.Id, key);
                    _session.Open(sessionOptions);
                }
                else
                {
                    var msg = MessageBox.Show("The SSH Host Fingerprint of the device you are connecting to, \r\n" +
                                              "doesn't match the original Fingerprint saved in the database.\r\n\r\n" +
                                              "Do you wish to accept the new Fingerprint \r\n" +
                                              "and update the one stored in the database?",
                                              "Fingerprint Mismatch",
                                              MessageBoxButtons.YesNo,
                                              MessageBoxIcon.Warning);

                    if (msg == DialogResult.Yes)
                    {
                        Dbh.updateSshFingerPrint(_connection.Id, key);
                    }
                    else
                    {
                        c_LogManager.add(_connection.Log_id, _connection.Project_id, _logData);
                        return(-1);
                    }
                }
            }

            return(_connection.Project_id);
        }