搜索插件、代码仓与 APP 分类

插件详情v2.2.3

ssh2

适用于 Flutter 的 SSH 和 SFTP 客户端。封装了 iOS 库 NMSSH 和 Android 库 JSch。

AndroidiOS
近 30 天下载
60
喜欢数
32
Pub 分数
130

安装插件

flutter pub add ssh2

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 包,通过封装原生库 NMSSH(iOS)和 JSch(Android),在 Android 和 iOS 上实现 SSH 和 SFTP 连接。它允许安全地访问远程服务器和传输文件,非常适合需要与服务器进行加密通信的应用程序。该包提供了一个简单的接口,用于建立连接、执行命令以及通过 SFTP 管理文件。在涉及远程配置、部署或数据同步的场景中尤为有用。开发者可以在保持跨平台一致性的同时,享受平台特定的优化。

适合场景

  • 远程服务器管理
  • 通过 SFTP 安全传输文件
  • 自动化部署工作流
  • 服务器配置自动化
  • 远程命令执行
  • 与远程系统的数据同步

主要特点

  • 跨平台 SSH 和 SFTP 支持
  • 原生库集成(NMSSH,JSch)
  • 安全加密连接
  • 远程服务器上的命令执行
  • 文件传输功能
  • 平台特定的性能优化

适合对象

  • 开发远程管理工具的开发者
  • 需要安全服务器访问的应用程序
  • CI/CD 集成功能
  • 物联网设备配置应用
  • 需要安全文件处理的应用程序

使用建议

  • 需要 iOS 和 Android 的原生依赖项
  • 仅限 Android 和 iOS 平台
  • 可能需要为防火墙或密钥认证进行额外设置
  • 不适用于网络或桌面目标
  • 处理 SSH 密钥和证书时可能出现的复杂性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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.",
);
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",
  },
);
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();
Close client
await client.disconnect();
Execute SSH command
var result = await client.execute("ps");
Get the fingerprint of the remote host
var result = await client.getHostFingerprint();
Get the banner of the remote host
var result = await client.getBanner();
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
  }
);
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.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目