ssh2 2.2.3 copy "ssh2: ^2.2.3" to clipboard
ssh2: ^2.2.3 copied to clipboard

SSH and SFTP client for Flutter. Wraps iOS library NMSSH and Android library JSch.

ssh2 #

SSH and SFTP client for Flutter. Wraps iOS library NMSSH and Android library JSch. This is a fork of flutter_ssh: https://github.com/shaqian/flutter_ssh

Wraps iOS library NMSSH and Android library JSch.

Installation #

Add ssh2 as a dependency in your pubspec.yaml file.

Known issue #

  • Older Gradle versions are not supported due to incompatibilities with the newer JSch dependency. This release was tested with Gradle version 7.0.2.

Usage #

Create a client using password authentication #

import 'package:ssh2/ssh2.dart';

var client = new SSHClient(
  host: "my.sshtest",
  port: 22,
  username: "sha",
  passwordOrKey: "Password01.",
);
copied to clipboard

Create a client using public key authentication #

import 'package:ssh2/ssh2.dart';

var client = new SSHClient(
  host: "my.sshtest",
  port: 22,
  username: "sha",
  passwordOrKey: {
    "privateKey": """-----BEGIN RSA PRIVATE KEY-----
    ......
-----END RSA PRIVATE KEY-----""",
    "passphrase": "passphrase-for-key",
  },
);
copied to clipboard

OpenSSH keys

Recent versions of OpenSSH introduce a proprietary key format that is not supported by most other software, including this one, you must convert it to a PEM-format RSA key using the puttygen tool. On Windows this is a graphical tool. On the Mac, install it per these instructions. On Linux install your distribution's putty or puttygen packages.

  • Temporarily remove the passphrase from the original key (enter blank password as the new password)
    ssh-keygen -p -f id_rsa
  • convert to RSA PEM format
    puttygen id_rsa -O private-openssh -o id_rsa_unencrypted
  • re-apply the passphrase to the original key
    ssh-keygen -p -f id_rsa
  • apply a passphrase to the converted key:
    puttygen id_rsa_unencrypted -P -O private-openssh -o id_rsa_flutter
  • remove the unencrypted version:
    rm id_rsa_unencrypted

Connect client #

await client.connect();
copied to clipboard

Close client #

await client.disconnect();
copied to clipboard

Execute SSH command #

var result = await client.execute("ps");
copied to clipboard

Get the fingerprint of the remote host #

var result = await client.getHostFingerprint();
copied to clipboard

Get the banner of the remote host #

var result = await client.getBanner();
copied to clipboard

Shell #

Start shell:

  • Supported ptyType: vanilla, vt100, vt102, vt220, ansi, xterm
var result = await client.startShell(
  ptyType: "xterm", // defaults to vanilla
  callback: (dynamic res) {
    print(res);     // read from shell
  }
);
copied to clipboard

Write to shell:

await client.writeToShell("ls\n");
copied to clipboard

Close shell:

await client.closeShell();
copied to clipboard

SFTP #

Connect SFTP:

await client.connectSFTP();
copied to clipboard

List directory:

var array = await client.sftpLs("/home"); // defaults to .
copied to clipboard

Create directory:

await client.sftpMkdir("testdir");
copied to clipboard

Rename file or directory:

await client.sftpRename(
  oldPath: "testfile",
  newPath: "newtestfile",
);
copied to clipboard

Remove directory:

await client.sftpRmdir("testdir");
copied to clipboard

Remove file:

await client.sftpRm("testfile");
copied to clipboard

Download file:

var filePath = await client.sftpDownload(
  path: "testfile",
  toPath: tempPath,
  callback: (progress) {
    print(progress); // read download progress
  },
);

// Cancel download:
await client.sftpCancelDownload();
copied to clipboard

Upload file:

await client.sftpUpload(
  path: filePath,
  toPath: ".",
  callback: (progress) {
    print(progress); // read upload progress
  },
);

// Cancel upload:
await client.sftpCancelUpload();
copied to clipboard

Close SFTP:

await client.disconnectSFTP();
copied to clipboard

Demo #

Refer to the example.

33
likes
140
points
382
downloads

Publisher

unverified uploader

Weekly Downloads

2024.09.21 - 2025.04.05

SSH and SFTP client for Flutter. Wraps iOS library NMSSH and Android library JSch.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, uuid

More

Packages that depend on ssh2