コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        private LoginStatus Enable()
        {
            LoginStatus pwdResult = LoginStatus.InProgress;

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

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

            if (devPrompts != null)
            {
                DevicePrompt       = devPrompts.DevicePrompt;
                DevicePromptConfig = devPrompts.DevicePromptConfig;
                DevicePromptString = devPrompts.DevicePromptString;
            }

            /*
             * string strTemp = "";
             * char LastPromptChar = '\0';
             * string strOutput = shell.cmd("", 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 + @".*\(c.*\)" + 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);
        }
コード例 #4
0
        public DevicePrompts detectPrompt(string unknownPrompt)
        {
            string        strTemp = "", strOutput = "";
            char          LastPromptChar = '\0';
            DevicePrompts prompts        = new DevicePrompts();

            //Get full Prompt
            strOutput = shell.cmd("", unknownPrompt);
            if ((strOutput == null) || (strOutput.Length == 0))
            {
                //Found no Prompt Exit...
                prompts = null;
                return(null);
            }
            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
                    prompts.DevicePrompt = HelperClass.quotemeta(strTemp);
                }
            }
            prompts.DevicePromptString = prompts.DevicePrompt;
            if ((LastPromptChar == '#') || (DeviceType.Equals("H3C")) || (DeviceType.Equals("HP") && (LastPromptChar == ']')))
            {
                //EnableMode = true;

                if (prompts.DevicePrompt.Length < 10)
                {
                    prompts.DevicePromptConfig = prompts.DevicePrompt.Substring(0, prompts.DevicePrompt.Length);
                }
                else
                {
                    prompts.DevicePromptConfig = prompts.DevicePrompt.Substring(0, 10);
                }
                if (prompts.DevicePromptConfig.EndsWith("\\"))
                {
                    prompts.DevicePromptConfig = prompts.DevicePromptConfig.Substring(0, prompts.DevicePromptConfig.Length - 1);
                }

                prompts.DevicePromptConfig = prompts.DevicePromptConfig + LastPromptChar;
                //DevicePromptConfig =~ s/(.*)(.)$/$1.*\\(config.*\\)$2/;	# s/(.*)([^\s])\s?$/$1.*\\(config.*\\)$2/;
                if ((DeviceType.Equals("HP") && (LastPromptChar == ']')) || DeviceType.Equals("H3C"))
                {
                    prompts.DevicePromptConfig = prompts.DevicePromptConfig.Substring(0, prompts.DevicePromptConfig.Length - 1) + ".*-.*" + LastPromptChar;
                    //DeviceType = "H3C";
                }
                else if (DeviceType.Equals("HP"))
                {
                    prompts.DevicePromptConfig = prompts.DevicePromptConfig.Substring(0, prompts.DevicePromptConfig.Length - 1) + ".*\\(.*\\)" + LastPromptChar;
                }
                else
                {
                    prompts.DevicePromptConfig = prompts.DevicePromptConfig.Trim();
                    //DevicePromptConfig = DevicePromptConfig.Substring(0, DevicePromptConfig.Length-1) + ".*\\(config.*\\)" + "#";
                    prompts.DevicePromptConfig = prompts.DevicePromptConfig.Substring(0, prompts.DevicePromptConfig.Length - 1) + ".*\\((?:c.*|.*-config)\\)" + LastPromptChar;
                }
                prompts.DevicePromptConfig = prompts.DevicePromptConfig + "[ \\r\\n]*$";
            }
            else
            {
                prompts.DevicePromptConfig = "";
            }

            prompts.DevicePrompt   = prompts.DevicePrompt + LastPromptChar + "[ \\r\\n]*$";
            prompts.LastPromptChar = LastPromptChar;
            if (_Debug > 0)
            {
                Console.WriteLine("\nDebug-Info:");
                Console.WriteLine("new Prompt: " + prompts.DevicePrompt);
                Console.WriteLine("Config-Prompt: " + prompts.DevicePromptConfig);
            }
            return(prompts);
        }