我试图在运行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
}
下面是我在程序中创建它的方法:
SSHStream = SSHClient.CreateExtShellStream("dumb", 240, 120, 512, 0, 65536);