/react-native-ssh-sftp

SSH/SFTP client for React Native

Primary LanguageJavaMIT LicenseMIT

react-native-ssh-sftp

SSH and SFTP client library for React Native.

Installation

This fork of the library is not published on npmjs.

If you have access to our own repository you can install using:

npm install @keeex/react-native-ssh-sftp

It is otherwise possible to install the package by hand. First, clone this repository; install dependencies, then build package. Finally, install in your project.

/ $ git clone https://github.com/KeeeX/react-native-ssh-sftp.git
/ $ cd react-native-ssh-sftp
/react-native-ssh-sftp $ npm install
/react-native-ssh-sftp $ npm pack
/react-native-ssh-sftp $ cd ../your_project
/your_project $ npm install ../react-native-ssh-sftp/keeex-react-native-ssh-sftp-1.1.1.tgz

iOS (only)

(procedure below is untested on recent version)

NMSSH is required for iOS.

  1. Initialize Pod:
    cd ios
    pod init
    
  2. Open Podfile and add:
    target '[your project's name]' do
    	pod 'NMSSH', '2.2.8'
    end
    
  3. Install Pod:
    pod install
    

Manual Link

iOS

(not that this should not be required anymore)

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-ssh-sftp and add RNSSHClient.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNSSHClient.a to your project's Build PhasesLink Binary With Libraries

Android

Don't manual link.

Demo

example

  • 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

All functions that run asynchronously where we have to wait for a result returns Promises that can reject if an error occured.

Create a client using password authentication

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

SSHClient.connectWithPassword(
  "10.0.0.10",
  22,
  "user",
  "password"
).then(client => {/*...*/});

Create a client using public key authentication

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

SSHClient.connectWithKey(
  "10.0.0.10",
  22,
  "user",
  privateKey="-----BEGIN RSA...",
  passphrase
).then(client => {/*...*/});
  • 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.disconnect();

Execute SSH command

var command = 'ls -l';
client.execute(command)
  .then(output => console.warn(output));

Shell

Start shell:

  • Supported ptyType: vanilla, vt100, vt102, vt220, ansi, xterm
var ptyType = 'vanilla';
client.startShell(ptyType)
  .then(() => {/*...*/});

Read from shell:

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

Write to shell:

var str = 'ls -l\n';
client.writeToShell(str)
  .then(() => {/*...*/});

Close shell:

client.closeShell();

SFTP

Connect SFTP

client.connectSFTP()
  .then(() => {/*...*/});

List directory:

var path = '.';
client.sftpLs(path)
  .then(response => console.warn(response));

Create directory:

client.sftpMkdir('dirName')
  .then(() => {/*...*/});

Rename file or directory:

client.sftpRename('oldName', 'newName')
  .then(() => {/*...*/});

Remove directory:

client.sftpRmdir('dirName')
  .then(() => {/*...*/});

Remove file:

client.sftpRm('fileName')
  .then(() => {/*...*/});

Download file:

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

// Download progress (setup before call)
client.on('DownloadProgress', (event) => {
  console.warn(event);
});

// Cancel download:
client.sftpCancelDownload();

Upload file:

client.sftpUpload('[path-to-local-file]', '[path-to-remote-directory]')
  .then(() => {/*...*/});

// Upload progress (setup before call)
client.on('UploadProgress', (event) => {
  console.warn(event);
});

// Cancel upload:
client.sftpCancelUpload();

Close SFTP:

client.disconnectSFTP();

Credits

  • iOS SSH library: NMSSH
  • Android SSH library: JSch