【笔记】Node-SSH学习笔记

前言

Node-SSH is an extremely lightweight Promise wrapper for ssh2.(Github

下载依赖

1
npm install -D node-ssh

远程执行Shell命令

src/main.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
import { NodeSSH } from "node-ssh";

const sshClient = await new NodeSSH().connect({
host: "",
port: 22,
username: "",
password: ""
});

const result = await sshClient.execCommand("<shell>", { cwd: "/" });
console.log(result.stdout);

sshClient.dispose();

文件上传

../package.json:本地路径
/root/package.json:远程路径

recursive:是否递归上传
concurrency:并发上传文件数,缺省值为10

src/main.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { NodeSSH } from "node-ssh";

const sshClient = await new NodeSSH().connect({
host: "",
port: 22,
username: "",
password: ""
});

await sshClient.putFile("../package.json", "/root/package.json", {
recursive: true,
concurrency: 10
});

sshClient.dispose();

文件下载

../package.json:本地路径
/root/package.json:远程路径

src/main.mjs
1
2
3
4
5
6
7
8
9
10
11
12
import { NodeSSH } from "node-ssh";

const sshClient = await new NodeSSH().connect({
host: "",
port: 22,
username: "",
password: ""
});

await sshClient.getFile("../package.json", "/root/package.json");

sshClient.dispose();

完成