private LoginStatus TryenPWD() { LoginStatus pwdResult = LoginStatus.InProgress; string match = ""; if (_Debug > 0) { Console.WriteLine("\nDebug-Info:"); Console.WriteLine("enLogin-Try-" + PWDCounter.ToString() + ": " + enPWDArray[PWDCounter]); } shell.buffer_empty(); shell.print(enPWDArray[PWDCounter]); //sleep 1; match = shell.WaitForRegEx("Password:|" + DevicePromptString + @"[>#]"); if ((match == null) || (match.Length == 0)) { if (_Debug > 0) { Console.WriteLine(shell.ShowScreen() + "---"); } return(LoginStatus.Failed); //LoginStatus.InProgress } if (HelperClass.RegExCompare(match, DevicePromptString + @"[#]\s*\r?\n?$", RegexOptions.Multiline)) { if (_Debug > 0) { Console.WriteLine("Found Match-" + PWDCounter.ToString() + ": " + match); } pwdResult = LoginStatus.Success; } else if (HelperClass.RegExCompare(match, DevicePromptString + @"[>]\s*\r?\n?$", RegexOptions.Multiline)) { shell.buffer_empty(); if ((PWDCounter + 1) < enPWDArray.Count) { shell.print("\nenable"); pwdResult = LoginStatus.Continue; match = shell.WaitForRegEx(".*Password:.*"); if ((match == null) || (match.Length == 0)) { if (_Debug > 0) { Console.WriteLine(shell.ShowScreen() + "---"); } return(LoginStatus.Failed); //LoginStatus.InProgress } } else { return(LoginStatus.UserMode); } } else if (match.ToLower().Contains("password:")) { pwdResult = LoginStatus.Continue; } return(pwdResult); }
private LoginStatus TryPWD() { LoginStatus pwdResult = LoginStatus.InProgress; string match = ""; shell.buffer_empty(); if (Debug > 0) { Console.WriteLine("\nDebug-Info:"); Console.WriteLine("Login-Try-" + PWDCounter.ToString() + ": " + PWDArray[PWDCounter]); } shell.print(PWDArray[PWDCounter]); // sleep 1; if (bConnected > 1) { match = shell.WaitForRegEx("Password:|" + unknownPrompt + "|" + DeviceHistory[DeviceCounter].DevicePrompt); } else { match = shell.WaitForRegEx("Password:|" + unknownPrompt); } //ToDo: // instead of: }else if( $match =~ /[>]\s?$|[#]\s?$/m ) use $unknownPrompt if ((match == null) || (match.Length == 0)) { if (Debug > 0) { Console.WriteLine("\nNo Prompt Found."); } if ((shell != null) && !shell.Connected) { pwdResult = LoginStatus.InProgress; // We are disconnected from Remote-Device } else { pwdResult = LoginStatus.Failed; } } else if ((bConnected > 1) && HelperClass.RegExCompare(match, DeviceHistory[DeviceCounter].DevicePrompt, RegexOptions.Multiline | RegexOptions.IgnoreCase)) { //We are back on the source-Device pwdResult = LoginStatus.InProgress; } else if (HelperClass.RegExCompare(match, @"[>]\s?$|[#]\s?$", RegexOptions.Multiline)) { if (Debug > 0) { Console.WriteLine("\nFound Match-" + PWDCounter.ToString() + ": " + match); } pwdResult = LoginStatus.Success; } else if (match.ToLower().Contains("password:")) { pwdResult = LoginStatus.Continue; } else { pwdResult = LoginStatus.Unknown; } return(pwdResult); }
private LoginStatus TryUserLogin(string strPrompt, string matchPrompt) { LoginStatus pwdResult = LoginStatus.InProgress; string match = ""; 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() + "---"); } return(LoginStatus.Failed); } shell.print(UserArray[PWDCounter].Pwd); if (bConnected > 1) { match = shell.WaitForRegEx(strPrompt + "|(Username:)|(Login:)|(Login Name:)|(" + DeviceHistory[DeviceCounter].DevicePrompt + ")"); } else { match = shell.WaitForRegEx(strPrompt + "|(Username:)|(Login:)|(Login Name:)"); } if ((match == null) || (match.Length == 0)) { if ((shell != null) && !shell.Connected) { return(LoginStatus.InProgress); //We are Disconnected from Remote-Device } else { if ((Debug > 0) && (shell != null) && shell.Connected) { Console.WriteLine(shell.ShowScreen() + "---"); } return(LoginStatus.Failed); } } if ((bConnected > 1) && HelperClass.RegExCompare(match, DeviceHistory[DeviceCounter].DevicePrompt, RegexOptions.Multiline | RegexOptions.IgnoreCase)) { //We are back on the source-Device pwdResult = LoginStatus.InProgress; } else if (HelperClass.RegExCompare(match, matchPrompt, RegexOptions.Multiline)) { if (Debug > 0) { Console.WriteLine("\nFound Match-" + PWDCounter.ToString() + ": " + match); } pwdResult = LoginStatus.Success; } else if ((match.ToLower().Contains("username:"******"login:"******"login name:"))) { pwdResult = LoginStatus.Continue; // 2 } else if (match.Contains(">")) { pwdResult = LoginStatus.UserMode; } else { pwdResult = LoginStatus.Unknown; } return(pwdResult); }
private LoginStatus Login(string host, string ConnectOptions) { string match = "", strOutput = ""; 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 = { "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.ToUpper().Contains("HEWLETT")) { DeviceType = "HP"; // HEWLETT-PACKARD } strTemp = ""; if (match.ToLower().Contains("press any key to continue")) { match = ""; shell.buffer_empty(); string[] LoginPrompt2 = { "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("username:"******"login:"******"login name:"))) { //$PWDCounter = 0; bResult = LoginStatus.Continue; while ((PWDCounter <= UserArray.Count) && (bResult > LoginStatus.InProgress) && (bResult < LoginStatus.Unknown)) { bResult = TryUserLogin(unknownPrompt, unknownPrompt); PWDCounter++; } if (bResult > LoginStatus.Success) { return(bResult); } } else if (match.ToLower().Contains("password:"******"Test-" + PWDCounter.ToString() + ":" + bResult.ToString()); } PWDCounter++; } if (bResult > 0) { return(bResult); } } if (Debug > 0) { Console.WriteLine("\nDebug-Info: Login success."); } //Get full Prompt DevicePrompt = ""; strOutput = shell.cmd("", unknownPrompt); if ((strOutput == null) || (strOutput.Length == 0)) { //Found no Prompt Exit... return(LoginStatus.Unknown); } string[] strLines = strOutput.Split(new char[] { '\n' }); foreach (string strLine in strLines) { strTemp = strLine.Replace("\n", ""); strTemp = strTemp.Replace("\r", ""); strTemp = strTemp.Trim(); // strTemp.Replace(" ", ""); if (strTemp.Length > 0) { LastPromptChar = strTemp.ToCharArray(strTemp.Length - 1, 1)[0]; strTemp = strTemp.Substring(0, strTemp.Length - 1); // Remove Last Char DevicePrompt = HelperClass.quotemeta(strTemp); } } DevicePromptString = DevicePrompt; if (LastPromptChar == '#') { EnableMode = true; DevicePromptConfig = DevicePrompt.Substring(0, 10); if (DevicePromptConfig.EndsWith("\\")) { DevicePromptConfig = DevicePromptConfig.Substring(0, DevicePromptConfig.Length - 1); } DevicePromptConfig = DevicePromptConfig + LastPromptChar; //DevicePromptConfig =~ s/(.*)(.)$/$1.*\\(config.*\\)$2/; # s/(.*)([^\s])\s?$/$1.*\\(config.*\\)$2/; if (DeviceType.Equals("HP")) { DevicePromptConfig = DevicePromptConfig.Substring(0, DevicePromptConfig.Length - 1) + ".*\\(.*\\)" + "#"; } else { DevicePromptConfig = DevicePromptConfig.Trim(); DevicePromptConfig = DevicePromptConfig.Substring(0, DevicePromptConfig.Length - 1) + ".*\\(config.*\\)" + "#"; } DevicePromptConfig = DevicePromptConfig + "[ \\r\\n]*$"; } else { DevicePromptConfig = ""; } DevicePrompt = DevicePrompt + LastPromptChar + "[ \\r\\n]*$"; if (Debug > 0) { Console.WriteLine("\nDebug-Info:"); Console.WriteLine("new Prompt: " + DevicePrompt); Console.WriteLine("Config-Prompt: " + DevicePromptConfig); } return(LoginStatus.Success); }
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); }