c# - Using a SSH library to connect to unix and tail a file: Is this the right approach? -


as ive stated few other questions, ive been using new ssh .net library connect unix server , run various scripts , commands. well, i've attempted use run unix tail -f on live log file , display tail in winforms richtextbox.

since library not fully-fleshed out, kinda-sorta solution i've come seems lacking... feeling when know there has better way. have connection/tailing code in separate thread avoid ui thread lock-ups. thread supports cancellation request (which allow connection gracefully exit, way ensure process unix side killed). here's code far (which record seems work, thoughts on if right way go it):

passwordconnectioninfo connectioninfo = new passwordconnectioninfo(lineip, username, password);  string command = "cd /logs; tail -f " + buildfilename() + " \r\n";  using (var ssh = new sshclient(connectioninfo)) {     ssh.connect();      var output = new memorystream();     var shell = ssh.createshell(encoding.ascii, command, output, output);      shell.start();      long positionlastwrite = 0;      while (!testbackgroundworker.cancellationpending) //checks cancel request       {         output.position = positionlastwrite;          var result = new streamreader(output, encoding.ascii).readtoend();         positionlastwrite = output.position;         updatetextbox(result);          thread.sleep(1000);     }      shell.stop();     e.cancel = true; } 

the updatetextbox() function thread-safe way of updating richtextbox used display tail different thread. positionlastwrite stuff attempt make sure don’t loose data in between thread.sleep(1000).

now im not sure 2 things, first being have feeling might missing out on data each time whole changing memorystream position thing (due lack of experiance memorystreams, , second being whole sleep 1 second update again thing seems pretty archaic , inefficient... thoughts?

mh, realized not creator of ssh library (although it's on codeplex submit patches), anyway: might want wrap loop try {} {} , call shell.stop() in block make sure cleaned up.

depending on available interfaces polling might way go , not inherently bad. whether or not loose data depends on shell object doing buffering: buffer output in memory, throw away output after time?

my original points still stand:

one thing comes mind looks shell object buffering whole output in memory time poses potential resource problem (out of memory). 1 option of changing interface use blockingqueue in shell object. shell enqueuing output remote host in there , in client can sit there , dequeue block if nothing there read.

also: consider making shell object (whatever type createshell returns) idisposable. description sounds shell.stop() required clean won't happen in case exception thrown in while loop.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -