This project is read-only.

SSH.NET and telnet

Mar 21, 2012 at 3:23 PM

Hello

Is there any possibility to establish and process telnet session  using SSH.NET library (and all its method like expect etc)?

It would be convenient

Mar 21, 2012 at 3:25 PM

Hi,

 

Yes,

 

Please take a look at ShellStream class and Shell discussion in this forum.

 

Thanks,

Oleg

Mar 22, 2012 at 7:53 PM
Edited Mar 22, 2012 at 8:02 PM

I'm sorry but I can't find anything about telnet session. I tried:

..

         using (var telnetses = new SshClient("127.0.0.1", 23, "login","password"))
                     {
                telnetses.Connect();

..

..

and console opens but nothing is displayed - it looks as it hangs.  When I use putty it works as I started all necessary serrvices on  my computer (with windows 7)

When I use Ip adress of real telnet server (not my loopback) there is a error in telnetses.Connect(); line:


"Server string is null or empty."

Mar 23, 2012 at 2:05 PM

Hi,

Like I mentioned earlier, you need to use ShellStream for shell like functionality.

 

You will not see anything on the screen since library by itself does not show any output anywhere.

 

Thanks,

Oleg

Mar 23, 2012 at 7:02 PM

Please, give me an example. There is no documentation about ShellStream in SshClient.chm file

The code which I test (created on the basis of discussion which I read on this forum) is below:

using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.SshNet;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {



        static void Main(string[] args)
        {


            string buffer;

            var connectionInfo = new PasswordConnectionInfo("10.10.10.6", 22, "norbert", "mypass");

            using (var ssh = new SshClient(connectionInfo))
            {
                ssh.Connect();


                using (ShellStream stream = ssh.CreateShellStream("dumb", 80, 24, 800, 600, 1024))
                {
                    var reader = new StreamReader(stream);
                    var writer = new StreamWriter(stream);
                    writer.AutoFlush = true;

                    stream.Expect(new ExpectAction[] {
		new ExpectAction("pozcs", (a)=>{Console.Write("logged to pozcs!\n"+a);}),new ExpectAction("dcscs1", (s)=>{Console.Write("logged to dcscs!\n"+s);})
	});


                    while (stream.Length == 0)
                    {
                        Thread.Sleep(500);
                    }

                    //  Simple line read
                    var line = reader.ReadLine();
                    while (line != null)
                    {
                        Console.WriteLine(line);
                        line = reader.ReadLine();
                    }

                    writer.WriteLine("ls -l");


                    while (stream.Length == 0)
                    {
                        Thread.Sleep(500);
                    }


                    line = reader.ReadLine();
                    while (line != null)
                    {
                        Console.WriteLine(line);
                        line = reader.ReadLine();
                    }
                }
                Thread.Sleep(50000);
                ssh.Disconnect();
            }


        }
    }
}

 

How should I change this code to connect via telnet?
And addictional question - Is it any way to specify maximum amount of time which Expect should wait for incoming  pattern?

I tried to use exemples from http://sshnet.codeplex.com/discussions/301739  but it doesn't work i.e:

 

buffer = shellStream.Expect(userPromptMatch, 5000, LineMatchModes.Partial);
Apr 13, 2012 at 2:28 PM

Hi,

 

Sorry for delay in asnwer.

Currently there is no timeout, sorry, I implemented this feature too quick and many options are still not there, you can open an issue for that so when I have more time I could get back to it.

As far as an example at the bottom, it does not work because I refactored it to use ExpectAction, it was an early attempt to implement an expect functionality.

 

Also, in the code above, you dont have to user reader and writer, for the most part ShellStream functionality should be enough.

 

Hope it helps,

Thanks,

Oleg