nodejs之SSH

SSH(Secure Shell)是一个加密的网络协议,用于在不安全的网络上安全地执行远程命令和管理系统。SSH 最常见的用途是通过加密的连接远程登录计算机,但它也可以用于传输文件、隧道其他协议、以及执行其他网络服务。

而 node-ssh 是一个用于 Node.js 的库,封装了 SSH 协议的功能,使得开发者可以通过 JavaScript 或 TypeScript 代码在 Node.js 应用程序中实现 SSH 功能。

使用例子:

const { NodeSSH } = require("node-ssh");
const ssh = new NodeSSH();

ssh
  .connect({
    host: "127.0.0.1",
    username: "moment",
    password: "moment",
  })
  .then(() => {
    // 执行远程命令
    ssh.execCommand("uptime").then((result) => {
      console.log("STDOUT: " + result.stdout);
      console.log("STDERR: " + result.stderr);
    });

    // 上传文件
    ssh
      .putFile("local-path.txt", "/remote-path.txt")
      .then(() => {
        console.log("文件上传成功");
      })
      .catch((err) => {
        console.error("文件上传失败:", err);
      });

    // 下载文件
    ssh
      .getFile("local-path.txt", "/remote-path.txt")
      .then(() => {
        console.log("文件下载成功");
      })
      .catch((err) => {
        console.error("文件下载失败:", err);
      });
  })
  .catch((err) => {
    console.error("SSH连接失败:", err);
  });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值