react-native-ssh-sftp
SSH and SFTP client library for React Native.
Installation
npm install react-native-ssh-sftp --save
react-native link react-native-ssh-sftp
iOS (only)
NMSSH is required for iOS.
- Initialize Pod:
cd ios pod init
- Open Podfile and add:
target '[your project's name]' do pod 'NMSSH' end
- Install Pod:
pod install
Manual Link
iOS
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-ssh-sftp
and addRNSSHClient.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNSSHClient.a
to your project'sBuild Phases
➜Link Binary With Libraries
Android
- Open up
android/app/src/main/java/[...]/MainActivity.java
- Addimport com.reactlibrary.RNSshClientPackage;
to the imports at the top of the file - Addnew RNSshClientPackage()
to the list returned by thegetPackages()
method - Append the following lines to
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')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-ssh-sftp')
Demo
- This library is also used in iOS app PiHelper.
Run demo
iOS
cd example
cd ios
pod install
cd ..
npm install
react-native run-ios
Android
cd example
npm install
react-native run-android
Usage
Create a client using password authentication
; let client = '10.0.0.10' 22 'user' 'password' { if error console;};
Create a client using public key authentication
; let client = '10.0.0.10' 22 'user' privateKey: '-----BEGIN RSA......' { if error console;};
- Public key authentication also supports:
{privateKey: '-----BEGIN RSA......'}
{privateKey: '-----BEGIN RSA......', publicKey: 'ssh-rsa AAAAB3NzaC1yc2EA......'}
{privateKey: '-----BEGIN RSA......', publicKey: 'ssh-rsa AAAAB3NzaC1yc2EA......', passphrase: 'Password'}
Close client
client;
Execute SSH command
var command = 'ls -l';client;
Shell
Start shell:
- Supported ptyType: vanilla, vt100, vt102, vt220, ansi, xterm
var ptyType = 'vanilla';client;
Read from shell:
client;
Write to shell:
var str = 'ls -l\n';client;
Close shell:
client;
SFTP
Connect SFTP
client;
List directory:
var path = '.';client;
Create directory:
client;
Rename file or directory:
client;
Remove directory:
client;
Remove file:
client;
Download file:
client; // Downlowd progressclient; // Cancel download:client;
Upload file:
client; // Upload progressclient; // Cancel upload:client;
Close SFTP:
client;