execute method

Future<SSHSession> execute(
  1. String command, {
  2. SSHPtyConfig? pty,
  3. Map<String, String>? environment,
})

Execute command on the remote side. Returns a SSHChannel that can be used to read and write to the remote side.

Implementation

Future<SSHSession> execute(
  String command, {
  SSHPtyConfig? pty,
  Map<String, String>? environment,
}) async {
  await _authenticated.future;

  final channelController = await _openSessionChannel();

  if (environment != null) {
    for (var pair in environment.entries) {
      channelController.sendEnv(pair.key, pair.value);
    }
  }

  if (pty != null) {
    final ptyOk = await channelController.sendPtyReq(
      terminalType: pty.type,
      terminalWidth: pty.width,
      terminalHeight: pty.height,
      terminalPixelWidth: pty.pixelWidth,
      terminalPixelHeight: pty.pixelHeight,
    );
    if (!ptyOk) {
      channelController.close();
      throw SSHChannelRequestError('Failed to start pty');
    }
  }

  final success = await channelController.sendExec(command);
  if (!success) {
    channelController.close();
    throw SSHChannelRequestError('Failed to execute');
  }

  return SSHSession(channelController.channel);
}