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

插件详情v0.0.7

ssh

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

近 30 天下载
83
喜欢数
42
Pub 分数
35

安装插件

flutter pub add ssh

插件介绍

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

一个 Flutter 包,通过封装原生库(iOS 使用 NMSSH,Android 使用 JSch)提供 SSH 和 SFTP 客户端功能。它使 Flutter 应用能够实现安全的远程连接和文件传输,支持通过密码或密钥进行身份验证。该包专为需要跨平台网络操作且使用安全协议的开发者设计,简化了将基于 SSH 的工作流程集成到移动应用中的过程,而无需深入了解特定平台的知识。

适合场景

  • 远程服务器管理
  • 通过 SFTP 安全传输文件
  • 自动化部署脚本
  • 安全设备配置
  • 远程调试
  • 系统管理任务

主要特点

  • 跨平台 SSH/SFTP 支持
  • 密码和密钥认证
  • 与 iOS NMSSH 和 Android JSch 的集成
  • 用于连接和文件操作的简单 API
  • 安全加密通信

适合对象

  • 开发人员构建管理工具
  • 需要远程访问的移动应用
  • 移动应用中的CI/CD集成
  • 网络自动化项目
  • 处理安全数据交换的应用程序

使用建议

  • iOS 和 Android 需要原生库设置
  • 仅限iOS和Android平台
  • 身份验证凭据必须安全处理
  • 低端设备上可能存在性能开销
  • 没有用于SSH会话的内置UI组件

原始 README

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

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

  • Platform exception in release mode for Android:

    PlatformException(connection_failure, java.lang.ClassNotFoundException: com.jcraft.jsch.jce.Random, null)
    

    There are 2 workarounds:

    • Disable shrink:

      flutter build apk --no-shrink

    • Configure proguard-rules. Refer to this comment for details.

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.

  • 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");
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.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目