private void OnStandardDataReceived(object sender, StandardDataReceivedEventArgs e)
 {
     if (StandardDataReceived != null)
     {
         StandardDataReceived(sender, e);
     }
 }
        private void session_StandardDataReceived(object sender, StandardDataReceivedEventArgs e)
        {
            if (e.Type == StandardStreamType.OUTPUT)
            {
                if (Status != ConsoleStatus.ACCESSGRANTED)
                {
                    if (e.Data == "login as: ")
                    {
                        Status = ConsoleStatus.LOGINAS;
                    }
                    else if (e.Data.EndsWith("'s password: "******"Passphrase for key "))
                    {
                        Status = ConsoleStatus.PRIVATEKEY;
                        OnPasswordRequested();
                    }

                    StandardOutput += e.Data;
                }
                else
                {
                    char[] data = new char[e.Data.Length];
                    int    read = 0;

                    bool gotESC = false;
                    bool gotOSC = false;
                    bool gotCSI = false;

                    for (var i = 0; i < e.Data.Length; i++)
                    {
                        char c = e.Data[i];

                        if (gotOSC)
                        {
                            if (c == 7) // BEL
                            {
                                gotOSC = false;
                            }
                        }
                        else if (gotCSI)
                        {
                            if (c >= 64 && c <= 126) // @ to ~
                            {
                                gotCSI = false;
                            }
                        }
                        else if (gotESC)
                        {
                            if (c == 91) // [
                            {
                                gotCSI = true;
                            }
                            else if (c == 93) // ]
                            {
                                gotOSC = true;
                            }

                            gotESC = false;
                        }
                        else if (c == 27) // ESC
                        {
                            gotESC = true;
                        }
                        else
                        {
                            data[read++] = c;
                        }
                    }

                    string output = new string(data, 0, read);
                    if (output.EndsWith("Password: "******"[sudo] password for "))
                    {
                        OnPasswordRequested();
                    }

                    StandardOutput += output;
                }
            }

            else if (e.Type == StandardStreamType.ERROR)
            {
                if (Status != ConsoleStatus.ACCESSGRANTED)
                {
                    string[] lines = e.Data.Split(_splitby, StringSplitOptions.RemoveEmptyEntries);

                    if (Array.IndexOf(lines, "Access granted") >= 0)
                    {
                        Status = ConsoleStatus.ACCESSGRANTED;
                        _session.Connected();
                    }
                    else if (Array.IndexOf(lines, "Store key in cache? (y/n) ") >= 0)
                    {
                        Status = ConsoleStatus.STOREHOST;
                    }
                    else if (Array.IndexOf(lines, "Update cached key? (y/n, Return cancels connection) ") >= 0)
                    {
                        Status = ConsoleStatus.UPDATEHOST;
                    }
                    else if (Array.Find <string>(lines, line => line.StartsWith("FATAL ERROR:")) != null)
                    {
                        Status = ConsoleStatus.ERROR;
                    }
                }

                StandardError += e.Data;
            }
        }