This project is read-only.

VB.net SSH client with VB.net

Sep 1, 2014 at 2:24 PM
Hi -
( my VB is mot the best ... so if response is in C# ..then i'm goosed !)
I want to write a vb app that will :
  1. Connect to a device through SSH.
  2. Send a list commands and wait for the response of each command, before sending the next.
    I'd like each response to go to a textbox.
I've seen sharpssh which I can get to work - kind of - but I have to put in the expected prompt for each command ... I don't want to do that .. I just want to see the full response in the textbox ... before I send the next command.

I've also seen renci .. but for some reason cant get that to work

For sharpssh - I don't want to have to enter something for sshstr.prompt .. I just ant to see the full responce..

any ideas how I could do this ?
thanks



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim result As String
    Dim sshstr As New SshStream("10.226.145.108", "super", "super")
    TextBox1.Text = "Connecting ...."
    Me.Refresh()

    sshstr.Prompt = "TSM"

    '//Remove terminal emulation characters
    sshstr.RemoveTerminalEmulationCharacters = True

    TextBox1.Text = sshstr.ReadResponse()
    TextBox1.Select(TextBox1.TextLength, 0)
    TextBox1.ScrollToCaret()
    MsgBox(" 1")
    System.Threading.Thread.Sleep(5000)
    Threading.Thread.Sleep(1000)
' command # 1
    sshstr.Write("/c 1")
    TextBox1.AppendText("/c 1")
    TextBox1.Select(TextBox1.TextLength, 0)
    TextBox1.ScrollToCaret()
    Me.Refresh()
    System.Threading.Thread.Sleep(2000)
     sshstr.Prompt = ""
    result = sshstr.ReadResponse()

    TextBox1.AppendText(result)
    TextBox1.Select(TextBox1.TextLength, 0)
    TextBox1.ScrollToCaret()
    Threading.Thread.Sleep(1000)
' command # 2
    sshstr.Write(Chr(24))
    sshstr.Write(Chr(13))
    TextBox1.AppendText("Chr(24)")
    TextBox1.Select(TextBox1.TextLength, 0)
    TextBox1.ScrollToCaret()
    Me.Refresh()
    sshstr.Prompt = "TSM"
    result = sshstr.ReadResponse()
    TextBox1.AppendText(result)
    TextBox1.Select(TextBox1.TextLength, 0)
    TextBox1.ScrollToCaret()
    sshstr.Close()

    ' //Reading from the SSH channel

End Sub

Sep 1, 2014 at 8:43 PM
Hi bgengine,
You ask for something that is difficult. You are asking that the SSH Client somehow know that the server is done with a command, but you won't tell it how to recognize the output when it is done. Suppose a command takes a few seconds, or outputs some, waits, outputs more, etc.?

[IMHO] The only way you can consistently do this is with one of the following, or a combination:
  1. You tell the client what to look for in the return stream that signals the command is complete (this depends on the server quite a bit, as they all return something different-even based on the command(s) given), or
  2. You give it a timeout period. "After 10 seconds, I am assuming you have the output, and I can send more. If not, oh well!"
I don't use all kinds of ssh servers, so I can't give you advise. But if your server somehow 'echoes' back it's input, or you know the prompt it will return, that all helps. Also, another 'gotcha' is if you enter &nix-style commandS <-plural on the command line, separated by ;. Is that one command, or multiple? What about pipes and redirects? What about timed/cycle or 'tail' style commands that run until you cancel them? And what about rapid-fire/batch commands send as multiple lines?

You see, from the program's perspective, it can only recognize the end based on what you tell it it should recognize as the end of a command.

If you find a way to compensate/prepare for all of that, I would love to see it! :)

pat
:)
Sep 2, 2014 at 9:24 AM
Thanks Pat for your response,

Ok _ I think I understand the issue ..

I don't fully understand ssh so please forgive my ignorance.. but I was wondering is it possible to put a timer on the ssh buffer ( if such a buffer exsists ?)
so that if the buffer has something in it - then read it and put it in the text box ....
When a command is sent - continually check the incoming buffer and dump the contents to the text box ... until the buffer is empty


Perhaps this is not possible ... I did something like this with a serial port buffer some time ago but I know very little about ssh..
regards
Sep 2, 2014 at 6:21 PM
@bgengine,

I might suggest start messing with it to start.

I have posted many VB examples here. I will leave it to the experts to say for sure, but networking protocols don't talk the way hard-wired ones do. The buffer you're talking about in the serial world, when talking about the network world, is not a static thing that you can say "OK, it's empty". There is no way of knowing if another packet will arrive, and the 'buffer' be filled again.

I have described my approaches here, but basically there are 2 approaches (I alluded to them earlier). The subtlety being whether you know what to expect or not, and whether the response is really gig, or very small-or not at all. That alone can open a large discussion...

So what I do MOST of the time with a response I know will be large is this:
  1. I issue a command.
  2. I wait for the response (might take time, so I need to set a timer to decide "I give up waiting").
  3. If that timeout is not yet, I RESET an 'inactivity' timer to 0 every time I receive something.
  4. That inactivity timer also has a timeout period to say "I guess there is no more output!".
  5. As long as there is input, and the inactivity timer are below threshold, I continue to add to the input stream (the content I expected).
  6. At some point, one of the timers will expire, and I can't wait any longer. I put these timeouts in the Function() calls, as Optional with defaults.
  7. I return whatever I clumped together.
This works in my scenarios because I am not using SSH for files (I guess I could), but for text in/out, as in commands & responses. Most folks here are doing things much more exotic than I am-hell, I can't even figure out how to write tests! :)
  • So again, I suggest you put a small test project in your solution, and try (with Intellisense) a few options... And go back and read some of the excellent threads here where folks have posted their working code (like me) for your benefit. I didn't post it to help me, but to help you. :)
I think you will make much faster progress, and understand your choices for your scenario better. I chose this SSH util suite because it is current, useful & well written, and the support/forum/community is active. Sometimes I pose bizarre problems that nobody knows how to fix, but most times folks are eager to assist-probably a lot more so than on the elitist SO...

Go until you can't continue, try others' solutions, and when you get to a line/step you can't get past, post here.

pat
:)