开发者

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

开发者 https://www.devze.com 2023-03-02 05:58 出处:网络
Hey all, Ive been using a new SHH library to send commands to a unix server and its been working great for me.It sends normal commands just fine and recives the proper responses.However, I seem to be

Hey all, Ive been using a new SHH library to send commands to a unix server and its been working great for me. It sends normal commands just fine and recives the proper responses. However, I seem to be running into an issue 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 get this to work.

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
  • 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 have tried开发者_JAVA百科 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 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, or maybe some silly setting I've forgotten. Let me know if you need anymore details.

EDIT: By DOES NOT WORK, I mean that the result that is returned to the C# is supposed to say some stuff, but the result is blank. Additionaly, the script sends a TIBCO Rendevous message which eventually adds an entry to a DB, which is not showing up. When I say WORKS, I mean that the entry is showing up in the DB.


Well, I figured it out. Turns out the script was running a command that is known to the unix server via being added to the unix eviroment variables. When just using the standard way to send commands in SSH.NET, the enviroment variables are not imported (which is why you are able to send normal commands like ls, grep, cat, ect, but not this one).

The fix was to use the libraries SSH shell functionality because it does import the enviroment variables, therefore it knows what that command in the script is and is able to run it. For those using this great library (which is still be activily developed, always a plus) I have included below example SSH.NET Shell code.

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
}


Does the user being logged on using the ssh client have execute privileges on the called executable?


As the script should be executable by everyone there should be something wrong with the arguments. Get the generated command-String and try to execute it by yourself on a SSH-session (with something like PuTTY). If possible provide us the generated command. I assume there might be chars in it which need proper escaping.

0

精彩评论

暂无评论...
验证码 换一张
取 消