Skip to content

Spiria-Digital/react-native-ssh-sftp

 
 

Repository files navigation

react-native-ssh-sftp

SSH and SFTP client library for React Native.

Installation

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

yarn add react-native-ssh-sftp --save
Demo (deprecated)

Usage

Create a client using password authentication

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

let client = new SSHClient("10.0.0.10", 22, "user", "password");
await client.connect();

Create a client using public key authentication

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

let client = new SSHClient("10.0.0.10", 22, "user", { privateKey: "-----BEGIN RSA......" });

await client.connect();
  • 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";
const output = await client.execute(command);

Shell

Start shell:

  • Supported ptyType: vanilla, vt100, vt102, vt220, ansi, xterm
var ptyType = "vanilla";
await client.startShell(ptyType);

Read from shell:

await client.on("Shell");

Write to shell:

var str = "ls -l\n";
await client.writeToShell(str);

Close shell:

client.closeShell();

SFTP

Connect SFTP

await client.connectSFTP();

List directory:

var path = ".";
const response = await client.sftpLs(path);

Create directory:

await client.sftpMkdir("/tmp/dirName");

Rename file or directory:

await client.sftpRename("/tmp/test.tar.gz", "/tmp/test1.tar.gz");

Remove directory:

await client.sftpRmdir("/tmp");

Remove file:

await client.sftpRm("/tmp/test.tar.gz");

Download file:

// Download progress
client.on("DownloadProgress", (percentage) => {
  console.warn(percentage);
});

const downloadedFilePath = await client.sftpDownload(
  "[path-to-remote-file]",
  "[path-to-local-direcotry]"
);

// Cancel download:
client.sftpCancelDownload();

Upload file:

// Upload progress
client.on("UploadProgress", (percentage) => {
  console.warn(percentage);
});

await client.sftpUpload("[path-to-local-file]", "[path-to-remote-directory]");

// Cancel upload:
client.sftpCancelUpload();

Close SFTP (Android only):

client.disconnectSFTP();

Credits

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

About

SSH/SFTP client for React Native

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Objective-C 37.0%
  • JavaScript 29.6%
  • Java 29.6%
  • Starlark 2.6%
  • Ruby 1.2%