如何解决在z / OS上使用SSH.NET通过SSH更改密码的命令
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);
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#中执行此操作的好方法是什么?
解决方法
您的服务器似乎不支持“ 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
类以像IChannelSession.Write
那样公开ShellStream.Write
。
否则,您将不得不使用其他库(尽管我不知道SSH.NET以外的其他免费库)。或者运行诸如PuTTY Plink之类的外部工具(我不喜欢那样,但这可能是最简单的方法)。参见Sending input during command output with SSH.NET。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。