Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/coverage_report.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: report coverage data to code climate
on:
push:
tags:
- 'v*'
branches:
- main
jobs:
test:
uses: ./.github/workflows/run_test.yml
Expand All @@ -12,7 +12,7 @@ jobs:
name: coverage report
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: code-coverage-report
Expand Down
22 changes: 0 additions & 22 deletions .github/workflows/npm_publish.yml

This file was deleted.

29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release and Publish

on:
push:
branches:
- main

jobs:
test:
uses: ./.github/workflows/run_test.yml

release:
needs: test
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- name: Semantic Release
uses: cycjimmy/semantic-release-action@v4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
17 changes: 7 additions & 10 deletions .github/workflows/run_test.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
name: run UT
on:
push:
tags-ignore:
- 'v*'
pull_request:
branches:
- '*'
- '!main'
- main
workflow_call:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand All @@ -24,19 +21,19 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x, 16.x]
node-version: [16.x, 18.x, 20.x, 22.x]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run test
- run: npm run coverage
if: matrix.node-version == '20.x'
if: matrix.node-version == '22.x'
- uses: actions/upload-artifact@v4
if: matrix.node-version == '20.x'
if: matrix.node-version == '22.x'
with:
name: code-coverage-report
path: coverage-report.lcov
Expand Down
16 changes: 16 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"branches": ["main"],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
["@semantic-release/npm", {
"npmPublish": true
}],
["@semantic-release/git", {
"assets": ["package.json", "package-lock.json", "CHANGELOG.md"],
"message": "chore(release): ${nextRelease.version} [skip ci]\\n\\n${nextRelease.notes}"
}],
"@semantic-release/github"
]
}
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,102 @@
[![Maintainability](https://api.codeclimate.com/v1/badges/8f3c0ea00e755ae31081/maintainability)](https://codeclimate.com/github/so5/ssh-client-wrapper/maintainability)
# ssh-client-wrapper
open ssh client wrapper library for nodejs

## Installation
```bash
npm install ssh-client-wrapper
```

## Usage
Here is a simple example of how to use `ssh-client-wrapper` to execute a command on a remote server.

```javascript
import SshClientWrapper from 'ssh-client-wrapper';

const hostInfo = {
host: 'remote.server.com',
port: 22,
user: 'username',
password: 'password'
};

const ssh = new SshClientWrapper(hostInfo);

async function run() {
try {
await ssh.canConnect();
console.log('Connection successful!');

const { output, rt } = await ssh.execAndGetOutput('ls -l /home');
if (rt === 0) {
console.log('Directory listing:');
output.forEach(line => console.log(line));
} else {
console.error('Error executing command');
}
} catch (err) {
console.error('Connection or command failed:', err);
} finally {
ssh.disconnect();
}
}

run();
```

## API Guide

### `new SshClientWrapper(hostInfo)`
Creates a new SSH client instance.

* `hostInfo` (Object): Connection details for the remote host.
* `host` (string): The hostname or IP address of the server.
* `user` (string): The username for authentication.
* `port` (number, optional): The port number. Defaults to 22.
* `password` (string | Function, optional): The password for authentication, or a function that returns the password.
* `keyFile` (string, optional): The path to the private key file for key-based authentication.
* `passphrase` (string | Function, optional): The passphrase for the private key, or a function that returns it.
* `noStrictHostKeyChecking` (boolean, optional): If `true`, bypasses strict host key checking.
* And more... see `lib/index.js` for all available options.

### `.exec(cmd, [timeout], [outputCallback], [rcfile], [prependCmd])`
Executes a command on the remote host.

* Returns: `Promise<number>` - The return code of the command.

### `.execAndGetOutput(cmd, [timeout], [rcfile], [prependCmd])`
Executes a command and returns its standard output.

* Returns: `Promise<{output: string[], rt: number}>` - An object containing the output as an array of strings and the return code.

### `.ls(target, [lsOpt], [timeout])`
Executes the `ls` command on the remote host.

* Returns: `Promise<string[]>` - The output of the `ls` command as an array of strings.

### `.expect(cmd, expects, [timeout])`
Executes a command and interacts with it, similar to the `expect` tool.

* Returns: `Promise<number>` - The return code of the command.

### `.send(src, dst, [opt], [timeout])`
Uploads files or directories to the remote host using `rsync`.

* `src` (string[]): An array of local paths to send.
* `dst` (string): The remote destination path.
* Returns: `Promise<void>`

### `.recv(src, dst, [opt], [timeout])`
Downloads files or directories from the remote host using `rsync`.

* `src` (string[]): An array of remote paths to receive.
* `dst` (string): The local destination path.
* Returns: `Promise<void>`

### `.canConnect([timeout])`
Checks if a connection to the remote host can be established.

* Returns: `Promise<boolean>` - Resolves with `true` on success.

### `.disconnect()`
Closes the master SSH connection to the remote host.
Loading