使用ssh上传数据到阿里云ESC云服务上

在这之前需要安装 ssh2-sftp-client 直接在终端输入:npm i ssh2-sftp-client

直接上代码:

javascript 复制代码
const path = require('path');
const Client = require('ssh2-sftp-client');

// 配置连接参数
const config = {
  host: 'your-server-ip', // 云服务器的IP地址
  port: 22, // SSH端口号,默认为22
  username: 'your-username', // SSH用户名
  password: 'your-password' // SSH密码
};

// 创建 SFTP 客户端
const sftp = new Client();

// 上传的本地文件路径
const localFilePath = path.resolve(__dirname, 'userInfo.json'); // 我这里数据userInfo.json

// 远程服务器上的目标路径
const remoteFilePath = '/root/userInfo.json';    // 我这里的目录是/root
// 后面只是上传数据的名字,如果没有弄后面的userInfo.json,上传默认数据名字就是root

sftp.connect(config)
  .then(() => {
    return sftp.put(localFilePath, remoteFilePath);
  })
  .then(() => {
    console.log(`JSON 文件上传成功`);
    sftp.end();
  })
  .catch((err) => {
    console.error(`上传文件时出错: ${err.message}`);
    sftp.end();
  });
相关推荐
鼠鼠龙年发大财7 小时前
【x**3专享】安装SSH、XFTP、XShell、ARM Linux
linux·arm开发·ssh
学习3人组1 天前
CentOS 上配置多服务器 SSH 免密登录
服务器·centos·ssh
FreeBuf_2 天前
SSHamble:一款针对SSH技术安全的研究与分析工具
运维·安全·ssh
Flying_Fish_roe2 天前
linux-安全管理-SSH 安全管理
linux·安全·ssh
_Johnny_2 天前
linux 使用SSH密钥配置免密登录
linux·服务器·ssh
anddddoooo3 天前
pWnOS的第二种全新解法(ssh私钥破解、webmin漏洞提权)
linux·运维·安全·web安全·网络安全·ssh
Grassto4 天前
Gitlab 中几种不同的认证机制(Access Tokens,SSH Keys,Deploy Tokens,Deploy Keys)
go·ssh·gitlab·ci
aidroid5 天前
linux配置远程ssh服务
linux·运维·ssh
学习3人组5 天前
第4步CentOS配置SSH服务用SSH终端XShell等连接方便文件上传或其它操作
linux·centos·ssh
Amagi.6 天前
http连接与ssh连接的区别
网络协议·http·ssh