dartssh 1.0.3+3

Library providing a pure Dart SSH implementation with tunneling.

example/dartssh.dart

// Copyright 2019 dartssh developers
// Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

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

import 'package:args/args.dart';

import 'package:dartssh/client.dart';
import 'package:dartssh/identity.dart';
import 'package:dartssh/pem.dart';
import 'package:dartssh/ssh.dart';
import 'package:dartssh/transport.dart';

Identity identity;
SSHClient client;
Channel forwardChannel;

void main(List<String> arguments) async {
  stdin.lineMode = false;
  stdin.echoMode = false;
  exitCode = await ssh(
      arguments, stdin, (_, String v) => stdout.write(v), () => exit(0));
}

Future<int> ssh(List<String> arguments, Stream<List<int>> input,
    ResponseCallback response, VoidCallback done) async {
  final argParser = ArgParser()
    ..addOption('login', abbr: 'l')
    ..addOption('identity', abbr: 'i')
    ..addOption('tunnel')
    ..addOption('kex')
    ..addOption('key')
    ..addOption('cipher')
    ..addOption('mac')
    ..addOption('debug')
    ..addOption('trace');

  final ArgResults args = argParser.parse(arguments);

  if (args.rest.length != 1) {
    print('usage: ssh -l login url [args]');
    print(argParser.usage);
    return 1;
  }

  final String host = args.rest.first,
      login = args['login'],
      identityFile = args['identity'],
      tunnel = args['tunnel'];

  if (login == null || login.isEmpty) {
    print('no login specified');
    return 1;
  }

  if (tunnel != null && tunnel.split(':').length != 2) {
    print('tunnel target should be specified host:port');
    return 2;
  }

  applyCipherSuiteOverrides(
      args['kex'], args['key'], args['cipher'], args['mac']);

  try {
    client = SSHClient(
        hostport: parseUri(host),
        login: login,
        print: print,
        debugPrint: ((args['debug'] != null) ? print : null),
        tracePrint: ((args['trace'] != null) ? print : null),
        response: response,
        loadIdentity: () {
          if (identity == null && identityFile != null) {
            identity = parsePem(File(identityFile).readAsStringSync());
          }
          return identity;
        },
        disconnected: done,
        startShell: tunnel == null,
        success: tunnel == null
            ? null
            : () {
                List<String> tunnelTarget = tunnel.split(':');
                forwardChannel = client.openTcpChannel(
                    '127.0.0.1',
                    1234,
                    tunnelTarget[0],
                    int.parse(tunnelTarget[1]),
                    (_, Uint8List m) => response(client, utf8.decode(m)));
              });

    await for (String x in input.transform(utf8.decoder)) {
      if (forwardChannel != null) {
        client.sendToChannel(forwardChannel, utf8.encode(x));
      } else {
        client.sendChannelData(utf8.encode(x));
      }
    }
  } catch (error, stacktrace) {
    print('ssh: exception: $error: $stacktrace');
    return -1;
  }

  return 0;
}
7
likes
70
pub points
50%
popularity

Library providing a pure Dart SSH implementation with tunneling.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

greenappers@gmail.com

License

MIT (LICENSE)

Dependencies

asn1lib, convert, http, meta, pedantic, pointycastle, tweetnacl, validators

More

Packages that depend on dartssh