Command Error

Apr 25, 2011 at 4:29 PM

Hi,

I am connecting to a machine via ssh and machine is getting connected successfully but when I am running the command "omc version" its not returning anything. in the error its showing ksh: omc: not found \n so i think its not considering the version in the command. I ran the ls command its gave me the output but not the omc version command. can anyone please help me?

var sshClient = new SshClient(IpAddress,login, passwd);

sshClient.Connect();

var cmd = sshClient.runCommand("omc version");

var result = cmd.result;

result is not showing anything but i have checked the cmd.error and it showing ksh: omc: not found\n.

So i think its executing only omc.

I dont think its command may be its script but manually i am able to run "omc version" command easily and i am getting output too.

Coordinator
Apr 25, 2011 at 4:48 PM

Hi,

 

The space should be considered as what client does it takes the command string and send it to the server.

To ensure that it works issue this command to check : "echo 1234" and see if you get 1234 back.

 

Thanks,

Oleg

Apr 26, 2011 at 1:31 AM

Hi Oleg,

Thanks a lot for reply. Yes its return back 1234 and i ran few other command too but its not taking this command (omc version). I am new to unix and may be its script but i can run this command simply by logging on the machine. Can you please tell me how to run this if it is script?

 

Thanks & Regards,

Shyam Sohane

Apr 26, 2011 at 7:24 AM

Hi Oleg,

 

I ran the command "omc version"  by creating the shell and its giving me the correct output but it got hand after recieving the data from channel. The code i am using below. But its getting hang on shell.stop. Can you please help me how to make this work.

            var sshClient = new SshClient("ip", "userid", "password");
            sshClient.Connect(); 
           
            var input = new MemoryStream(Encoding.ASCII.GetBytes("omc version\r"));            
            TextWriter text;           
            var shell = sshClient.CreateShell(input, Console.Out, Console.Out, "xterm", 80, 24, 800, 600, "");                        
            shell.Start();            
            shell.Stop();

            Console.ReadLine();
            sshClient.Disconnect();


Thanks & Regards,
Shyam Sohane

Apr 26, 2011 at 12:17 PM

Hi Oleg,

 

I was debugging the code and two threads are going in channel dispose waithandle call and will deadlock. First when shell.start() runs after finishing it will try to dispose channel and when main method calls the shell.stop it will also try to dispose channel and go in wait handle.

I am not threading expert so may be i am wrong :( but i tried to debug and find out the reason so it will be easy  for you to help me.

Thanks & Regards,

Shyam Sohane

Apr 27, 2011 at 8:42 PM

I think Im having a similar problem.. trying to run a script using your code and nothing happens (results is "") but if I enter the exact same command on the actual unix box it works fine.  My code:

string unixResults = String.Empty;
string ip = "some ip";

string command = "cd script; script.oi " + arg1 + " " + arg2;

PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo(ip, "user", "pass");

try
{
     using (var client = new SshClient(connectionInfo))
     {
          client.Connect();
          var cmd = client.RunCommand(command);
          unixResults = cmd.Result;
          client.Disconnect();
     }

     return unixResults;
}
catch (Exception ex)
{
     // Do exception stuff
}

This does nothing, but interestingly enough if I change command to "cd script; cat script.oi" it opens and returns the script's contents as you'd expect it to.

Coordinator
Apr 28, 2011 at 2:15 PM

See me reponse in http://sshnet.codeplex.com/discussions/255435 thread cause I think it could be related.

 

Oleg

Apr 29, 2011 at 9:46 PM

So youre suggesting to make a simple shell to send the command?  If so, how would I go about doing that? Theres no shell examples in your help file.

Coordinator
Apr 30, 2011 at 3:29 AM

Something like this, I think:

 

 

string unixResults = String.Empty;
string ip = "some ip";

string command = string.Format("tcsh -c \"cd script; script.oi {0} {1} \" ",arg1,arg2);

PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo(ip, "user", "pass");

try
{
     using (var client = new SshClient(connectionInfo))
     {
          client.Connect();
          var cmd = client.RunCommand(command);
          unixResults = cmd.Result;
          client.Disconnect();
     }

     return unixResults;
}
catch (Exception ex)
{
     // Do exception stuff
}

 

 

Please not how I added "tcsh" into executed comand.

tcsh -c "<you command goes here>"

 

Hope it helps,

 

Thanks,

Oleg

May 2, 2011 at 2:04 PM

Was adding tcsh -c the only thing I need to do? Cause that alone didnt seem to have an effect.

May 3, 2011 at 12:49 PM

Yes me too tried with tcsh -c but no effect. may be we have to do something else.

 

May 3, 2011 at 1:58 PM
shyamsohane wrote:

Yes me too tried with tcsh -c but no effect. may be we have to do something else. 


Shucks.  Well, let me know if you figure something out

Coordinator
May 3, 2011 at 2:03 PM

Hi,

 

I just tried this:

 

            using (var ssh = new SshClient(connectionInfo))
            {
                ssh.Connect();
                //var cmd = ssh.RunCommand("ls -l");
                var cmd = ssh.RunCommand("tcsh -c \"ls -l\"");

                Console.WriteLine(cmd.Result);
            }

 

It it worked just fine. It printed out the list of files correctly using tcsh shell.

One reason why it might not work for you is because you dont have tcsh shell available for you, in this case you need to try to use different shell. tcsh was just an example.

To test if its going to work first, you can try to execute it in putty first and see if it works.

I run this command in the putty first just to see if its gonna work:

-bash-3.2$  tcsh -c "ls -l"

 

Hope it helps,

Oleg

May 3, 2011 at 4:06 PM
Edited May 3, 2011 at 4:13 PM

So tcsh is something that would be avialable on the unix server itself? If so, how would I check if I have its capability (or check if there are other shells available)?

EDIT: So I ran "echo $SHELL" and got "/usr/bin/csh".  Does the CSH shell run the same way?

Coordinator
May 3, 2011 at 4:31 PM

yes, csh shell should work the same way.

You can run man csh command to see what flags are avaialble.

May 3, 2011 at 5:29 PM

Hi Oleg,

 

I have run the command on unix system and running fine but the same command is not giving any output by .runcommand. May be the command is running but not getting the output in result.

 

Thanks & Regards,

Shyam Sohane

May 3, 2011 at 6:51 PM

Im not sure thats whats going wrong Oleg.

On the unix server itself the following commands work perfectly and do what they are intended:

  • cd script; script.oi someArg someArg - WORKS
  • csh -c "cd script; script.oi someArg someArg" - ALSO WORKS

However, in the code I have tried the following:

  • string command = string.Format("csh -c \"cd script; script.oi {0} {1}\"", arg1, arg2); - DOES NOT WORK
  • string command = string.Format("cd script; script.oi {0} {1}", arg1, arg2); - DOES NOT WORK

So to me it seems like something else is going on.  I did try the following:

  • string command = string.Format("csh -c \"ls\"", arg1, arg2); - WORKS
  • string command = string.Format("ls", arg1, arg2); - WORKS

It looks like it has to do with the fact that Im trying to run a custom script.

Coordinator
May 3, 2011 at 6:53 PM

instead of cd script; script.oi try to use absolute path. something like:

/usr/bin/script/script.oi {0} {1}

 

Oleg

May 3, 2011 at 7:02 PM
Edited May 3, 2011 at 7:03 PM
olegkap wrote:

instead of cd script; script.oi try to use absolute path. something like:

/usr/bin/script/script.oi {0} {1} 

Oleg

On unix server:

  • cd /users/bin/script; script.oi someArg1 someArg 2 - WORKS
  • csh -c "cd /users/bin/script; script.oi someArg1 someArg 2" - WORKS

In code:

  •  string command = string.Format("cd /users/bin/script; script.oi {0} {1}", arg1, arg2); - DOES NOT WORK
  •  string command = string.Format("csh -c \"cd /users/bin/script; script.oi {0} {1}\"", arg1, arg2); - DOES NOT WORK
Coordinator
May 3, 2011 at 7:06 PM

did you tried :

string command = string.Format("/users/bin/script/script.oi {0} {1}", arg1, arg2);

May 3, 2011 at 7:15 PM
Edited May 3, 2011 at 7:15 PM
olegkap wrote:

did you tried:

string command = string.Format("/users/bin/script/script.oi {0} {1}", arg1, arg2);

Just tried it out, "/users/bin/script/script.oi someArg1 someArg2" works fine in the unix server but

  • string command = string.Format("/users/bin/script/script.oi {0} {1}", arg1, arg2);

did not work

May 3, 2011 at 8:19 PM
Edited May 3, 2011 at 9:20 PM

Also, I looked into what the script actually does.  It runs a different command (tibco rv message command, installed on the unix server), so i decided to run that command by itself (no script).  Again, on the unix server it worked fine but in the code it did not...

EDIT: Interestingly enough, I connect with the same info I use in the code with PuTTY (which should be doing the same thing as your library) and running most of those commands works.

May 5, 2011 at 2:13 PM

Any ideas Oleg? This is pretty strange

Coordinator
May 5, 2011 at 2:30 PM

Well, there is one difference between running command using RunCommand and running same command from using putty.

When you use putty, it creates a shell, and then runs commands within it shell. When you use RunCommand it only runs specific command without creating any shell.

So my guess would be is there is something going on inside this scripts that relies on being run inside of the shell session and not command session.

I am not familiar with tibco command and what it does so I guess I am running out of options.

You can try to use shell object, I dont know if you tried that already, and it should work, but catching output would be tricky...

 

Hope it helps,

Oleg

May 5, 2011 at 2:56 PM

Actually this is a rare case where I dont really need to catch the output (I was just using the output as a way to see if it worked or not).  Would those shell examples you posted in other threads work?

Coordinator
May 5, 2011 at 3:35 PM

I have some shell examples in this thread as well as in some others.

May 5, 2011 at 3:47 PM
olegkap wrote:

I have some shell examples in this thread as well as in some others.

Awesome, thanks again man.  Ill let you know how it goes