Getting full command output from ShellStream of C# SSH.NET

こ雲淡風輕ζ 提交于 2019-12-04 11:51:19

The shell is an endless stream. There's no send command – receive output sequence. The ReadToEnd cannot know where an output of one command ends. All you can do is to read until you yourself can tell that the output ended. If you cannot tell that, you can help yourself by appending some kind of end-of-output mark like:

command 1 ; echo this-is-the-end-of-the-output

and read until you get "this-is-the-end-of-the-output" line.


Generally a "shell" channel is not an ideal solution for automation. It's intended for an interactive sessions.

You better use "exec" channel using SshClient.CreateCommand. With CreateCommand the channel closes once the command finishes. So there's clear "end of the stream", what makes the ReadToEnd() work as you expect. And SSH.NET even makes whole command output available in SshCommand.Result (which internally uses ReadToEnd()).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!