shaqian / flutter_ssh

SSH and SFTP client for Flutter
https://pub.dartlang.org/packages/ssh
MIT License
117 stars 83 forks source link
dart flutter sftp ssh

ssh

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

Installation

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

Known issue

Usage

Create a client using password authentication

import 'package:ssh/ssh.dart';

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

Create a client using public key authentication

import 'package:ssh/ssh.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",
  },
);

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.

Connect client

await client.connect();

Close client

await client.disconnect();

Execute SSH command

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

Shell

Start shell:

Write to shell:

await client.writeToShell("ls\n");

Close shell:

await client.closeShell();

SFTP

Connect SFTP:

await client.connectSFTP();

List directory:

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

Create directory:

await client.sftpMkdir("testdir");

Rename file or directory:

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

Remove directory:

await client.sftpRmdir("testdir");

Remove file:

await client.sftpRm("testfile");

Download file:

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

// Cancel download:
await client.sftpCancelDownload();

Upload file:

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

// Cancel upload:
await client.sftpCancelUpload();

Close SFTP:

await client.disconnectSFTP();

Demo

Refer to the example.