CreateShell

Jan 12, 2012 at 2:33 AM

The documentation shows no examples and says CreateShell is incomplete.

Is there anywork arounds for CreateShell? All my commands are returning "stdin is not tty" even after calling CreateShell() and creating a "dumb" terminal.

I can parse the stdin is not a tty error and return from the .Error property, however, this is not a very good idea on my side.

 

Can you give me some assistance?

 

Coordinator
Jan 12, 2012 at 3:11 AM

Hi,

 

Sorry, I dont have full documentation yet :(:(

 

Here is an example of how I tested it when I developed this feature:

            using (var ssh = new SshClient(connectionInfo))
            {
                ssh.Connect();
                var input = new MemoryStream();  
                var sr = new StreamWriter(input);
                var output = Console.OpenStandardOutput();
                var shell = ssh.CreateShell(input, output, output, "xterm", 80, 24, 800, 600, "");
                shell.Stopped += delegate
                {
                    Console.WriteLine("\nDisconnected...");
                };
                shell.Start();
                sr.Write("su root");
                sr.Flush();
                Thread.Sleep(1000 * 1);
                sr.WriteLine("password");
                sr.Flush();
                Thread.Sleep(1000 * 100);
                shell.Stop();

I currently didnt run it but simply copy pased it from my old test so I hope it still works, but this is an idea of how I was planning to do it.

Hope it helps,

Please let me know if anything need to be changed or fixed, since this feature is very undeveloped, haven't touched since it was developed I think.

Thanks.

Oleg

Jan 12, 2012 at 3:13 AM

Thank you very much, and I appreciate the quick reply. I see now that I should be using the two streams I created to do the reading and writing to the shell. Thank you again! Hope you have a wonderful new year :)

Jan 25, 2012 at 1:19 AM
Edited Jan 25, 2012 at 3:34 AM

could you please show us how to read the output of what was entered?

the responce back i mean.

say i wanted to do a ls.

and read the file information back using the code provided above.

 

edit: figgured it out set a variable

Dim

test = ssh.RunCommand("ls")

msgbox(test.result) 

 

Nov 8, 2013 at 5:06 PM
Edited Nov 10, 2013 at 4:28 PM
I've been trying to get this to work with PowerShell, but haven't had any luck yet. I asked for help by posted to:
http://stackoverflow.com/questions/19864617/powershell-read-write-to-ssh-net-streams

Edit: I found the resolution to my problem at https://sshnet.codeplex.com/discussions/439210. I posted my solution at StackOverflow.
Nov 11, 2013 at 1:00 AM
Oleg,

I used your example to also create a solution:
 Function SendCommand(cmd As String, s As ShellStream) As String
        Try
            reader = New StreamReader(s)
            writer = New StreamWriter(s)
            writer.AutoFlush = True
            writer.WriteLine(cmd)
            While s.Length = 0
                Thread.Sleep(500)
            End While
        Catch ex As Exception
            Debug.WriteLine("SendCommand(" & cmd & ") caught exception: " & ex.ToString())
        End Try
        Return reader.ReadToEnd()
    End Function
    Function SendCommandW(cmd As String, s As ShellStream) As String
        Try
            reader = New StreamReader(s)
            writer = New StreamWriter(s)
            writer.AutoFlush = True
            writer.Write(cmd)
            While s.Length = 0
                Thread.Sleep(500)
            End While
        Catch ex As Exception
            Debug.WriteLine("SendCommandW(" & cmd & ") caught exception: " & ex.ToString())
        End Try
        Return reader.ReadToEnd()
    End Function
    Function SendCommandCR(cmd As String, s As ShellStream) As String
        Try
            reader = New StreamReader(s)
            writer = New StreamWriter(s)
            writer.AutoFlush = True
            writer.Write(cmd & vbCr)
            While s.Length = 0
                Thread.Sleep(500)
            End While
        Catch ex As Exception
            Debug.WriteLine("SendCommandCR(" & cmd & ") caught exception: " & ex.ToString())
        End Try
        Return reader.ReadToEnd()
    End Function
    Function SendCommandLF(cmd As String, s As ShellStream) As String
        Try
            reader = New StreamReader(s)
            writer = New StreamWriter(s)
            writer.AutoFlush = True
            writer.Write(cmd & vbLf)
            While s.Length = 0
                Thread.Sleep(500)
            End While
        Catch ex As Exception
            Debug.WriteLine("SendCommandLF(" & cmd & ") caught exception: " & ex.ToString())
        End Try
        Return reader.ReadToEnd()
    End Function
A little respect for the VB.NET folks!

VS 2012 Ultimate
Nov 22, 2013 at 4:22 PM
Hi All,

I need to switch to 'root' through a SSH session so I'm using Oleg's code above to try and run 'sudo su' within the SSH session. I was hoping that creating a shell would allow me to run the command. Alas I keep getting the dreaded 'Error: sudo: sorry, you must have a tty to run sudo'
                var input = new MemoryStream();
                var sw = new StreamWriter(input);
                var output = Console.OpenStandardOutput();
                var shell = client.CreateShell(input, output, output, "xterm", 80, 24, 800, 600, "");
                shell.Stopped += delegate
                {
                    Console.WriteLine("\nDisconnected...");
                };

                shell.Started += delegate
                {
                    sw.AutoFlush = true;

                    //  Start Running Commands
                    var cmd1 = client.RunCommand("sudo su");

                    if (cmd1.Error == null)
                    {
                        Console.WriteLine(cmd1.Result);
                    }
                    else if (cmd1.Error != null)
                    {
                        Console.WriteLine("Error: " + cmd1.Error);
                    }
                    shell.Stop();
                };

//  Start the tty shell
                shell.Start();
I would appreciate some assistance if possible.

I am re-directing the console output to a Text box if that makes a difference???
Nov 30, 2013 at 7:26 PM