/// <summary> /// Not sure this works as we have not needed it. /// However, if password is requested by server, it will pass what was used. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void HandleKeyEvent(object sender, AuthenticationPromptEventArgs e) { foreach (AuthenticationPrompt prompt in e.Prompts) { if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1) { if (DataFiles.CheckPass()) { prompt.Response = Defs.PassWord; } } } }
/// <summary> /// Setup SSH Connection to given Host. /// </summary> /// <returns></returns> private bool SetupConnection() { bool retVal = true; if (_sshClient == null || !_sshClient.IsConnected) { try { //make sure a host exists, if not ask for one. if (!DataFiles.CheckHost()) { return(false); } //make sure an environment exists, if not ask for one. DataFiles.CheckEnv(); //make sure a username exists, if not ask for one. if (!DataFiles.CheckUser()) { return(false); } //make sure a password exists, if not ask for one. if (!DataFiles.CheckPass()) { return(false); } //display welcome text for application. General.ApplicationHeader(true); //let the user know what we are doing. Log.Verbose("\n\n Setting authentication...", ConsoleColor.Yellow); //account setup.. KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(Defs.UserName); PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(Defs.UserName, Defs.PassWord); kauth.AuthenticationPrompt += new EventHandler <AuthenticationPromptEventArgs>(HandleKeyEvent); //setup connection ConnectionInfo connectionInfo = new ConnectionInfo(Defs.HostName, Defs.Port, Defs.UserName, pauth, kauth); connectionInfo.Timeout = Defs.ConnectionTimeout; //pass connection data _sshClient = new SshClient(connectionInfo); //let the user know what we are doing. Log.Verbose("Attempting to connect...", ConsoleColor.Yellow); //connect to linux _sshClient.Connect(); //let the user know what we are doing. Log.Verbose("Connected, creating shell stream...", ConsoleColor.Green); //lets build out a stream that will each back what we request. var terminalMode = new Dictionary <TerminalModes, uint>(); //terminalMode.Add(TerminalModes.ECHO, 53); //create shell stream _shellStream = _sshClient.CreateShellStream("input", 255, 50, 400, 600, 4096, terminalMode); //keep track of remote directory. Can be useful, in case response of User@Server isn't configured to return. SendCommand("pwd -P", false); //it is possible that sometimes Messages come back on first connection to linux machine, before the information. if (Defs.DataContent.Length > 50) { Defs.DataContent.Clear(); SendCommand("pwd -P", false); } //break it down for prompt. string[] remote = Defs.DataContent.ToString().Replace(Environment.NewLine, "").Split('/'); Defs.DataContent.Clear(); //Don't set prompt if still showing message. if (string.Join("/", remote).Trim().Length < 40 && remote.Length <= 3) { Defs.PromptsRemoteDir = remote[remote.Length - 1].Trim(); } //if default Remote Path not setup, give it the current folder. don't set remote path if still showing message. if (Defs.RemotePath.Equals("/") && string.Join("/", remote).Trim().Length < 40 && remote.Length <= 3) { Defs.RemotePath = string.Join("/", remote).Trim(); } //save host and user to history. if (Defs.NewHost) { DataFiles.SaveHostHistory(); } //let the user know what we are doing. Log.Verbose("Successful Connection Established...\n", ConsoleColor.Green); //set the current folder as local path Directory.SetCurrentDirectory(Defs.LocalPath); Console.Title = $"{Defs.ConsoleTitle} -=[{Defs.UserName}@{Defs.HostName}]=-"; //clear screen and display new header General.ApplicationHeader(true); //let the user know what's configured. Log.Verbose($"Local Path has been set to {Defs.LocalPath}...", ConsoleColor.Yellow); Log.Verbose($"Remote Path has been set to {Defs.RemotePath}...\n", ConsoleColor.Yellow); Log.Verbose($"Change these paths with \"set-local\" and \"set-remote\"...\n\n", ConsoleColor.Yellow); } catch (Exception ex) { retVal = false; Log.Error($"Exception - {ex.Message}", 1); Log.Verbose("Press any key to continues.", ConsoleColor.White); Console.ReadKey(); //connection didn't work right, shut it down. Defs.Shutdown = true; } } return(retVal); }