C# SSH.NET详细模式

C# SSH.NET详细模式,c#,.net,security,ssh,openssh,C#,.net,Security,Ssh,Openssh,我试图在运行SSH.NET应用程序时在控制台上打印更多日志,我知道对于OpenSSH客户端,您只需添加SSH-vvv即可获得所有跟踪 还有类似于SSH.NET客户端的东西吗?这里有一个定制的ShellStream包装器,它提供了日志记录功能。它还具有其他主要用途的自定义功能,这是用于网络交换机配置的CLI包装 public static class SshClientExt { public static ExtShellStream CreateExtShellStream(this

我试图在运行SSH.NET应用程序时在控制台上打印更多日志,我知道对于OpenSSH客户端,您只需添加SSH-vvv即可获得所有跟踪


还有类似于SSH.NET客户端的东西吗?

这里有一个定制的
ShellStream
包装器,它提供了日志记录功能。它还具有其他主要用途的自定义功能,这是用于网络交换机配置的CLI包装

public static class SshClientExt {
    public static ExtShellStream CreateExtShellStream(this SshClient sc, string termName, uint cols, uint rows, uint width, uint height, int bufSize) =>
        new ExtShellStream(sc.CreateShellStream(termName, cols, rows, width, height, bufSize));
}

public class ExtShellStream : IDisposable {
    static Regex reEscVT100 = new Regex("\x1B\\[[^A-Z]+[A-Z]", RegexOptions.Compiled);
    static TimeSpan ReadTimeout = new TimeSpan(0, 0, 10);

    public bool Debug = false;

    ShellStream ssh;
    StreamReader sr;
    StreamWriter sw;

    public ExtShellStream(ShellStream anSSH) {
        ssh = anSSH;
        sr = new StreamReader(ssh);
        sw = new StreamWriter(ssh);
    }

    public List<string> ReadLinesUpTo(string prompt, TimeSpan? timeout = null) {
        if (Debug) {
            Console.WriteLine($"{DateTime.Now:HH:mm:ss.ff}: >>>ReadLinesUpTo(\"{prompt}\", {timeout:%s})");
            Console.WriteLine($"{DateTime.Now:HH:mm:ss.ff}: " + new String('v', 60));
        }

        var ans = new List<string>();
        var now = DateTime.Now;
        do {
            var line = sr.ReadLine();
            if (line != null) {
                line = line.Remove(reEscVT100).TrimEnd();
                if (Debug)
                    Console.WriteLine($@"<""{line}""");

                if (line.EndsWith(prompt)) {
                    if (Debug)
                        Console.WriteLine($"{DateTime.Now:HH:mm:ss.ff}: Found prompt, done reading");
                    break;
                }

                ans.Add(line);
                if (ssh.Length < 240) { // wait for more lines to come in
                    Thread.Sleep(50);
                }
                now = DateTime.Now; // reset timeout while data is available
            }
            else
                Thread.Sleep(250); // if no prompt, wait for more data until timeout
        } while (!timeout.HasValue || DateTime.Now - now <= timeout);

        if (Debug) {
            Console.WriteLine($"{DateTime.Now:HH:mm:ss.ff}: " + new String('^', 60));
            Console.WriteLine($"{DateTime.Now:HH:mm:ss.ff}: <<<ReadLinesUpTo(\"{prompt}\")");
        }
        return ans;
    }

    static TimeSpan DumpTimeout = TimeSpan.FromSeconds(0.1);
    public void DumpLines() => ReadLinesUpTo("#", DumpTimeout);

    public void Send(string toSend, bool dumpLines = false) {
        if (Debug)
            Console.WriteLine($"Send(\"{toSend}\", {dumpLines})");
        sw.Write(toSend);
        sw.Flush();
        if (dumpLines)
            DumpLines();
    }

    public IEnumerable<string> DoCommand(string cmd, TimeSpan? timeout, string prompt) {
        sr.DiscardBufferedData();
        if (Debug)
            Console.WriteLine($"Write>\"{cmd}\\r\"");
        sw.Write(cmd);
        Send("\r");

        while (!ssh.DataAvailable)
            Thread.Sleep(250);

        return ReadLinesUpTo(prompt, timeout).Select(l => l.StartsWith(cmd) ? l.Substring(cmd.Length) : l);
    }

    #region IDisposable Support
    private bool disposedValue = false; // To detect redundant calls

    protected virtual void Dispose(bool disposing) {
        if (!disposedValue) {
            if (disposing) {
                // prevent double dispose
                // don't dispose of sr or sw: their only resource is ssh
                ssh.Dispose();
            }

            disposedValue = true;
        }
    }

    // This code added to correctly implement the disposable pattern.
    public void Dispose() {
        // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
        Dispose(true);
    }
    #endregion
}

这个问题可能就是您要查找的:我要查找的是调试输出日志而不是命令输出。我创建了一个自定义的
ExShellStream
类,该类包装
ShellStream
,并具有一个调试标志来输出读写内容。它还具有读取提示和收集响应的方法。@NetMage您能告诉我该类的位置吗?
SSHStream = SSHClient.CreateExtShellStream("dumb", 240, 120, 512, 0, 65536);