React Native SSH SFTP 組件

分享一下最近寫的 React Native 的 SSH / SFTP 組件,iOS 端封裝了 NMSSH,Android 端封裝了 JSch。支持 SSH 執(zhí)行命令、實(shí)時(shí) Shell 和基本的 SFTP 操作,同時(shí)支持密碼或密鑰驗(yàn)證。

Github repo: react-native-ssh-sftp


安裝

npm install react-native-ssh-sftp --save
react-native link react-native-ssh-sftp

iOS (only)

NMSSH 需要用 Pod 安裝,如果已經(jīng)有 Pod 可以直接 pod 'NMSSH'。沒(méi)有的話按以下步驟初始化后安裝:

  1. 初始化 Pod 文件:
    cd ios
    pod init
    
  2. 打開(kāi) Podfile 添加:
    target '[your project's name]' do
        pod 'NMSSH'
    end
    
  3. 安裝:
    pod install
    

手動(dòng) Link

react-native link 不好使的情況試一下手動(dòng)添加:

iOS

  1. 打開(kāi) XCode 的 project navigator,右擊 Libraries ? Add Files to [項(xiàng)目名稱]
  2. 找到 node_modules ? react-native-ssh-sftp 然后選擇 RNSSHClient.xcodeproj
  3. 打開(kāi) XCode 的 project navigator,選中你的項(xiàng)目,添加 libRNSSHClient.a 到項(xiàng)目的 Build Phases ? Link Binary With Libraries

Android

  1. 打開(kāi) android/app/src/main/java/[...]/MainActivity.java
    - 添加 import com.reactlibrary.RNSshClientPackage; 到文件開(kāi)頭的 imports 中
    - 添加 new RNSshClientPackage()getPackages() 方法的列表中
  2. android/settings.gradle 中添加以下內(nèi)容 :
    include ':react-native-ssh-sftp'
    project(':react-native-ssh-sftp').projectDir = new File(rootProject.projectDir,     '../node_modules/react-native-ssh-sftp/android')
    
  3. android/app/build.gradle 文件的 dependencies 中添加:
    compile project(':react-native-ssh-sftp')
    

演示

example

運(yùn)行演示項(xiàng)目

iOS

cd example
cd ios
pod install
cd ..
npm install
react-native run-ios

Android

cd example
npm install
react-native run-android

使用方法介紹

創(chuàng)建 client 并使用密碼驗(yàn)證

import SSHClient from 'react-native-ssh-sftp';

let client = new SSHClient('10.0.0.10', 22, 'user', 'password', (error) => {
  if (error)
    console.warn(error);
});

創(chuàng)建 client 并使用密鑰驗(yàn)證

import SSHClient from 'react-native-ssh-sftp';

let client = new SSHClient('10.0.0.10', 22, 'user', {privateKey: '-----BEGIN RSA......'}, (error) => {
  if (error)
    console.warn(error);
});
  • 密鑰驗(yàn)證的其他格式:
{privateKey: '-----BEGIN RSA......'}
{privateKey: '-----BEGIN RSA......', publicKey: 'ssh-rsa AAAAB3NzaC1yc2EA......'}
{privateKey: '-----BEGIN RSA......', publicKey: 'ssh-rsa AAAAB3NzaC1yc2EA......', passphrase: 'Password'}

關(guān)閉 client

client.disconnect();

執(zhí)行 SSH 命令

var command = 'ls -l';
client.execute(command, (error, output) => {
  if (error)
    console.warn(error);
  if (output)
    console.warn(output);
});

Shell

開(kāi)啟 shell:

  • ptyType 的可選類型: vanilla, vt100, vt102, vt220, ansi, xterm
var ptyType = 'vanilla';
client.startShell(ptyType, (error) => {
  if (error)
    console.warn(error);
});

從 shell 獲取數(shù)據(jù):

client.on('Shell', (event) => {
  if (event)
    console.warn(event);
});

向 shell 寫數(shù)據(jù):

var str = 'ls -l\n';
client.writeToShell(str, (error) => {
  if (error) 
    console.warn(error);
});

關(guān)閉 shell:

client.closeShell();

SFTP

連接 SFTP

client.connectSFTP((error) => {
  if (error)
    console.warn(error);
});

獲取目錄列表:

var path = '.';
client.sftpLs(path, (error, response) => {
  if (error)
    console.warn(error);
  if (response)
    console.warn(response);
});

創(chuàng)建目錄:

client.sftpMkdir('dirName', (error) => {
  if (error)
    console.warn(error);
});

重命名文件或目錄:

client.sftpRename('oldName', 'newName', (error) => {
  if (error)
    console.warn(error);
});

刪除目錄:

client.sftpRmdir('dirName', (error) => {
  if (error)
    console.warn(error);
});

刪除文件:

client.sftpRm('fileName', (error) => {
  if (error)
    console.warn(error);
});

下載文件:

client.sftpDownload('[path-to-remote-file]', '[path-to-local-direcotry]', (error, downloadedFilePath) => {
  if (error)
    console.warn(error);
  if (downloadedFilePath)
    console.warn(downloadedFilePath);
});

// 獲取下載進(jìn)度
client.on('DownloadProgress', (event) => {
  console.warn(event);
});

// 取消下載
client.sftpCancelDownload();

上傳文件:

client.sftpUpload('[path-to-local-file]', '[path-to-remote-directory]', (error) => {
  if (error)
    console.warn(error);
});

// 獲取上傳進(jìn)度
client.on('UploadProgress', (event) => {
  console.warn(event);
});

// 取消上傳
client.sftpCancelUpload();

斷開(kāi) SFTP:

client.disconnectSFTP();
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容