首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SSH.NET详细模式

SSH.NET详细模式
EN

Stack Overflow用户
提问于 2021-02-23 06:44:46
回答 1查看 192关注 0票数 0

我正在尝试在控制台上打印更多的日志,同时运行我的SSH.NET应用程序,我知道对于OpenSSH客户端,您可以简单地添加ssh -vvv来获取所有跟踪

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

EN

回答 1

Stack Overflow用户

发布于 2021-02-24 06:47:23

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

代码语言:javascript
复制
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
}

下面是我如何在程序中创建它:

代码语言:javascript
复制
SSHStream = SSHClient.CreateExtShellStream("dumb", 240, 120, 512, 0, 65536);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66324530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档