使用 SSH 在 C# 中的 Unix 服务器上运行自定义脚本

发布于 2024-11-04 12:01:12 字数 1566 浏览 0 评论 0原文

大家好,我一直在使用 新的 SHH 库 向 unix 服务器发送命令,它对我来说非常有用。它可以很好地发送正常命令并接收正确的响应。但是,当我尝试使用它来运行自定义脚本(不是 shell 脚本,而是包含另一个命令并具有参数的文件)时,我似乎遇到了问题,

我尝试了多种方法来使其正常工作。

在 unix 服务器本身上,以下命令完美运行并执行其预期操作:

  • cd script; script.oi someArg someArg - 可以工作
  • csh -c "cd script; script.oi someArg someArg" - 也可以工作
  • 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

但是,在代码中我有尝试了以下操作:

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

所以对我来说,似乎发生了其他事情。我确实尝试了以下操作:

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

看起来这与我尝试运行自定义脚本这一事实有关,或者可能是我忘记了一些愚蠢的设置。如果您需要更多详细信息,请告诉我。

编辑: 通过“不起作用”,我的意思是返回到 C# 的结果应该显示一些内容,但结果是空白的。此外,该脚本发送一条 TIBCO Rendevous 消息,最终将一个条目添加到数据库中,但该条目未显示。当我说“有效”时,我的意思是该条目显示在数据库中。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

成熟稳重的好男人 2024-11-11 12:01:12

好吧,我想通了。事实证明,该脚本正在运行一个命令,该命令通过添加到 unix 环境变量而为 unix 服务器所知。当仅使用标准方式在 SSH.NET 中发送命令时,不会导入环境变量(这就是为什么您能够发送普通命令,如 ls、grep、cat 等,但不能发送此命令)。

解决方法是使用库 SSH shell 功能,因为它确实导入环境变量,因此它知道脚本中的命令是什么并且能够运行它。对于那些使用这个出色的库(仍在积极开发中,始终是一个优点)的人,我在下面提供了 SSH.NET Shell 示例代码。

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
}

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
}
乱世争霸 2024-11-11 12:01:12

使用 ssh 客户端登录的用户是否对调用的可执行文件具有执行权限?

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

海之角 2024-11-11 12:01:12

由于脚本应该可由每个人执行,因此参数应该有问题。获取生成的命令字符串并尝试在 SSH 会话上自行执行它(使用 PuTTY 之类的工具)。如果可能,请向我们提供生成的命令。我认为其中可能有需要正确转义的字符。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文