• Stars
    star
    183
  • Rank 210,154 (Top 5 %)
  • Language
    JavaScript
  • Created almost 11 years ago
  • Updated 9 months ago

Reviews

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

Repository Details

SSH and SFTP tasks for gulp

gulp-ssh

SSH and SFTP tasks for gulp

NPM version Downloads CI Status

Install

Install with npm

npm install --save-dev gulp-ssh

Example

'use strict'

var fs = require('fs');
var gulp = require('gulp')
var GulpSSH = require('gulp-ssh')

var config = {
  host: '192.168.0.21',
  port: 22,
  username: 'node',
  privateKey: fs.readFileSync('/Users/zensh/.ssh/id_rsa')
}

var gulpSSH = new GulpSSH({
  ignoreErrors: false,
  sshConfig: config
})

gulp.task('exec', function () {
  return gulpSSH
    .exec(['uptime', 'ls -a', 'pwd'], {filePath: 'commands.log'})
    .pipe(gulp.dest('logs'))
})

gulp.task('dest', function () {
  return gulp
    .src(['./**/*.js', '!**/node_modules/**'])
    .pipe(gulpSSH.dest('/home/iojs/test/gulp-ssh/'))
})

gulp.task('sftp-read', function () {
  return gulpSSH.sftp('read', '/home/iojs/test/gulp-ssh/index.js', {filePath: 'index.js'})
    .pipe(gulp.dest('logs'))
})

gulp.task('sftp-write', function () {
  return gulp.src('index.js')
    .pipe(gulpSSH.sftp('write', '/home/iojs/test/gulp-ssh/test.js'))
})

gulp.task('shell', function () {
  return gulpSSH
    .shell(['cd /home/iojs/test/thunks', 'git pull', 'npm install', 'npm update', 'npm test'], {filePath: 'shell.log'})
    .pipe(gulp.dest('logs'))
})

API

var GulpSSH = require('gulp-ssh')

GulpSSH(options)

var gulpSSH = new GulpSSH(options)

options.sshConfig

Required Type: Object

  • host - String - Hostname or IP address of the server. Default: 'localhost'

  • port - Number - Port number of the server. Default: 22

  • username - String - Username for authentication. Default: (none)

  • password - String - Password for password-based user authentication. Default: (none)

  • privateKey - String or Buffer - Buffer or string that contains a private key for key-based user authentication (OpenSSH format). Default: (none)

  • privateKeyFile - String - A path to a file that contains a private key for key-based user authentication (OpenSSH format). gulp-ssh extension. Default: (none)

  • useAgent - Boolean - Auto-detect the running SSH agent (via SSH_AUTH_SOCK environment variable) and use it to perform authentication. gulp-ssh extension. Default: (false)

  • ...and so forth.

For a full list of connection options, see the reference for the connect() method from the SSH2 module.

options.ignoreErrors

Type: Boolean

Ignore errors when executing commands. Default: (false)


gulpSSH.shell(commands, options)

return stream, there is a event "ssh2Data" on stream that emit ssh2 stream's chunk.

IMPORTANT: If one of the commands requires user interaction, this function will hang. Observe the ssh2Data event to debug the interaction with the server.

commands

Required Type: String or Array

options.filePath

Option Type: String

file path to write on local. Default: ('gulp-ssh.shell.log')

options.autoExit

Option Type: Boolean

auto exit shell. Default: (true)

gulpSSH.exec(commands, options)

return stream, there is a event "ssh2Data" on stream that emit ssh2 stream's chunk.

IMPORTANT: If one of the commands requires user interaction, this function will hang. Observe the ssh2Data event to debug the interaction with the server.

commands

Required Type: String or Array

options.filePath

Option Type: String

file path to write on local. Default: ('gulp-ssh.exec.log')

gulpSSH.sftp(command, filePath, options)

return stream

command

Required Type: String Value: 'read' or 'write'

filePath

Required Type: String

file path to read or write on server. Default: (none)

options

Option Type: Object

gulpSSH.dest(destDir, options)

return stream, copy the files to remote through sftp, acts similarly to Gulp dest, will make dirs if not exist.

Tests

This library is an SSH/SFTP transfer client. Therefore, we need to connect to an SSH server to test it.

The strategy used to test this library is to connect to the current machine as the current user over SSH. This allows the tests to verify both ends of the SSH transfer.

To run the tests, you need a local SSH server running and an SSH key the tests can use to authenticate against it. (The instructions in this section are specific to Linux).

First, let's generate an SSH key without a passphrase for the tests to use.

mkdir -p test/etc/ssh
ssh-keygen -t rsa -b 4096 -N "" -f test/etc/ssh/id_rsa -q
chmod 600 test/etc/ssh/id_rsa*

Next, add this key to the authorized SSH keys for the current user:

mkdir -p ~/.ssh
chmod 700 ~/.ssh
cat test/etc/ssh/id_rsa.pub > ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

If you already have an SSH server running on your machine, you can use it to run the tests. Let's test to make sure we can connect to it.

ssh -i etc/test/ssh $USER@localhost uptime

You should see the uptime of the current machine printed to the console. If that works, you're ready to run the tests!

yarn test

If you don't have an SSH server running, you can run one on a user-space port (2222) for the tests. Start by creating a server configuration and host key:

mkdir -p test/etc/sshd
cat << EOF > test/etc/sshd/sshd_config
Port 2222
ListenAddress 127.0.0.1
HostKey $(pwd)/test/etc/sshd/host_rsa
PidFile $(pwd)/test/etc/sshd/pid
PasswordAuthentication no
PubkeyAuthentication yes
ChallengeResponseAuthentication no
Subsystem sftp /usr/lib/openssh/sftp-server
UsePAM no
EOF
ssh-keygen -t rsa -b 4096 -N "" -f test/etc/sshd/host_rsa -q

Next, start the server:

/usr/sbin/sshd -f test/etc/sshd/sshd_config

If the sshd command is missing, you'll need to install the openssh-server package for your distribution.

Let's try to connect to it make sure it's running properly:

ssh -i etc/test/ssh -p 2222 $USER@localhost uptime

You should see the uptime of the current machine printed to the console. If that works, you're ready to run the tests!

CI=true yarn test

We pass CI=true so that the tests use port 2222 to connect instead of the default port.

When you're done running the tests, you can use this command to stop the SSH server:

kill $(cat test/etc/sshd/pid)

In the test/scripts directory you can find the setup and teardown process that's used in CI to run these tests, which is similar to the one described in this section.

License

MIT © Teambition

More Repositories

1

then.js

[快照]史上最快,与 node callback 完美结合的异步流程控制库!
JavaScript
573
star
2

gear

A lightweight, composable and high performance web service framework for Go.
Go
551
star
3

teambition-sdk

Isomorphic JavaScript SDK for Teambition APIs
TypeScript
433
star
4

rrule-go

Go library for working with recurrence rules for calendar dates.
Go
313
star
5

TBEmptyDataSet

An extension of UITableView/UICollectionView's super class, it will display a placeholder emptyDataSet when the data of tableView/collectionView is empty.
Swift
206
star
6

Hire

Working With Great People
200
star
7

gulp-sequence

Run a series of gulp tasks in order
JavaScript
195
star
8

merge2

Merge multiple streams into one stream in sequence or parallel (~119M/month downloads).
JavaScript
171
star
9

DropdownMenu

Dropdown menu for NavigationController in Swift, also support Dropup Menu
Swift
133
star
10

snapper-core

Teambition push messaging service, backed by redis.
JavaScript
115
star
11

webapp-solutions

Try to collect anwsers on problems we encounter in Web Apps
113
star
12

ReactiveDB

Reactive ORM for Lovefield
TypeScript
105
star
13

jsonrpc-lite

Parse and Serialize JSON-RPC2 messages in node.js, or browser.
JavaScript
95
star
14

ratelimiter-go

The fastest abstract rate limiter, base on go-redis/redis.
Go
93
star
15

RRuleSwift

Swift rrule library for working with recurrence rules of calendar dates.
JavaScript
90
star
16

swaggo

Convert Go annotations to Swagger Documentation (version 2.0)
Go
68
star
17

SegmentedControl

A highly customizable segmented control for iOS applications.
Swift
64
star
18

HanziPinyin

A lightweight Swift library supporting convertion between Chinese(both Simplified and Tranditional) characters and Pinyin.
Swift
57
star
19

WebBrowser

A web browser using WebKit and written in Swift for iOS apps.
Swift
55
star
20

timed-queue

Distributed timed job queue, backed by Redis.
JavaScript
50
star
21

pdfviewer

PDF Viewer using Mozilla PDF JS
JavaScript
49
star
22

AMRAudioSwift

A useful tool to encode or decode audio between AMR and WAVE.
Swift
38
star
23

CardStyleTableView

An extension of UITableView and UITableViewCell which displays a card style view in grouped tableView, similar to the system's tableView before iOS 7.
Swift
32
star
24

trie-mux

A minimal and powerful trie based url path router (or mux) for Go.
Go
27
star
25

Pocket-Node-SDK

Headless Node.js SDK for integrating with [Pocket](http://getpocket.com/)
CoffeeScript
23
star
26

TB-Icons

A classified icons set that consists of a part of Material Design icons and some original icons by TB-UI team. Available in Icon Fonts and SVG Symbols.
Stylus
23
star
27

limbo

Factory/Loader of mongoose
CoffeeScript
17
star
28

json-mask-go

👺 JSON mask for Go
Go
10
star
29

tb-i18n-loader

i18n loader of teambition web for webpack
JavaScript
9
star
30

tws-auth

Node.js SDK of TWS (Teambition Web Service) client.
TypeScript
8
star
31

snapper-swift

Snapper-core client by Swift 2.0
Swift
8
star
32

node-teambition

JavaScript
7
star
33

redlimit

RedLimit is a redis-based distributed rate limit HTTP service, implemented with Rust.
Rust
7
star
34

tb-apps-sdk

Teambition API Bridge
TypeScript
7
star
35

teambition-node-sdk

teambition node sdk for server applications
TypeScript
6
star
36

WeChatSDK

Swift version of WeChat SDK.
Objective-C
5
star
37

node-chinese-finance-number

JavaScript
4
star
38

smart-limiter

Smart rate limiter middleware for express.
JavaScript
4
star
39

urbs-setting

Urbs 灰度平台灰度策略服务
Go
4
star
40

ilog

light-weight, smart and pure log module
JavaScript
3
star
41

teambition-java-sdk

teambition java sdk for server applications
Java
3
star
42

pipe-errors

Handle errors on piping streams and pipe error to the end.
JavaScript
3
star
43

i18n-middleware

An i18n middleware built for web apps.
CoffeeScript
2
star
44

grpclb

grpclb policy that consistent hash implemention, base on discovery systems like etcd, consul etc.
Go
2
star
45

teambition-server-sdk

TypeScript
2
star
46

gem-next

general entity manager next generation
TypeScript
2
star
47

tws-tcm

Node.js SDK of TWS (Teambition Web Service) cloud messaging service
TypeScript
1
star