native react ssh_React Native SSH SFTP 组件

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

安装

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

react-native link react-native-ssh-sftp

iOS (only)

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

初始化 Pod 文件:

cd ios

pod init

打开 Podfile 添加:

target '[your project's name]' do

pod 'NMSSH'

end

安装:

pod install

手动 Link

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

iOS

打开 XCode 的 project navigator,右击 Libraries ➜ Add Files to [项目名称]

找到 node_modules ➜ react-native-ssh-sftp 然后选择 RNSSHClient.xcodeproj

打开 XCode 的 project navigator,选中你的项目,添加 libRNSSHClient.a 到项目的 Build Phases ➜ Link Binary With Libraries

Android

打开 android/app/src/main/java/[...]/MainActivity.java

- 添加 import com.reactlibrary.RNSshClientPackage; 到文件开头的 imports 中

- 添加 new RNSshClientPackage() 到 getPackages() 方法的列表中

在 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')

在 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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值