Using SSH to run custom scripts on a Unix server in C #

Hi everyone, Ive used the new SHH library to send commands to a unix server, and it works great for me. It perfectly conveys ordinary commands and returns the correct answers. However, I seem to encounter a problem when I try to use it to run a custom script (not a shell script, but a file that contains another command and has arguments)

Ive tried several ways to make this work.

On the unix server itself, the following commands work fine and do what they are intended to do:

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

However, in the code, I 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
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 
string command = string.Format("/users/bin/script/script.oi {0} {1}", arg1, arg2); - DOES NOT WORK

So it seems to me that something else is happening. I tried the following:

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

This seems to be due to the fact that Im trying to run a custom script, or maybe some kind of dumb setup that I forgot. Let me know if you need more details.

EDIT: DOES NOT WORK, I mean that the result returned in C # should say some things, but the result is empty. In addition, the script sends a TIBCO Rendevous message, which ultimately adds a record to the database that is not displayed. When I say WORKS, I mean that the record appears in the database.

+3
3

, . , script , unix, evixment unix. SSH.NET, ( , ls, grep, cat, ect, ).

, SSH , , , script . , ( - , ), SSH.NET.

PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo("some IP", "user", "pass");

try
{
    using (var client = new SshClient(connectionInfo))
    {
        client.Connect();
        var input = new MemoryStream(Encoding.ASCII.GetBytes(command));
        var shell = client.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);
        shell.Stop();
        client.Disconnect();
    }   
}
catch (Exception ex)
{
    // Exception stuff
}
+2

, ssh, ?

0

Since the script must be executed by everyone, there must be something wrong with the arguments. Get the generated String command and try to execute it yourself in an SSH session (with something like PuTTY). If possible, we will provide us with the generated command. I suggest that there may be characters in it that need proper escaping.

0
source

All Articles