【问题标题】:Password change command via SSH using SSH.NET on z/OS在 z/OS 上使用 SSH.NET 通过 SSH 更改密码命令
【发布时间】:2020-09-17 17:53:14
【问题描述】:

我希望将手动完成的密码更改自动化:

plink -ssh user@host changepass
user@host's password:
Access granted. Press Return to begin session.
Enter current password:
...

我的理解是,我无法使用 RunCommand 完成此操作,因为它不是交互式的,并且不允许我进一步指定当前密码、按 Enter 键,然后是新密码等。据我所知,我会使用CreateShellStream 模拟外壳。当我尝试做这样的事情时:

using (var client = new SshClient(host, username, password))
{
    client.Connect();

    ShellStream shellStream = client.CreateShellStream(string.Empty, 0, 0, 0, 0, 0);
    Console.Write(SendCommand("changepass", shellStream));
    
    client.Disconnect();
}

结果是The command executed is invalid.....,这与我尝试使用 plink 而不指定内联命令时得到的结果相同:

plink -ssh -t user@host
Using username "user".
-- Pre-authentication banner message from server: ----------------------------
user@host's password:
Access granted. Press Return to begin session.
The command executed is invalid.....

我想知道我的方法是否无效以及这是否可以通过 SSH.NET 完成。如果不是,那么从 C# 中执行此操作的好方法是什么?

【问题讨论】:

    标签: c# ssh zos ssh.net


    【解决方案1】:

    您的服务器似乎不支持“shell”通道,只支持“exec”通道。

    确实 SSH.NET 不支持为使用“exec”通道执行的命令提供输入。见Providing input/subcommands to a command (cli) executed with SSH.NET SshClient.RunCommand

    如果它是一个 *nix 服务器,您可能会尝试使用输入重定向来提供输入。

    client.RunCommand("echo input | command");
    

    虽然 1) 这不是一种安全的密码方式 2) 我不知道 z/OS 的正确语法(或者甚至可能)。


    要允许正确提供输入,您必须获取 SSH.NET 代码并修改 SshCommand 类以像 ShellStream.Write 那样公开 IChannelSession.Write


    否则您将不得不使用不同的库(尽管我不知道除了 SSH.NET 之外的其他免费的好库)。或者运行像PuTTY Plink 这样的外部工具(我不喜欢这样,但这可能是最简单的方法)。见Sending input during command output with SSH.NET

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-25
      • 2015-08-27
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 2011-03-06
      • 2019-06-17
      • 2023-03-20
      相关资源
      最近更新 更多