private static SshClient CreateClient(CliVerbOptionsBase options) { var simpleConnectionInfo = new PasswordConnectionInfo(options.Host, options.Port, options.Username, options.Password); return(new SshClient(simpleConnectionInfo)); }
/// <summary> /// Checks that both, SFTP and SSH clients have a working connection. If they don't it attempts to reconnect. /// </summary> /// <param name="sshClient">The SSH client.</param> /// <param name="sftpClient">The SFTP client.</param> /// <param name="verbOptions">The verb options.</param> private static void EnsureMonitorConnection(SshClient sshClient, SftpClient sftpClient, CliVerbOptionsBase verbOptions) { if (sshClient.IsConnected == false) { $"Connecting to host {verbOptions.Host}:{verbOptions.Port} via SSH.".WriteLine(); sshClient.Connect(); } if (sftpClient.IsConnected == false) { $"Connecting to host {verbOptions.Host}:{verbOptions.Port} via SFTP.".WriteLine(); sftpClient.Connect(); } }
static void Main(string[] args) { #region Handle Single Instance Application bool isNewMutex; AppMutex = new Mutex(true, MutexName, out isNewMutex); if (isNewMutex == false) { AppMutex = null; Environment.ExitCode = CommandLine.Parser.DefaultExitCodeFail; return; } #endregion Console.WriteLine("SSH Deployment Tool [Version " + typeof(Unosquare.Labs.SshDeploy.Program).Assembly.GetName().Version.ToString() + "]"); Console.WriteLine("(c) 2015 Unosquare SA de CV. All Rights Reserved."); var invokedVerbName = string.Empty; CliVerbOptionsBase invokedVerbOptions = null; var options = new CliOptions(); var parseResult = CommandLine.Parser.Default.ParseArguments(args, options, (verb, verbOptions) => { invokedVerbName = verb; invokedVerbOptions = verbOptions as CliVerbOptionsBase; if (invokedVerbOptions != null) { ConsoleManager.Verbose = invokedVerbOptions.Verbose != 0; } }); if (parseResult == false) { Environment.ExitCode = CommandLine.Parser.DefaultExitCodeFail; return; } try { switch (invokedVerbName) { case CliOptions.RunVerb: { var verbOptions = invokedVerbOptions as RunVerbOptions; DeploymentManager.ExecuteRunVerb(verbOptions); break; } case CliOptions.ShellVerb: { var verbOptions = invokedVerbOptions as ShellVerbOptions; DeploymentManager.ExecuteShellVerb(verbOptions); break; } case CliOptions.MonitorVerb: { var verbOptions = invokedVerbOptions as MonitorVerbOptions; DeploymentManager.ExecuteMonitorVerb(verbOptions); break; } } } catch (Exception ex) { ConsoleManager.ErrorWriteLine("Error - " + ex.GetType().Name); ConsoleManager.ErrorWriteLine(ex.Message); ConsoleManager.ErrorWriteLine(ex.StackTrace); Environment.ExitCode = CommandLine.Parser.DefaultExitCodeFail; } if (Environment.ExitCode != 0) { ConsoleManager.ErrorWriteLine("Completed with errors. Exit Code " + Environment.ExitCode.ToString()); } else { ConsoleManager.WriteLine("Completed."); } }