【问题标题】:Commands fail with “<command> not found”, when executed using SSH.NET CreateCommand in C#在 C# 中使用 SSH.NET CreateCommand 执行时,命令失败并显示“<command> not found”
【发布时间】:2020-03-04 04:48:07
【问题描述】:

我试图使用SSH.NET NuGet package 远程执行命令以获取安装在连接到 Mac 的 iPhone 上的应用版本。

如果在 Mac 上使用以下命令执行,我会得到它的版本:

ideviceinstaller -l|grep <bundleIdOfMyAppPackage>

所以我用这个包在 C# 中构建了一个小实用程序,希望我能利用它。但是,我得到的只是一个空字符串。谁能让我知道我能做些什么来得到我想要的结果?谢谢!

var host = "myhost";
var username = "username";
var password = "password";

using (var client = new SshClient(host, username, password))
{
    client.HostKeyReceived += delegate(object sender, HostKeyEventArgs e) { e.CanTrust = true; };

    client.Connect();
    var command = client.CreateCommand("ideviceinstaller -l|grep <bundleIdOfMyAppPackage>");
    command.Execute();

    var result = command.Result;
    Console.WriteLine(result);

    client.Disconnect();
}

我从command.Error 得到的错误是

zsh1:找不到命令 ideviceinstaller`

这很奇怪,因为如果我浏览到那里,我可以在该文件夹中看到 ideviceinstaller


感谢@Martin Prikryl,将命令更改为:

/usr/local/bin/ideviceinstaller -l|grep <myAppBundleId>

【问题讨论】:

    标签: c# .net shell ssh ssh.net


    【解决方案1】:

    SSH.NET SshClient.CreateCommand(或SshClient.RunCommand)不会在“登录”模式下运行 shell,也不会为会话分配伪终端。因此,与常规交互式 SSH 会话相比,(可能)获取了一组不同的启动脚本(特别是对于非交互式会话,.bash_profile 未获取)。和/或脚本中的不同分支基于TERM 环境变量的缺失/存在而被采用。

    可能的解决方案(按优先顺序):

    1. 修复命令不依赖于特定环境。在命令中使用ideviceinstaller 的完整路径。例如:

       /path/to/ideviceinstaller ...
      

      如果您不知道完整路径,在常见的 *nix 系统上,您可以在交互式 SSH 会话中使用 which ideviceinstaller 命令。

    2. 修复您的启动脚本,为交互式和非交互式会话设置相同的 PATH

    3. 尝试通过登录 shell 显式运行脚本(将 --login 开关与常见的 *nix shell 一起使用):

       bash --login -c "ideviceinstaller ..."
      
    4. 如果命令本身依赖于特定的环境设置并且您无法修复启动脚本,您可以在命令本身中更改环境。其语法取决于远程系统和/或 shell。在常见的 *nix 系统中,这是可行的:

       PATH="$PATH;/path/to/ideviceinstaller" && ideviceinstaller ...
      
    5. 另一个(不推荐)是使用“shell”通道通过SshClient.CreateShellStreamSshClient.CreateShell执行命令,因为它们分配伪终端

       ShellStream shellStream = client.CreateShellStream(string.Empty, 0, 0, 0, 0, 0);
       shellStream.Write("ideviceinstaller\n");
      
       while (true)
       {
           string s = shellStream.Read();
           Console.Write(s);
       }
      

      使用 shell 和伪终端自动执行命令会给您带来讨厌的副作用。

    【讨论】:

    • 太棒了!!!我已经让它工作了。谢谢。我会更新我的问题
    猜你喜欢
    • 2021-08-07
    • 2011-02-12
    • 1970-01-01
    • 2017-02-22
    相关资源
    最近更新 更多