This project is read-only.

Successful to connect linux server,but can't execute command.

Nov 15, 2010 at 1:59 PM
Edited Nov 16, 2010 at 1:30 AM

Hi,

            var client1 = new SshClient("11.11.1.1", "user", "pwd");
            client.Connect();
            Console.WriteLine(client.IsConnected); //print true
            var cmd = client.RunCommand("ls -l");  
            var result = cmd.Result;
            Console.WriteLine(result); //print nothing, why?

With SecureCRT, when I first logged into the server(11.11.1.1),the server will prompt me to enter some text. 
But when I use SSH.NET Library, How to put text to the server with my C# code.

Kevin
Thanks.


Coordinator
Nov 15, 2010 at 2:05 PM

What is the values in other properties, such cmd.ExitStatus and cmd.Error?

Also, can you try some other command, for example "echo 123" and see if you get back "123"

Thanks,

Oleg

 

 

 

Nov 15, 2010 at 3:14 PM

cmd.ExitStatus = 1, cmd.Error="".

command "echo 123" 's result is the same.

Coordinator
Nov 15, 2010 at 3:20 PM

since exit status is not 0 it means something happened, unless you plan to return status to be something else.

When you say the same, you mean it returns "123" or empty?

What text do you need to enter when you logon into server using SecureCRT, other then username/password?

What happens if you issue this commands on the server 

echo 123

echo $?

What number you will see here?

 

Thanks,

Oleg

Nov 15, 2010 at 3:31 PM

command "echo 123" 's result is "".

When I logon into server,the server will first print some info , then prompt me to choose one value ( shell scripts in the server).

I guess it(the prompt info from server) stops me to execute commands properly.

Coordinator
Nov 15, 2010 at 3:34 PM

ooh, ok, got it.

So yea, probably script waits for the response and since I was not ready for that it never gets one and all following commands will not be executed.

 

Let me think about it and to try to simulate the environment here and see if I can find solution for that

but as of right now, this scenario is not supported :(

Sorry.

I will keep you updated if I have a fix for this.

 

Thanks,

Oleg

Nov 15, 2010 at 3:34 PM
Edited Nov 15, 2010 at 3:34 PM

When I logon into the server,the server execute some shell scripts right now, and wait for me to type parameter to it.

How bypss it?

Nov 15, 2010 at 3:37 PM

Yeah,Thanks for your attention. I will wait for your update. :)

// Sorry , My english is very poor...... 

Coordinator
Nov 15, 2010 at 3:44 PM

yea, no problem,

by the way, is it a fingerprint question or some other server specific question?

 

Thanks,

Oleg

Nov 15, 2010 at 3:47 PM
Edited Nov 16, 2010 at 2:37 AM

With server(like a proxy), then choose some value to connect the online server in the proxy server.

In the proxy server , I will connect online server with ssh command.

I can connect the proxy server,proxy server can connect to the online server(but I can't connect the onlive server directly).

Coordinator
Nov 16, 2010 at 3:07 PM

I am trying to simulate the environment, where server ask a question upon login, here but with no luck. I am not a unix person so it even harder for me.

Can you give a software example that server uses that causes to ask this question?

May be you have an idea of how to simulate the environment?

Also, when you logon, do you use username/password or key file, which is password protected?

 

Thanks,

Oleg

Coordinator
Nov 17, 2010 at 6:33 PM

Hi,

 

I just checked in last version and would like for you to try something, since I can reproduce your scenario here.

 

            var s = new SshClient("ip", 22, "username", "password");
            s.Connect();
            var shell = s.CreateShell();
            var output = new MemoryStream();
            var output1 = new MemoryStream();
            shell.Connect(output, output1);
            Thread.Sleep(1000 * 2);
            shell.Send("<your input here>\r\n");
            Thread.Sleep(1000 * 2);
            var result = Encoding.ASCII.GetString(output.ToArray());

            var cmd = s.RunCommand("ls -l");
            var result1 = cmd.Result;

And replace where it says <your input goes here> with what you need to enter when you logon into the server and then check cmd for ExitCode and Result properties and let me know if it worked.

 

Thanks,

Oleg

 

Coordinator
Nov 23, 2010 at 4:44 AM

Hi,

 

I updated shell examle where now you can feed any input to the session from the file or any stream.

For example:

            var input = new MemoryStream(Encoding.ASCII.GetBytes("exit\r\n"));
            var shell = s.CreateShell(input, Console.Out, Console.Out, "xterm", 80, 24, 800, 600, "");
            shell.Stopped += delegate(object sender, EventArgs e)
            {
                Console.WriteLine("\nDisconnected...");
            };
            shell.Start();
            Thread.Sleep(1000 * 1000);
            shell.Stop();

Instead of exit command you can put any input that it expects you to enter and then exit the session.

After you do that you should be able to run commands.

Let me know if it works for you.

 

Thanks,

Oleg

Nov 25, 2010 at 1:28 PM

Hi,

I use secureCRT to log the server.When I logon to the server, the sever will print info like:

"Last login: Thu Nov 25 21:24:59 2010 请选择你的权限组(group)和目标机器(server):
Select group: Nobody
 1: NobodyCSL1.cm3    2: NobodyCSL1.cm4  

Select server: "

 

It need to  enter 1 or 2 to end .

After then, I can type "ls - l" commands,and the command result is right.

Nov 25, 2010 at 1:34 PM

With the IP address, user name , password and your SSH library , I can logon to the server . The  IsConnected property is True.

When I logon to the server,the server will print info like :

"Last login: Thu Nov 25 21:24:59 2010 请选择你的权限组(group)和目标机器(server):
Select group: Nobody
 1: NobodyCSL1.cm3    2: NobodyCSL1.cm4  

Select server: "

 

The info above stucks me to execute other commands , like ls , echo, etc.

Nov 25, 2010 at 1:40 PM

Hi,

Actually I don't need the shell to input commands I wants to execute.

I just want to pass the commands to some API in your ssh library , and then I will get the result. 

When my commands need user to enter some info in the process of executing , I hope your ssh library also can support to 'type' text , just to simulate user typing.

 

Thanks,

Kevin

Coordinator
Nov 25, 2010 at 2:00 PM

SSH as a protocol doesnt support the functionality of passing some input when logged so what might be happening is some scirp running when you logon into shell.

 

Shen you use SSH terminal tools such secureCRT and others they open shell session when you login and thats allows you to pass some input and get output from SSH server terminal.

What you can do in this case is try the following:

 var input = new MemoryStream(Encoding.ASCII.GetBytes("1\r\n")); // or 2 
            var shell = s.CreateShell(input, Console.Out, Console.Out, "xterm", 80, 24, 800, 600, "");
            shell.Stopped += delegate(object sender, EventArgs e)
            {
                Console.WriteLine("\nDisconnected...");
            };
            shell.Start(); // this will send your input to the shell
            shell.Stop(); // This will stop the shell
            
           var cmd = s.RunCommand("ls -l");  // This command should run now

Let me know if this work. I couldnt simulate this case on my server so it will  be hard for me to debug this issue.

 

Thanks,

Oleg