1.0.3 • Published 2 years ago

ssh-package v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

SSH-Package

Small package to transfer data with ssh using SFTP.

Installation

Using npm

$ npm i ssh-package

Using yarn

$ yarn add ssh-package

Start connection

import { Client } from "ssh-package";

const connection = new Client({
  host: "hostIp",
  username: "username",
  password: "password",
  port: port, // default = 22
});

connection.on("ready", async () => {
  // do stuff
});

Transfer data

Download file(s)

connection.download
  .file(remotePath, localPath)
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log("Error: ", err);
  });

// async
try {
  const response = await connection.download.files([
    { remote: remotePath1, local: localPath1 },
    { remote: remotePath2, local: localPath2 },
  ]);
} catch (err) {
  console.log("Error: ", err);
}

Download directory(ies)

connection.download
  .directory(remotePath, localPath)
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log("Error: ", err);
  });

// async
try {
  const response = await connection.download.directories([
    { remote: remotePath1, local: localPath1 },
    { remote: remotePath2, local: localPath2 },
  ]);
} catch (err) {
  console.log("Error: ", err);
}

Upload file(s)

connection.upload
  .file(localPath, remotePath)
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log("Error: ", err);
  });

// async
try {
  const response = await connection.upload.files([
    { remote: remotePath1, local: localPath1 },
    { remote: remotePath2, local: localPath2 },
  ]);
} catch (err) {
  console.log("Error: ", err);
}

Upload directory(ies)

connection.upload
  .directory(localPath, remotePath)
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log("Error: ", err);
  });

// async
try {
  const response = await connection.upload.directories([
    { remote: remotePath1, local: localPath1 },
    { remote: remotePath2, local: localPath2 },
  ]);
} catch (err) {
  console.log("Error: ", err);
}

Execute command

connnection
  .exec("ls -l")
  .then((res) => {
    console.log(res);
  })
  .catch((e) => {
    console.log("Error: ", e);
  });

// async
try {
  const response = await connnection.exec("ls -l", {
    cwd: "/home/user",
    encoding: "utf-8",
  });
} catch (err) {
  console.log("Error: ", err);
}

Create directory

connnection
  .mkdir("/path/to/new/directory")
  .then((res) => {
    console.log(res);
  })
  .catch((e) => {
    console.log("Error: ", e);
  });

// async
try {
  const response = await connection.mkdir("/path/to/new/directory");
} catch (err) {
  console.log("Error: ", err);
}
1.0.3

2 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago