dartssh2 icon indicating copy to clipboard operation
dartssh2 copied to clipboard

Listening to SSH output

Open MA171 opened this issue 1 year ago • 4 comments

Hi, I'm trying to execute a large python script over ssh, and I want to read all the output without waiting for it to finish executing. I tried to do it over SSHSession and listening to the output stream but it only trigger the event when the output is finished. Is there anyway to listed to the output without waiting for the script to finish executing? Thanks!

MA171 avatar Sep 18 '22 20:09 MA171

This can be done by listening to SSHSession.stdout and SSHSession.stderr.

Here is the example:

import 'dart:convert';
import 'dart:io';

import 'package:dartssh2/dartssh2.dart';

void main() async {
  final client = SSHClient(
    await SSHSocket.connect('localhost', 22),
    username: '<username>',
    onPasswordRequest: () => '<password>',
  );

  final session = await client.execute('ping 1.1.1.1');

  await for (final chunk in session.stdout.cast<List<int>>().transform(const Utf8Decoder())) {
    print('chunk: "$chunk"');
  }
}

https://user-images.githubusercontent.com/15033141/190933676-47912af0-e8ba-41f1-9b5a-6bbc36a08dd7.mp4

xtyxtyx avatar Sep 19 '22 00:09 xtyxtyx

I tried to listen to the stdout stream, the listener only received the whole output at the end of the execution, but when I switched the output to stderr and listened to it, the listener got the output right.

MA171 avatar Sep 19 '22 01:09 MA171

Did you mean that you got exactly the same output from stdin and stdout, and the only difference is that the output from stderr is chunked?

xtyxtyx avatar Sep 19 '22 01:09 xtyxtyx

the above example code final session = await client.execute('ping 1.1.1.1'); works perfectly fine for getting some ping results but, it fails to get output when I do for commands like await client.execute('tail -f /tmp;/logs.txt')

Any solution for this @xtyxtyx ?

Sofstica-Abdul-Bari avatar Jan 03 '23 10:01 Sofstica-Abdul-Bari