public TelnetSSHLogin() { _instance = this; _isRunning = true; _Console = new ConsoleMain(); _terminalOptions = new TerminalOptions(""); _terminalSettings = new TerminalSettings(); _timeout = _Console.ProtocolOptions.SocketConnectTimeout / 1000; }
private LoginStatus Login(string host, string ConnectOptions) { string match = ""; string l_ConStr = ""; LoginStatus bResult = LoginStatus.InProgress; if (shell != null) //if ((shell != null) && shell.Connected) { shell.close(); } shell = null; shell = new TerminalSessions.TelnetSSHLogin(); if (_Debug > 0) { shell.DebugFlag = _Debug; Console.WriteLine("Debug-Info:\nConnect to " + host); } //shell.cmd_remove_mode = "1"; if (!shell.open(UserArray[PWDCounter].User, UserArray[PWDCounter].Pwd, host, 0, "ssh2", "", "", "")) { if (shell == null) { return(LoginStatus.Failed); } else if (!shell.Connected) { return(LoginStatus.InProgress); //LoginStatus.Failed } return(LoginStatus.InProgress); } else { bConnected = 1; } string LoginPrompt = "[Uu]ser:|[Uu]sername:|[Pp]assword:|Press any key to continue|" + unknownPrompt; if (DeviceCounter > 0) //Add Prompt from source-Device { LoginPrompt += "|" + DeviceHistory[DeviceCounter - 1].DevicePrompt; } match = shell.WaitForRegEx(LoginPrompt); if (_Debug > 0) { Console.WriteLine("Debug-Info:\nConnected, start Login."); } if ((match == null) || (match.Length == 0)) { if (_Debug > 0) { Console.WriteLine("Connection failed, found no Loginprompt."); } return(LoginStatus.Unknown); } else if (HelperClass.RegExCompare(match, "user:|username", RegexOptions.IgnoreCase | RegexOptions.Multiline)) { shell.buffer_empty(); if (_Debug > 0) { Console.WriteLine("\nDebug-Info:\n"); Console.WriteLine("Login-Try-" + PWDCounter.ToString() + ": " + UserArray[PWDCounter].User); } shell.print(UserArray[PWDCounter].User); match = shell.WaitForString("Password"); if ((match == null) || (match.Length == 0)) { if (_Debug > 0) { Console.WriteLine(shell.ShowScreen() + "---"); } shell.close(); shell = null; bConnected = 0; return(LoginStatus.InProgress); } shell.buffer_empty(); shell.print(UserArray[PWDCounter].Pwd); if (bConnected > 1) { match = shell.WaitForRegEx(unknownPrompt + "|(" + DeviceHistory[DeviceCounter].DevicePrompt + ")"); } else { match = shell.WaitForRegEx(unknownPrompt + "|(User:)|(Username:)|(Login:)|(Login Name:)"); } if ((match == null) || (match.Length == 0)) { if (_Debug > 0) { Console.WriteLine(shell.ShowScreen() + "---"); } shell.close(); shell = null; bConnected = 0; return(LoginStatus.InProgress); } else if (!HelperClass.RegExCompare(match, unknownPrompt, RegexOptions.Multiline)) { if (_Debug > 0) { Console.WriteLine(shell.ShowScreen() + "---"); } shell.close(); shell = null; bConnected = 0; return(LoginStatus.InProgress); } } else if (HelperClass.RegExCompare(match, "password:"******"Press any key to continue", RegexOptions.IgnoreCase | RegexOptions.Multiline)) { shell.buffer_empty(); shell.print(" "); match = shell.WaitForRegEx(LoginPrompt); if ((match == null) || (match.Length == 0)) { if (_Debug > 0) { Console.WriteLine("Connection failed, found no Loginprompt."); } return(LoginStatus.Unknown); } //ToDo: // Do the Login ??? } if (_Debug > 0) { Console.WriteLine("\nDebug-Info: Login success."); } if (HelperClass.RegExCompare(match, unknownPrompt, RegexOptions.Multiline)) { if (_Debug > 0) { Console.WriteLine("\nFound Match-" + PWDCounter.ToString() + ": " + match); } bResult = LoginStatus.Success; } else { bResult = LoginStatus.Unknown; //return (bResult); } //Get full Prompt DevicePrompt = ""; DevicePrompts devPrompts = detectPrompt(unknownPrompt); if (devPrompts == null) { //Found no Prompt Exit... return(LoginStatus.Unknown); } if (devPrompts.LastPromptChar == '#') { EnableMode = true; } DevicePrompt = devPrompts.DevicePrompt; DevicePromptConfig = devPrompts.DevicePromptConfig; DevicePromptString = devPrompts.DevicePromptString; return(LoginStatus.Success); }
private LoginStatus Login(string host, string ConnectOptions) { string match = ""; string l_ConStr = ""; string strTemp = ""; //char LastPromptChar = '\0'; LoginStatus bResult = LoginStatus.InProgress; if (bConnected == 0) { if (shell != null) //if ((shell != null) && shell.Connected) { shell.close(); } shell = null; shell = new TerminalSessions.TelnetSSHLogin(); if (_Debug > 0) { shell.DebugFlag = _Debug; Console.WriteLine("Debug-Info:\nConnect to " + host); } //shell.cmd_remove_mode = "1"; if (!shell.open("", "", host, 0, "telnet", "", "", "")) { return(LoginStatus.Failed); } else { bConnected = 1; } } else { shell.buffer_empty(); if (_Debug > 0) { Console.WriteLine("Debug-Info:\nConnect to " + host); } // shell.print("telnet " + host + " /vrf " + VRF); l_ConStr = host; if (ConnectOptions.Trim().Length != 0) { l_ConStr = l_ConStr + " " + ConnectOptions; } Console.WriteLine("Will connect to: " + l_ConStr); shell.print("telnet " + l_ConStr); bConnected++; } // sleep 1sec. string[] LoginPrompt = { "User:"******"Username:"******"Login:"******"Login Name:", "Password:"******"Press any key to continue" }; if (DeviceCounter > 0) //Add Prompt from source-Device { LoginPrompt = (string.Join(",", LoginPrompt) + "," + DeviceHistory[DeviceCounter - 1].DevicePrompt).Split(new char[] { ',' }); } match = shell.WaitFor(LoginPrompt, false, Timeout); if (_Debug > 0) { Console.WriteLine("Debug-Info:\nConnected, start Login."); } if ((match == null) || (match.Length == 0)) { if (_Debug > 0) { Console.WriteLine("Connection failed, found no Loginprompt."); } return(LoginStatus.Unknown); } strTemp = shell.ShowScreen(); if (strTemp == null) { strTemp = ""; } else if (strTemp.ToUpper().Contains("HEWLETT")) { DeviceType = "HP"; // HEWLETT-PACKARD } strTemp = ""; if (match.ToLower().Contains("press any key to continue")) { match = ""; shell.buffer_empty(); string[] LoginPrompt2 = { "User:"******"Username:"******"Login:"******"Login Name:", "Password:"******" "); match = shell.WaitFor(LoginPrompt2, false, Timeout); if ((match == null) || (match.Length == 0)) { if (_Debug > 0) { Console.WriteLine("Connection failed, found no Loginprompt."); } return(LoginStatus.Unknown); } } if ((match.ToLower().Contains("user:"******"username:"******"login:"******"login name:"))) { //$PWDCounter = 0; bResult = LoginStatus.Continue; while ((PWDCounter < UserArray.Count) && (bResult > LoginStatus.InProgress) && (bResult < LoginStatus.Unknown)) { bResult = TryUserLogin(unknownPrompt, unknownPrompt, UserArray[PWDCounter].User, UserArray[PWDCounter].Pwd, PWDCounter); PWDCounter++; } while ((bConnected > 1) && (bResult == LoginStatus.Continue)) { // Send Dummy-Login to get back to the CLI bResult = TryUserLogin(unknownPrompt, unknownPrompt, "Exit", "Exit", PWDCounter); PWDCounter++; } if (bResult > LoginStatus.Success) { return(bResult); } } else if (match.ToLower().Contains("password:"******"Test-" + PWDCounter.ToString() + ":" + bResult.ToString()); } PWDCounter++; } while ((bConnected > 1) && (bResult == LoginStatus.Continue)) { //Check for Password: and send Dummy-Pwd until last DevicePrompt is detected bResult = TryPWD("Exit", PWDCounter); PWDCounter++; } if (bResult > 0) { return(bResult); } } if (_Debug > 0) { Console.WriteLine("\nDebug-Info: Login success."); } //Get full Prompt DevicePrompt = ""; DevicePrompts devPrompts = detectPrompt(unknownPrompt); if (devPrompts == null) { //Found no Prompt Exit... return(LoginStatus.Unknown); } if (devPrompts.LastPromptChar == '#') { EnableMode = true; } DevicePrompt = devPrompts.DevicePrompt; DevicePromptConfig = devPrompts.DevicePromptConfig; DevicePromptString = devPrompts.DevicePromptString; return(LoginStatus.Success); }