Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
insinfo opened this issue Aug 27, 2021 · 0 comments
Open

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

insinfo opened this issue Aug 27, 2021 · 0 comments

Comments

@insinfo
Copy link

insinfo commented Aug 27, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant