Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Please allow us to customize `OutputStream` and `ExtendedOutputStream` before executing an SSH command.

オープン
#1,608 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
30/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
csharp
領域
networking

調査の方向性

まず、SshCommand.OutputStream と ExtendedOutputStream、拡張可能な PipeStream 型、および ExecuteAsync エントリポイントを確認します。reflection を使わずに標準出力とエラーをリアルタイムで処理でき、コマンド実行中も両方のストリームを使用可能なままにする、公開カスタマイズメカニズムを定義して検証します。

索引モデルが issue の本文から書いたものです。

説明

I have a use case where I want real time output from the console command to be printed to the console. Unfortunately, the streams that are written to cannot be customized with the existing public interface, even though the PipeStream class is extensible. The best solution I found to do what I need to do unfortunately requires reflection.

A public interface (such as a callback) for this feature would be greatly appreciated.

public static class SshClientExtensions
{
    public static async Task<SshCommand> ConnectAndExecuteCommandAsync(this SshClient client, string command, CancellationToken cancellationToken = default)
    {
        SshCommand sshCommand;
        try
        {
            await client.ConnectAsync(cancellationToken);
            sshCommand = client.CreateCommand(command);
            using CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;
            PropertyInfo stdOutProp = typeof(SshCommand).GetProperty(nameof(SshCommand.OutputStream), bindingFlags)!;
            PropertyInfo stdErrProp = typeof(SshCommand).GetProperty(nameof(SshCommand.ExtendedOutputStream), bindingFlags)!;

            PipeStream stdOutStream = (PipeStream)stdOutProp.GetValue(sshCommand)!;
            PipeStream stdErrStream = (PipeStream)stdErrProp.GetValue(sshCommand)!;

            await stdOutStream.DisposeAsync();
            await stdErrStream.DisposeAsync();

            TextWriterPipeStream stdOut = new(Console.Out);
            TextWriterPipeStream stdErr = new(Console.Error);

            stdOutProp.SetValue(sshCommand, stdOut);
            stdErrProp.SetValue(sshCommand, stdErr);

            await sshCommand.ExecuteAsync(cancellationToken);
        }
        catch (Exception e)
        {
            return await Task.FromException<SshCommand>(e);
        }
        finally
        {
            client.Disconnect();
        }
        return sshCommand;
    }

    private sealed class TextWriterPipeStream : PipeStream
    {
        private readonly TextWriter textWriter;

        public TextWriterPipeStream(TextWriter textWriter) : base()
        {
            this.textWriter = textWriter;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            base.Write(buffer, offset, count);
            string output = Encoding.UTF8.GetString(buffer, offset, count);
            textWriter.Write(output);
        }
    }
}
主要言語
C#
スター
4.4k
フォーク
993
平均マージ
9日 21時間
マージ済み PR(30日)
1

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

sshnet/SSH.NET のほかの issue

sshnet/SSH.NET の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。