This project is read-only.

Using shell with a live stream

Feb 10, 2012 at 2:06 AM

Hi Everyone,

I'm trying to use the shell function such that I can start a shell and connect it to a input/ output stream. Then When the user wishes to execute soemthing I add the command to the stream. When the user is complete I close the shell. Problem is It seems when I add soemthing to the input streamnothing happens. I'm thinking its something silly I am doing with the stream handling, could I ask for your help?

I have attached some example code showing that I make a connection then have some code in a function which gets executed over and over by the user to add commands to the stream and execute these commands, the I stop the shell. In this example I tried using a command such as "ls >> test.txt" which when executed should create a file on the remote machine, but it does not. I CAN get this working though if I fill the input stream before I start the shell, but this will not work for my requirements.

command = "ls >> test.txt"
pr = New SshClient(_server, 22, _user, _password)
pr.Connect()

termInput = New IO.MemoryStream()
termOutput = New IO.MemoryStream()
sshshell = pr.CreateShell(termInput, termOutput, termOutput, "xterm", 80, 24, 800, 600, "")
sshshell.Start()

'This code is in a seperate function and would be ran as much times as needed but with different input to "command"
Dim writer As IO.StreamWriter = New IO.StreamWriter(termInput, Encoding.ASCII)
Dim reader = New IO.StreamReader(termOutput, Encoding.ASCII)
writer.WriteLine("cd " & _directory)
writer.WriteLine(command)
writer.WriteLine("exit")
writer.Flush()
writer.Close()
' End of the function to send commands

sshshell.Stop()

This example is written in VB.net, but answers in C# or VB will work for me. I am familur with both and converting between the two.

Thanks,
-Stewart

Feb 10, 2012 at 2:22 AM

Having pretty much the same issue Stewart (also in VB myself).

Hopefully Oleg or someone else can come to the rescue.

Feb 10, 2012 at 2:31 AM

Hey guys,

 

Sorry I was pretty busy with other project.

 

It sounds to me that need redo or review at least Shell functionality.

I will try to start it tomorrow, if I will have time but meanwhile it seems there is a need for two features:

1. Some kind of live stream that you can read and write into it, correct? I just thought about it and I guess I could create new stream something like ShellStream which will do this work. Let me know what you think and if this kind of stream will work.

2. Ability to write and read string from Shell, but I think if I will implement first feature correctly then this one will be implemented too, I just know someone brought it up.

 

I will open a new discussion about Shell so everybody could add in there what they think they need and I ill see how and when I can implement it.

 

Thanks,

Oleg

Feb 10, 2012 at 2:36 AM

Right on Oleg, thanks a bunch!!!

Trying to use the Shell method to be able to:

1. Read and work with the output stream from the session

2. Send multiple commands through an input stream at different times

3. Keep the session alive and interactive (for #2)

Thanks again! Much appreciated!

 

 

Feb 10, 2012 at 4:21 AM

Perfect Oleg,

As DestroyerOfWorlds (nice name by the way) mentioned, my requirements are the same. Also as a test critiera maybe make sure it works with commands that span a session, such as sudo or su. Both commands are known to have problems with RunCommand (reason I am using shell).

Optionaly it would be nice to have an interface to start the shell, executeCommandOverShell, and stop shell. while shell is running it has an event to report output. This would then remove requirements to use streams. Would also be simple to use like runcommand, but allow for running multiple commands on same session - effectivly removing the problem runcommand has with sudo, su, etc.

 

Regards,
-Stewart