sshnet / sshnet/SSH.NET

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

Aperta
#1,608 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
C#
Stelle
4.4k
Fork
993
Merge medio
9g 21h
PR unite (30g)
1

Descrizione

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);
        }
    }
}

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando SshCommand.OutputStream e ExtendedOutputStream, il tipo estensibile PipeStream e il punto di ingresso ExecuteAsync. Definisci e convalida un meccanismo pubblico di personalizzazione che supporti la gestione in tempo reale dell'output standard e degli errori senza reflection, mantenendo entrambi gli stream utilizzabili durante l'esecuzione del comando.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp
Ambito
networking
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.