• This repository has been archived on 05/Feb/2022
  • Stars
    star
    381
  • Rank 112,474 (Top 3 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[MAINTAINER WANTED] A pure javascript scp program based on ssh2.

scp2

A pure javascript secure copy program based on ssh2.


scp2 is greatly powered by ssh2, implementing scp in an sftp way.

It is written in pure javascript, and should work on every OS, even Windows. Nodejs (v0.8.7 or newer) is required to use this library.

Install

You can either use it as a library, or via CLI. For Windows users who miss scp in the unix/linux world, you can get it with:

$ npm install scp2 -g

You will get a command line tool scp2, now let's try:

$ scp2 -h

To get the development version from the ninja channel:

$ npm install scp2@ninja

High level API

Get the client:

var client = require('scp2')

Copy a file to the server:

client.scp('file.txt', 'admin:[email protected]:/home/admin/', function(err) {
})

You can also add the port to the url (default is 22):

client.scp('file.txt', 'admin:[email protected]:port:/home/admin/', function(err) {
})

Copy a file to the server specifying the destination as an object:

client.scp('file.txt', {
    host: 'example.com',
    username: 'admin',
    password: 'password',
    path: '/home/admin/'
}, function(err) {})

Copy a file to the server and rename it:

client.scp('file.txt', 'admin:[email protected]:/home/admin/rename.txt', function(err) {
})

Copy a directory to the server:

client.scp('data/', 'admin:[email protected]:/home/admin/data/', function(err) {
})

Copy via glob pattern:

client.scp('data/*.js', 'admin:[email protected]:/home/admin/data/', function(err) {
})

Download a file from the server:

client.scp('admin:[email protected]:/home/admin/file.txt', './', function(err) {
})

Download a file from the server specifying the destination as an object:

client.scp({
    host: 'example.com',
    username: 'admin',
    password: 'password',
    path: '/home/admin/file.txt'
}, './', function(err) {})

Login with a private key:

client.scp('file.txt', {
    host: 'example.com',
    username: 'admin',
    privateKey: require("fs").readFileSync('path/to/private/key'),
    passphrase: 'private_key_password',
    path: '/home/admin/'
}, function(err) {})

TODO: download via glob pattern.

Low level API

Get the client:

var Client = require('scp2').Client;

The high level client is an instance of Client, but also contains the high level API scp.

Methods

  • defaults function({})

    set the default values for the remote server.

    client.defaults({
        port: 22,
        host: 'example.com',
        username: 'admin',
        privateKey: '....',
        // password: 'password', (accepts password also)
    });

    You can also initialize the instance with these values:

    var client = new Client({
        port: 22
    });

    More on these values at ssh2.

  • sftp function(callback) -> callback(err, sftp)

    Get the sftp object.

  • close function()

    Close all sessions.

  • mkdir function(dir, [attr], callback) -> callback(err)

    Make a directory on the remote server. It behaves like mdkir -p.

  • write function(options, callback) -> callback(err)

    Write content on the remote server.

    client.write({
        destination: '/home/admin/data/file.txt',
        content: 'hello world'
    }, callback)

    The options object can contain:

    • destination
    • content: string or buffer
    • attrs
    • source: the source path, e.g. local/file.txt
  • upload function(src, dest, callback) -> callback(err)

    upload a local file to the server.

    client.upload('file.txt', '/home/admin/file.txt', callback)
  • download function(src, dest, callback) -> callback(err)

    download a file from the server to local.

Events

You can listen for these events:

  • connect
  • ready
  • error (err)
  • end
  • close
  • mkdir (dir)
  • write (object)
  • read (src)
  • transfer (buffer, uploaded, total)

Changelog

2016-05-29 0.4.0

  • enable port

2016-05-20 0.4.0

  • upgrade glob

2016-04-18 0.3.0

  • Retain compatible with old implementations
  • Added parameter to scp to allow custom client
  • Add password to client object options
  • Fix scp from windows client with folder
  • Update version of ssh2 to 0.4.10
  • Add pass arguments to callback
  • Fix Closes conection when downloading a single file

2015-06-01 0.2.2

  1. Added error callback to download prototype
  2. Adding Client to scp module exports

2015-01-09 0.2.1

  1. Bugfix

2014-10-30 0.2.0

  1. Fixed the issue with corrupt download when file Size more than 65K
  2. Raising error on remote file not found
  3. Fix mode of all files being set to 0755
  4. Dependencies upgrade

2013-11-07 0.1.4 ~stable

  1. Bugfix

2013-06-04 0.1.3 ~stable

  1. Fixed mkdir mode bug

2013-06-04 0.1.2 ~stable

  1. Fixed for uploading a large file (beyond the limitation of fs.readFile)
  2. Event emit for transfer

2013-06-03 0.1.1 ~stable

  1. Bugfix for scp a large file.

2013-03-08 0.1.0 ~ stable

  1. remove the require of buffer, Buffer is on global

2013-03-07 0.1.0b1 ~ ninja

  1. show version options on binary
  2. bugfix of upload, it should mkdir right

2013-03-06 0.1.0a3 ~ ninja

  1. Fix path bug on windows.
  2. Pretty output log.

2013-03-06 0.1.0a2 ~ ninja

  1. Download a file from server works.
  2. Documentation on this lib.

2013-03-05 0.1.0a1 ~ ninja

  1. Init the program, take the name scp2 in npmjs.org.
  2. scp to server works.

License

Copyright (c) 2013 Hsiaoming Yang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

spm

Brand new static package manager.
JavaScript
903
star
2

spm2

ๆบ็ ๅทฒๅˆๅนถๅˆฐ
JavaScript
126
star
3

spmjs.io

Packaging manager server for spm
JavaScript
80
star
4

docs

Documents for spm 3.6 and above.
72
star
5

grunt-cmd-transport

Transport everything into common module.
JavaScript
70
star
6

spm-build

Build tool for spm.
JavaScript
36
star
7

grunt-scp

Copy files to remote server.
JavaScript
34
star
8

grunt-cmd-concat

concat cmd files.
JavaScript
30
star
9

yuan

yuan is a distributed packaging system for spmjs.org
Python
23
star
10

spm-webpack

SPM project bundler based on webpack.
JavaScript
23
star
11

examples

Project examples for spm.
CSS
20
star
12

spm-server

Server for debug with spm packages.
JavaScript
19
star
13

spm-yuan

A simple spm source server
JavaScript
16
star
14

cmd-util

Utilities for common module definition.
JavaScript
15
star
15

serve-spm

Middleware for spm debug, support connect, express and koa.
JavaScript
12
star
16

gulp-spm

gulp plugin for cmd transport
JavaScript
11
star
17

spm-client

spm client sdk
JavaScript
10
star
18

spm-sea

SPM project bundler in seajs.
JavaScript
9
star
19

hello-spm

spm hello world
JavaScript
9
star
20

spm-webpack-server

Server for spm projects.
JavaScript
8
star
21

spm-init

spm init template plugin
JavaScript
5
star
22

spm-grunt

A bridge between spm and grunt.
JavaScript
4
star
23

docs.spmjs.org

documentation site for [email protected]
CSS
4
star
24

bin.spmjs.io

try spmjs@3x package in cloud
JavaScript
3
star
25

spm-doc

Documentation generator and publisher.
JavaScript
2
star
26

issue-cases

issue cases for spm
JavaScript
2
star
27

spmrc

spmrc manager.
JavaScript
2
star
28

template-spmplugin

Create a spm plugin.
JavaScript
2
star
29

spmjs.io-backup

Backup tool for spmjs.io .
JavaScript
2
star
30

spm-migrate

migrate 2.x to 3.x
JavaScript
2
star
31

grunt-check-online

check if the file is online
JavaScript
1
star
32

spm-doc-server

JavaScript
1
star
33

highcharts

Highcharts JS is a JavaScript charting library based on SVG and VML rendering.
JavaScript
1
star