搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案