简体   繁体   English

SSH.NET 详细模式

[英]SSH.NET Verbose Mode

I am trying to print more logs on the console while running my SSH.NET app, I know for OpenSSH client you can simply add ssh -vvv to get all traces我正在尝试在运行 SSH.NET 应用程序时在控制台上打印更多日志,我知道对于 OpenSSH 客户端,您只需添加 ssh -vvv 即可获取所有跟踪

Is there anything similar to SSH.NET client as well?是否也有类似于 SSH.NET 客户端的东西?

Here is a custom ShellStream wrapper that has provision for logging.这是一个自定义的ShellStream包装器,它提供了日志记录。 It also has other custom features for my main use, which is a CLI wrapper for network switch configuration.它还具有我主要使用的其他自定义功能,它是用于网络交换机配置的 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
}

Here is how I create it in the program:这是我在程序中创建它的方式:

SSHStream = SSHClient.CreateExtShellStream("dumb", 240, 120, 512, 0, 65536);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM