React Native SSH SFTP 组件

分享一下最近写的 React Native 的 SSH / SFTP 组件,iOS 端封装了 NMSSH,Android 端封装了 JSch。支持 SSH 执行命令、实时 Shell 和基本的 SFTP 操作,同时支持密码或密钥验证。

Github repo: react-native-ssh-sftp


安装

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

iOS (only)

NMSSH 需要用 Pod 安装,如果已经有 Pod 可以直接 pod 'NMSSH'。没有的话按以下步骤初始化后安装:

  1. 初始化 Pod 文件:
    cd ios
    pod init
    
  2. 打开 Podfile 添加:
    target '[your project's name]' do
        pod 'NMSSH'
    end
    
  3. 安装:
    pod install
    

手动 Link

react-native link 不好使的情况试一下手动添加:

iOS

  1. 打开 XCode 的 project navigator,右击 LibrariesAdd Files to [项目名称]
  2. 找到 node_modulesreact-native-ssh-sftp 然后选择 RNSSHClient.xcodeproj
  3. 打开 XCode 的 project navigator,选中你的项目,添加 libRNSSHClient.a 到项目的 Build PhasesLink Binary With Libraries

Android

  1. 打开 android/app/src/main/java/[...]/MainActivity.java
    - 添加 import com.reactlibrary.RNSshClientPackage; 到文件开头的 imports 中
    - 添加 new RNSshClientPackage()getPackages() 方法的列表中
  2. android/settings.gradle 中添加以下内容 :
    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
  • 我的 App 树莓派助手 也在使用这个组件

运行演示项目

iOS

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

Android

cd example
npm install
react-native run-android

使用方法介绍

创建 client 并使用密码验证

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

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

创建 client 并使用密钥验证

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);
});
  • 密钥验证的其他格式:
{privateKey: '-----BEGIN RSA......'}
{privateKey: '-----BEGIN RSA......', publicKey: 'ssh-rsa AAAAB3NzaC1yc2EA......'}
{privateKey: '-----BEGIN RSA......', publicKey: 'ssh-rsa AAAAB3NzaC1yc2EA......', passphrase: 'Password'}

关闭 client

client.disconnect();

执行 SSH 命令

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

Shell

开启 shell:

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

从 shell 获取数据:

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

向 shell 写数据:

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

关闭 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);
});

创建目录:

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);
});

// 获取下载进度
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);
});

// 获取上传进度
client.on('UploadProgress', (event) => {
  console.warn(event);
});

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

断开 SFTP:

client.disconnectSFTP();

你可能感兴趣的:(React Native SSH SFTP 组件)