This project is read-only.

VB.NET Framework 3.5 and Renci ssh - keep session open

Apr 13, 2014 at 4:25 PM
Hi everyone,

I'm using Renci SSH within my project and wondering how I can pass multiple commands and keep the session open.

I'm just testing things at the moment, but I'd like to navigate to a directory "cd /home/temp/" with one command, then after that has run, do something like "ls -l" and provide the results in a label or text box.

My code at present is as follows...
    Private Sub btnSSHTest_Click(sender As Object, e As EventArgs) Handles btnSSHTest.Click
        'Create the objects needed to make the connection'

        Dim connInfo As New Renci.SshNet.PasswordConnectionInfo(txtConnectionNode.Text, txtboxUsername.Text, txtboxPassword.Text)

        Using client As New Renci.SshNet.SshClient(connInfo)

            Dim cmd As Renci.SshNet.SshCommand
            Dim result As String
            Dim result2 As String

            cmd = client.CreateCommand("cd /home/temp/")
            result = cmd.Execute
            cmd = client.CreateCommand("ls -l")
            cmd.CommandTimeout = TimeSpan.FromSeconds(10)
            result = cmd.Execute
            result2 = cmd.Error
            TextBox1.Text = cmd.ExitStatus

            If String.IsNullOrEmpty(result2) Then
                TextBox1.Text = result2
            End If

            TextBox1.Text = result

        End Using

    End Sub
But the ls -l command just doesn't recognise that the cd /home/temp/ command has been run. Am I missing something? Does the SSH not work this way?
Apr 13, 2014 at 5:14 PM
SshCommand opens a new shell for every command, so your commands are not in the same context.
In your case you can simply combine the commands by using "&&", "||" or ";".

e.g. client.CreateCommand("cd /home/temp/ && ls -l")
Apr 15, 2014 at 6:03 PM
Ah, that's a shame as I want to log into an ssh session and pass multiple db2 commands based on an original db2 query. I can do it and store it in a variable but it means multiple log ins and this is around 200 statements.

Shame, I wonder if there are any other ssh projects for that keep the session open. Do you know any?

Thanks for your help already though
Apr 15, 2014 at 6:32 PM
Edited Apr 15, 2014 at 6:34 PM
You understand me wrong. My solution works fine for your question.
You don't have to relogin for every command.
Open the session, login and run your commands.
But if your commands need the same shell context you have to combine them in one command or use a ShellStream.

If you want to talk to a database, why do you need the same shell for the commands?
Why not forward your database with port forwarding and talk to the database directly
instead of invoking shell commands?
Apr 16, 2014 at 5:11 AM
Hi da_rinkes,

I have a similar problem how can i access the SSHCLIENT in my C# different forms.

Am i supposed to open new connection in each form, or how that works please help me.

Apr 16, 2014 at 6:55 AM
avinashav wrote:
Hi da_rinkes,

I have a similar problem how can i access the SSHCLIENT in my C# different forms.

Am i supposed to open new connection in each form, or how that works please help me.

Your problem is that you are missing basic C#, .Net and WindowsForms knowledge.
I can't help you here, since I'm not a teacher. And your problem has nothing to do
with SSH or SSH.NET. And please stop spamming the Issues and Discussions.
I think should work perfect for you.
Apr 16, 2014 at 2:53 PM

I understand your problem. But your question (without knowing what you are trying to do) sounds almost like:

Q: "How do I build a house?"

I mean, Renci is like a wrench in your toolbox as you build the house, and the rest of the house is the C# form. With Renci being a library, it is more like a tool, just like visual studio is a tool. You can't run visual studio and do anything, but you can make an application that does.

So think of libraries, includes/imports, DLLs, etc. as nothing more than tools, or even blueprints to build your house (application). If you have not built many houses, you might not be sure just yet that SSH.NET is even the tool you need, as there are others you can use.

Building a house from scratch, in visual studio, is not hard, but it takes time for a learning curve, and time to actually write, test, etc. to make sure it meets your original requirements. For some like me, the act of figuring out which tools work best, and even doing more that you thought possible with tools you learn about, is the fun (yes, fun!) of programming.

If you are where you seem to be with programming, then you have a lot of great times ahead of you! If you are a very new beginner, start with the free tutorials on MSDN/Microsoft site, or sometimes the 'trails' that take you on a learning curve, or like me just buy all the books on the subject and mark hem up in pen & marker!

If, however, you are not new to programming, then is a good place to post your question, and see what comes up from searching as well.

I hope you see why asking the forum that makes a hammer how to use it to build a house might be more difficult for us to help you with, and asking on a forum where they build applications like the one you seek to create would be a better use of your time.

However, once your application is in process, and you are trying to accomplish something SSH.NET should be able to do per it's docs, or other forum posts here & elsewhere, and you can't make it work like you think it should, then that question will be much easier to answer here.

I hope this isn't seen as a negative post, but a positive one trying to save you frustration and get you to where you will find the answers you seek.

Sound good?



Apr 17, 2014 at 11:32 AM
Hi BogusException,

Thanks alot for your concern, really your response is a positive boost for me.

I am new to programming, was bit confused..!!!

Thanks alot, I will use those websites for learning.

Thank you very much, :)

Really I was upset by da_rinkes response. :(

Apr 17, 2014 at 12:34 PM
Yeah, right. I'm the bad boy here, telling you the truth and also helped you with some basics... how unfriendly, I'm a really terrible person! :/
Trying to hijack threads, open issues and discussions with the same text, including pasting my example code... and now you are upset. whao

BogusException: Wish you good luck here, with this kindergarten! I won't answer anything here the next time.
Apr 17, 2014 at 8:28 PM
I'd hate to lose you over this, and I'm sure all members of this little community share this feeling.
Please contact me if you want to discuss this further.
Apr 17, 2014 at 8:45 PM
Wow guys, come on, let's not get out of hand here, it's a forum where people's words can be taken out of context quite easily.
@avinashav... Don't get upset over the simplest thing or you'll never do well on forums. Your question was quite vague and I'm not sure how it fit with my original question as you haven't even got your form to use the ssh yet! Start a new thread for things like that.

@da_rinkes... You gave me some great advice and I've used the shellstream command now to great effect, it does what I'm after and much more! It sounds like you give great advice and I'm very grateful for the help you have given me on this. Thank you!

I do have a follow up question... Rather than the thread.wait, is there a way to wait for a command to come back? I'm presuming not but I'm very hopeful I've missed something and there is a command to wait for a response.

Thanks again guys