1
1
using CoreMeter ;
2
2
using Newtonsoft . Json ;
3
3
using Renci . SshNet ;
4
+ using Renci . SshNet . Common ;
4
5
using System ;
5
6
using System . Collections . Generic ;
6
7
using System . Collections . ObjectModel ;
@@ -41,6 +42,7 @@ public partial class MainWindow : Window
41
42
42
43
// SSH Handling
43
44
private SshClient _sshClient ;
45
+ private ShellStream _sshStream ;
44
46
private bool _sshMode = false ;
45
47
private string _sshServer = String . Empty ;
46
48
private string _sshUsername = String . Empty ;
@@ -174,18 +176,6 @@ private void OutputSSHUsage()
174
176
{
175
177
_terminalData . Add ( "Background Terminal manually handles SSH connection. (Ctrl + C to quit)" ) ;
176
178
_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 ;
189
179
}
190
180
#endregion
191
181
@@ -306,7 +296,7 @@ private void ErrorDataReceived(object sender, DataReceivedEventArgs e)
306
296
_terminalData . Add ( e . Data ) ;
307
297
}
308
298
309
- private string SendCommandSSH ( string command , bool silent = false )
299
+ private async Task < string > SendCommandSSH ( string command , bool silent = false )
310
300
{
311
301
// Handle SSH login connection
312
302
if ( _sshUsername . Equals ( String . Empty ) )
@@ -329,8 +319,27 @@ private string SendCommandSSH(string command, bool silent = false)
329
319
_terminalWindow . _passwordMode = false ;
330
320
_terminalWindow . _password = String . Empty ;
331
321
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
+
332
339
if ( _sshClient . IsConnected )
340
+ {
333
341
_terminalData . Add ( "Connected to " + _sshServer ) ;
342
+ }
334
343
else
335
344
{
336
345
_terminalData . Add ( "There was a problem connecting." ) ;
@@ -352,28 +361,7 @@ private string SendCommandSSH(string command, bool silent = false)
352
361
{
353
362
try
354
363
{
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 ) ;
377
365
}
378
366
catch ( Exception e )
379
367
{
@@ -403,16 +391,12 @@ private void SendCommandBGT(string command)
403
391
}
404
392
}
405
393
406
- private void SendCommand ( string command )
394
+ private async void SendCommand ( string command )
407
395
{
408
396
// Handle SSH mode
409
397
if ( _sshMode )
410
398
{
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 ) ;
416
400
}
417
401
418
402
// Background-Terminal application commands
0 commit comments