11using CoreMeter ;
22using Newtonsoft . Json ;
33using Renci . SshNet ;
4+ using Renci . SshNet . Common ;
45using System ;
56using System . Collections . Generic ;
67using System . Collections . ObjectModel ;
@@ -41,6 +42,7 @@ public partial class MainWindow : Window
4142
4243 // SSH Handling
4344 private SshClient _sshClient ;
45+ private ShellStream _sshStream ;
4446 private bool _sshMode = false ;
4547 private string _sshServer = String . Empty ;
4648 private string _sshUsername = String . Empty ;
@@ -174,18 +176,6 @@ private void OutputSSHUsage()
174176 {
175177 _terminalData . Add ( "Background Terminal manually handles SSH connection. (Ctrl + C to quit)" ) ;
176178 _terminalData . Add ( "Usage: ssh <server>" ) ;
177- _terminalData . Add ( "Note that SSH.net does not support change directory (cd), so you are required to prefix the " +
178- "command with a (cd) call to the directory you want to be in. (cd /my/directory && mycommand)" ) ;
179- _terminalData . Add ( "To get around this, I have implemented automated directory prefixing. If you call (cd) while in SSH mode, it will automatically prefix any " +
180- "further commands with the directory you previously specified." ) ;
181- }
182-
183- private string DirectoryPrefixCommand ( string command )
184- {
185- if ( ! _sshCurrentDirectory . Equals ( String . Empty ) )
186- return "cd " + _sshCurrentDirectory + " && " + command ;
187-
188- return command ;
189179 }
190180 #endregion
191181
@@ -306,7 +296,7 @@ private void ErrorDataReceived(object sender, DataReceivedEventArgs e)
306296 _terminalData . Add ( e . Data ) ;
307297 }
308298
309- private string SendCommandSSH ( string command , bool silent = false )
299+ private async Task < string > SendCommandSSH ( string command , bool silent = false )
310300 {
311301 // Handle SSH login connection
312302 if ( _sshUsername . Equals ( String . Empty ) )
@@ -329,8 +319,27 @@ private string SendCommandSSH(string command, bool silent = false)
329319 _terminalWindow . _passwordMode = false ;
330320 _terminalWindow . _password = String . Empty ;
331321
322+ var modes = new Dictionary < Renci . SshNet . Common . TerminalModes , uint > ( ) ;
323+ _sshStream = _sshClient . CreateShellStream ( "bgtTerm" , 255 , 50 , 800 , 600 , 1024 , modes ) ;
324+
325+ _sshStream . DataReceived += async ( object sender , ShellDataEventArgs e ) =>
326+ {
327+ if ( _sshStream != null && _sshStream . CanRead )
328+ {
329+ byte [ ] buffer = new byte [ 2048 ] ;
330+ int i = 0 ;
331+
332+ if ( ( i = await _sshStream . ReadAsync ( buffer , 0 , buffer . Length ) ) != 1 )
333+ {
334+ _terminalData . Add ( _sshClient . ConnectionInfo . Encoding . GetString ( buffer , 0 , i ) ) ;
335+ }
336+ }
337+ } ;
338+
332339 if ( _sshClient . IsConnected )
340+ {
333341 _terminalData . Add ( "Connected to " + _sshServer ) ;
342+ }
334343 else
335344 {
336345 _terminalData . Add ( "There was a problem connecting." ) ;
@@ -352,28 +361,7 @@ private string SendCommandSSH(string command, bool silent = false)
352361 {
353362 try
354363 {
355- SshCommand sshCommand = _sshClient . CreateCommand ( command ) ;
356- string result = sshCommand . Execute ( ) ;
357-
358- StreamReader reader = new StreamReader ( sshCommand . ExtendedOutputStream ) ;
359- string extendedResult = reader . ReadToEnd ( ) ;
360-
361- if ( result . Length > 0 && ( result [ result . Length - 1 ] == '\n ' || result [ result . Length - 1 ] == '\r ' ) )
362- result = result . Substring ( 0 , result . Length - 1 ) ;
363-
364- // Handle silent calls to pwd maintain SSH current directory
365- if ( silent )
366- return result ;
367-
368- if ( extendedResult . Length > 0 && ( extendedResult [ extendedResult . Length - 1 ] == '\n ' || extendedResult [ extendedResult . Length - 1 ] == '\r ' ) )
369- extendedResult = extendedResult . Substring ( 0 , extendedResult . Length - 1 ) ;
370-
371- if ( ! result . Equals ( String . Empty ) )
372- _terminalData . Add ( result ) ;
373-
374- if ( ! extendedResult . Equals ( String . Empty ) )
375- _terminalData . Add ( extendedResult ) ;
376-
364+ _sshStream . WriteLine ( command ) ;
377365 }
378366 catch ( Exception e )
379367 {
@@ -403,16 +391,12 @@ private void SendCommandBGT(string command)
403391 }
404392 }
405393
406- private void SendCommand ( string command )
394+ private async void SendCommand ( string command )
407395 {
408396 // Handle SSH mode
409397 if ( _sshMode )
410398 {
411- _terminalData . Add ( DirectoryPrefixCommand ( command ) ) ;
412- SendCommandSSH ( DirectoryPrefixCommand ( command ) ) ;
413-
414- if ( command . ToLower ( ) . StartsWith ( "cd" ) )
415- _sshCurrentDirectory = SendCommandSSH ( DirectoryPrefixCommand ( command ) + " && pwd" , true ) ;
399+ SendCommandSSH ( command ) ;
416400 }
417401
418402 // Background-Terminal application commands
0 commit comments