Reputation: 37
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
Is there anything similar to SSH.NET client as well?
Upvotes: 1
Views: 1516
Reputation: 26917
Here is a custom ShellStream
wrapper that has provision for logging. It also has other custom features for my main use, which is a CLI wrapper for network switch configuration.
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);
Upvotes: 1