jda258 / flutter_ssh2

SSH and SFTP client for Flutter
https://pub.dartlang.org/packages/ssh
MIT License
15 stars 8 forks source link

have plans to make this lib work on Flutter Windows #3

Open insinfo opened 3 years ago

insinfo commented 3 years ago

I'm currently developing an App Flutter for windows, where I need to send commands to a linux server via SSH, this commands will make the server mount a network share and copy backup files to this share, I'm trying to do this with this SSH lib https://github.com/TerminalStudio/dartssh

I currently have this code:

class SshService {
  final Uri uri;
  final String user;
  final String pass;
  SSHClient client;
  bool isConnected = false;
  StreamController<String> outStream;

  SshService({
    this.uri,//ssh://192.168.133.13:22
    this.user,
    this.pass,
  });

  Future<dynamic> connnect() {
    var completer = Completer<bool>();
    outStream = StreamController<String>.broadcast();
    try {
      client = SSHClient(
        hostport: uri,//ssh://192.168.133.13:22
        login: user,
        print: print,
        termWidth: 80,
        termHeight: 25,
        termvar: 'xterm-256color',
        getPassword: () => utf8.encode(pass),
        response: (transport, data) async {
          //print('SshService@response: ${utf8.decode(data)}');
          outStream.add(utf8.decode(data));
        },
        success: () {
          isConnected = true;
          Future.delayed(Duration(seconds: 1)).then((value) => completer.complete());
          print('SshService@success');
        },
        disconnected: () {
          print('SshService@disconnected');
          isConnected = false;
        },
      );
    } catch (e, s) {
      print('SshService@connnect $e $s');
      //completer.complete();
      completer.completeError(e, s);
    }
    return completer.future;
  }

  Future<String> sendCommand(String cmd) async {
    var completer = Completer<String>();
    try {
      client.sendChannelData(utf8.encode(cmd));
      var isEnd = false;
      var result = '';
      outStream.stream.listen((data) {
        //isaque.neves@laravel:/var/www/dart$
        result += data;
        if (data.trim().endsWith('\$')) {
          //print('stream.listen $data');
          if (isEnd == false) {
            isEnd = true;
            completer.complete(result);
          }
        }
      });
    } catch (e, s) {
      print('SshService@sendCommand $e $s');
      completer.completeError(e, s);
    }
    return completer.future;
  }

  void close() {
    client?.disconnect('terminate');
    outStream?.close();
  }
}

but it doesn't seem quite right

jda258 commented 3 years ago

This package currently doesn't support desktop operating systems. I haven't used Windows much for quite a while now, but can you use the Linux subsystem to call ssh directly using Process.run?

sandoval-san commented 3 years ago

Opa amigo, estou com uma situação parecida, estou cria um app desktop com flutter onde preciso uma SSH FLUTTER, só que no desktop estar dando erro. o pacote é este ( https://pub.dev/packages/ssh2 ), o erro é este ( MissingPluginException(No implementation found for method listen on channel shell_sftp)). preciso resolver este problema e não sei como peço uma dica. Obrigado

insinfo commented 3 years ago

@sandoval-san eu fiz um um pacote que implementa os protocolos SSH/SCP/SFTP para usar com o Flutter desktop em cima da libssh, irei publicar no pub.dev

jda258 commented 2 years ago

@insinfo I'd be very interested to see your Flutter package when it's ready. Using libssh directly through FFI is a much better solution that I've looked at some, but haven't had the time to get to yet. Let us know if we can help with development and testing efforts.

insinfo commented 2 years ago

@jda258 @sandoval-san

I just published libssh_binding I haven't had a lot of time to document and write tests, but it's working fine for me in production, I use it for a server backup application called "fsbackup" which I'm also writing will be open source, in my internal testing i'm getting the same performance if not better than native scp, i'll be glad to get any help with documentation or improvements.

https://pub.dev/packages/libssh_binding https://github.com/insinfo/fsbackup