This project is read-only.

Using ShellStream and readline gives me double prompts

Aug 9, 2012 at 9:57 PM

Real Simple vb.net program. Using vb.net 2008. One button  and showing output in immediate window. The version I am using: Renci.SshNet v2.0.50727

Private Sub BtnAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAll.Click
        Dim Client1 As Renci.SshNet.SshClient
        Client1 = New Renci.SshNet.SshClient("100.0.0.118", "test", "test")
        Client1.Connect()

        Dim line As String
        Dim stream = Client1.CreateShellStream("dumb", 80, 24, 800, 600, 1024)

        Dim reader = New StreamReader(stream)
        Dim writer = New StreamWriter(stream)
        writer.AutoFlush = True         'Flush buffer after every write

        line = reader.ReadToEnd
        Debug.Print(line)
        While (stream.Length = 0)
            Threading.Thread.Sleep(500)
        End While

        writer.WriteLine("ls")
        Debug.Print("after write ls")
        While (stream.Length = 0)
            Threading.Thread.Sleep(500)
        End While

        Debug.Print("before read")


        '***** Try A  '*****
        '  line = reader.ReadToEnd
        '  Debug.Print(line)
        '  Debug.Print("after read")


        '***** Try B  '*****
      
        line = reader.ReadLine

        While (Not line Is Nothing) 'Nothing = Null in C#
            Debug.Print(line)
            line = reader.ReadLine()
        End While
        Debug.Print("after read")


        Client1.Disconnect()

    End Sub


This is the output:
 
Ubuntu 10.04.3 LTS

Welcome to the Ubuntu Server
Last login: Thu Aug  9 15:36:11 2012 from 100.0.0.118


after write ls
before read
test@ubuntu:~$
ls
examples.desktop
test@ubuntu:~$
test@ubuntu:~$
after read



Why are there 2 test@ubuntu:~$ being returned with the readline?

Thanks for any input.

Aug 10, 2012 at 7:09 PM

Hi,

 

My only guess at this point is may be you passing 2 new lines and in return shell returns you to prompts.

It also could be that I forget to clear a buffer from previous calls but if I remember correctly I handled this problem already.

 

Hope it helps,

 

Thanks,

Oleg

Jan 26, 2014 at 7:23 PM
I have this problem too, any solutions yet?
Apr 6, 2014 at 4:48 PM
Try using the 2014.4.6-beta1 release.
I fixed a related issue.
Marked as answer by drieseng on 4/6/2014 at 9:04 AM
Jan 16, 2015 at 6:41 PM
drieseng wrote:
Try using the 2014.4.6-beta1 release.
I fixed a related issue.
I have tried both beta1 and beta2 of 2014.4.6, but I still receive double prompts. Help me, please.
Thank you and sorry for my terrible English
Jan 17, 2015 at 12:11 PM
duclm wrote:
drieseng wrote:
Try using the 2014.4.6-beta1 release.
I fixed a related issue.
I have tried both beta1 and beta2 of 2014.4.6, but I still receive double prompts. Help me, please.
Thank you and sorry for my terrible English
Please show us some code. Especially the parts with writes and reads to/from the stream.

It's very likely you also echo the user input, which is not neccesary since the ssh server sends back your writes as shell output.
See for example: http://sshnet.codeplex.com/discussions/576135 (Second comment by ManuelRitter)
Jan 17, 2015 at 2:00 PM
Here is my code:
var command = txtCommand.Text;
            try
            {

                writer.AutoFlush = true;

                var text = reader.ReadToEnd();
                writer.WriteLine(command);
                while (stream.Length == 0)
                {
                    Thread.Sleep(500);
                }
                
                text = reader.ReadToEnd();
                txtResult.AppendText(text);

                txtCommand.Text = "";
                txtCommand.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
And this is my application:

Image

I want to create a windows form application like Xshell but I don't know how to bring command line to form in windows form. So I create a fake command line and write command and result to black textbox.

Thank you for helping me :)
Jan 17, 2015 at 3:54 PM
The problem comes from the StreamWriter() you use.
Use writer.Write(cmd + "\n") or use the WriteLine of the ShellStream.

I suggest you remove your StreamWriter() and StreamReader() and use the methods included in SshShellStream instead.
Jan 17, 2015 at 4:47 PM
Thank you so much !

I have succeeded with cmd + "\n".

Can you give me an example using method included in SshShellStream?
Jan 17, 2015 at 4:56 PM
var shell = new SshShellStream(... here your shell config ...);
var reader = new StreamReader(shell);
shell.WriteLine(cmd);
... wait for output ...
reader.ReadToEnd()
Jan 17, 2015 at 5:10 PM
Edited Jan 17, 2015 at 5:11 PM
Thank you so much :)

P/s: I think CreateShellStream, not SshShellStream :)