예제 #1
0
        private LoginStatus Enable()
        {
            LoginStatus pwdResult = LoginStatus.InProgress;
            string      strTemp   = "";

            shell.buffer_empty();
            shell.print("\nenable");
            string match = "";

            string[] LoginPrompt = { "Username:"******"Login:"******"Password:"******"username:"******"login:"******"(>|#)", DevicePromptString + "#");
                    if (pwdResult == LoginStatus.UserMode)
                    {
                        shell.buffer_empty();
                        shell.print("\nenable");
                    }
                    PWDCounter++;
                }
                if (pwdResult > LoginStatus.Success)
                {
                    return(LoginStatus.InProgress);
                }
            }
            else if (match.ToLower().Contains("password:"******"", DevicePromptString + "#");

            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.Substring(strTemp.Length - 1, 1).ToCharArray()[0];
                    strTemp        = strTemp.Substring(0, strTemp.Length - 1); //Remove Last Char
                    DevicePrompt   = HelperClass.quotemeta(strTemp);
                }
            }
            DevicePromptString = DevicePrompt;
            DevicePromptConfig = DevicePrompt;
            if (DevicePromptConfig.Length > 10)
            {
                DevicePromptConfig = DevicePromptConfig.Substring(0, 10);
            }
            if (DeviceType.Equals("HP"))
            {
                DevicePromptConfig = DevicePromptConfig + @".*\(.*\)" + LastPromptChar;
            }
            else
            {
                DevicePromptConfig = DevicePromptConfig + @".*\(config.*\)" + LastPromptChar;
            }
            DevicePromptConfig = DevicePromptConfig + "[ \\r\\n]*$";
            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);
        }
예제 #2
0
        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);
        }