dartssh2
dartssh2 copied to clipboard
Listening to SSH output
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!
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
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.
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?
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 ?