StreamReader content and non printable characters

Dec 23, 2014 at 2:12 PM
Hello Comminuty,

i have 2 questions.

1) I use SSH.NET for Shell connection to SSH hosts with
public class Connector
    {
        SshClient ssh;
        StreamReader read;
        StreamWriter write;
        ShellStream shell;
        Thread readerThread;

        public Connector(string ip, string user, string password)
        {
            Console.CancelKeyPress += Console_CancelKeyPress;
            ssh = new SshClient(ip, user, password);
            readerThread = new Thread(new ThreadStart(Reader));
            readerThread.Start();
            Connector2();
        }

        void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            write.Write('\x003');
        }
        public void Connector2()
        {
            ssh.Connect();
            shell = ssh.CreateShellStream("vt100", 80, 24, 800, 600, 1024);
            read = new StreamReader(shell);
            write = new StreamWriter(shell);
            Console.TreatControlCAsInput = true;
            do
            {
                write.Write(Console.ReadKey().KeyChar);
                write.Flush();

            } while (ssh.IsConnected);
        }

        private void Reader()
        {
            while (true)
            {                
                System.Threading.Thread.Sleep(10);
                if(!ssh.IsConnected) continue;
                if (read == null)
                    continue;
                string result = read.ReadToEnd();
                if (string.IsNullOrEmpty(result))
                    continue;
                Console.Write(result);
            }
        }
    }
But StreamReader not only returns console output, it also returns my input.
For example, i type "ping 8.8.8.8" and got "ppiinngg 88..88..88..88" in console window. It is not an functionality issue, its more optical issue.

2) Like point 1, its more optical issue. I have sometimes non printable characters form a MikroTik router, but never had non printable characters when using putty oder something else.
♥←[K    ←[m←[32msent=←[m5 ←[m←[32mreceived=←[m5 ←[m←[32mpacket-loss=←[m0% ←[m←[3
2mmin-rtt=←[m3ms ←[m←[32mavg-rtt=←[m3ms ←[m←[32mmax-rtt=←[m3ms
In putty it the same link is
    sent=5 received=5 packet-loss=0% min-rtt=2ms avg-rtt=2ms max-rtt=3ms
Any solutions for this?

Regards
Manuel
Dec 23, 2014 at 3:13 PM
Edited Dec 23, 2014 at 3:14 PM
Solved the first issue by supressing key output on console with
write.Write(Console.ReadKey(true).KeyChar);
so every output comes from StreamReader.
Dec 23, 2014 at 5:50 PM
Edited Dec 23, 2014 at 5:52 PM
Hi,

to 1) Yes, thats expected behavior. The SSH server sends back your input, cause it's also output on the shell.

to 2) You have to handle the terminal protocol. SSH.NET just gives you access to the shell stream.
You don't see those chars in putty, cause it handles the terminal protocols. In your case vt100.
Dec 24, 2014 at 8:30 AM
Hello da_rinkes,

thanks for your explanation.
Now i got another question. I have one type of machines where i had no luck to connect via SSH.NET but putty has no problem.
SSH.NET says "Renci.SshNet.SshOperationTimeoutException" when i call the "Connect" method of SshClient.

I checked both, putty and SSH.NET connection via Wireshark and SSH.NET stuck before Key Exchange

SSH.NET:
32625 1429.943738000 172.20.0.138 10.30.0.4 TCP 66 40283→22 [SYN] Seq=0 Win=65535 Len=0 MSS=1460 WS=4 SACK_PERM=1
32626 1429.959065000 10.30.0.4 172.20.0.138 TCP 66 22→40283 [SYN, ACK] Seq=0 Ack=1 Win=5840 Len=0 MSS=1460 SACK_PERM=1 WS=16
32627 1429.959157000 172.20.0.138 10.30.0.4 TCP 54 40283→22 [ACK] Seq=1 Ack=1 Win=137072 Len=0
32629 1429.979514000 10.30.0.4 172.20.0.138 SSHv2 70 Server: Protocol (SSH-2.0-SSH.1.0)
32630 1429.989451000 172.20.0.138 10.30.0.4 SSHv2 92 Client: Protocol (SSH-2.0-Renci.SshNet.SshClient.0.0.1)
PuTTy:
33421 1474.605012000 172.20.0.138 10.30.0.4 TCP 66 40300→22 [SYN] Seq=0 Win=8192 Len=0 MSS=1460 WS=256 SACK_PERM=1
33422 1474.618777000 10.30.0.4 172.20.0.138 TCP 66 22→40300 [SYN, ACK] Seq=0 Ack=1 Win=5840 Len=0 MSS=1460 SACK_PERM=1 WS=16
33423 1474.618874000 172.20.0.138 10.30.0.4 TCP 54 40300→22 [ACK] Seq=1 Ack=1 Win=65536 Len=0
33424 1474.628474000 10.30.0.4 172.20.0.138 SSHv2 70 Server: Protocol (SSH-2.0-SSH.1.0)
33425 1474.628878000 172.20.0.138 10.30.0.4 SSHv2 82 Client: Protocol (SSH-2.0-PuTTY_Release_0.63)
33426 1474.628949000 172.20.0.138 10.30.0.4 SSHv2 726 Client: Key Exchange Init
33427 1474.638400000 10.30.0.4 172.20.0.138 TCP 60 22→40300 [ACK] Seq=17 Ack=29 Win=5840 Len=0
33428 1474.648490000 10.30.0.4 172.20.0.138 TCP 60 22→40300 [ACK] Seq=17 Ack=701 Win=8768 Len=0
33429 1474.649245000 10.30.0.4 172.20.0.138 SSHv2 302 Server: Key Exchange Init
33430 1474.697866000 172.20.0.138 10.30.0.4 TCP 54 40300→22 [ACK] Seq=701 Ack=265 Win=65280 Len=0
33449 1475.809952000 172.20.0.138 10.30.0.4 SSHv2 198 Client: Diffie-Hellman Key Exchange Init
33450 1475.851027000 10.30.0.4 172.20.0.138 TCP 60 22→40300 [ACK] Seq=265 Ack=845 Win=10112 Len=0
33496 1477.154579000 10.30.0.4 172.20.0.138 SSHv2 694 Server: Diffie-Hellman Key Exchange Reply
33497 1477.173475000 172.20.0.138 10.30.0.4 SSHv2 70 Client: New Keys
33498 1477.185717000 10.30.0.4 172.20.0.138 SSHv2 70 Server: New Keys
With putty i got that the server uses "diffie-hellman-group1-sha1" as key-exchange algorithm, so i call
            ssh.ConnectionInfo.KeyExchangeAlgorithms.Clear();
            ssh.ConnectionInfo.KeyExchangeAlgorithms.Add("diffie-hellman-group1-sha1", typeof(Renci.SshNet.Security.KeyExchangeDiffieHellmanGroup1Sha1));
The Server says nothing in logfiles and since it is a proprietary router firmware, i have no chance to extend logging or something on the server.
The only settings i can change are
ssh server : enable / disable
ssh authtype : pap / chap
ssh version : sshv1 / sshv2

Actually it is
ssh server : enable
ssh authtype : pap
ssh version : sshv2


Thanks in advance
Manuel
Coordinator
Dec 26, 2014 at 3:37 PM
Manuel,

The connection problem you reported is caused by two stubborn SSH implementations.
Both SSH.NET and your router are waiting for the SSH_MSG_KEXINIT of the other party before sending the SSH_MSG_KEXINIT messages themselves.
Please submit an issue for SSH.NET.

Thanks!
Gert
Jan 5, 2015 at 7:18 AM
Hello Gert,

i submited an issue as you proposed, thanks for your help.

Manuel