MinimalisticTelnet.TelnetConnection.Login C# (CSharp) Method

Login() public method

public Login ( string Username, string Password, int LoginTimeOutMs ) : string
Username string
Password string
LoginTimeOutMs int
return string
        public string Login(string Username,string Password,int LoginTimeOutMs)
        {
            int oldTimeOutMs = TimeOutMs;
            TimeOutMs = LoginTimeOutMs;
            string s = Read();

            if (!s.TrimEnd().EndsWith(":"))
               throw new Exception("Failed to connect : no login prompt");

            WriteLine(Username);

            s += Read();
            if (!s.TrimEnd().EndsWith(":"))
                throw new Exception("Failed to connect : no password prompt");

            WriteLine(Password);

            s += Read();
            TimeOutMs = oldTimeOutMs;

            return s;
        }

Usage Example

Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //create a new telnet connection to hostname "gobelijn" on port "23"
            TelnetConnection tc = new TelnetConnection("gobelijn", 23);

            //login with user "root",password "rootpassword", using a timeout of 100ms, and show server output
            string s = tc.Login("root", "rootpassword",100);
            Console.Write(s);

            // server output should end with "$" or ">", otherwise the connection failed
            string prompt = s.TrimEnd();
            prompt = s.Substring(prompt.Length -1,1);
            if (prompt != "$" && prompt != ">" )
                throw new Exception("Connection failed");

            prompt = "";

            // while connected
            while (tc.IsConnected && prompt.Trim() != "exit" )
            {
                // display server output
                Console.Write(tc.Read());

                // send client input to server
                prompt = Console.ReadLine();
                tc.WriteLine(prompt);

                // display server output
                Console.Write(tc.Read());
            }
            Console.WriteLine("***DISCONNECTED");
            Console.ReadLine();
        }
All Usage Examples Of MinimalisticTelnet.TelnetConnection::Login