使用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();
  });
相关推荐
埃博拉酱10 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
zhangfeng113311 天前
趋动云 如何ssh登录 服务区 项目server
运维·人工智能·ssh
txzz888811 天前
CentOS-Stream-10 Secure Shell服务器
linux·centos·ssh·secure shell·ssh服务器
文静小土豆12 天前
CentOS 7 OpenSSH 10.2p1 升级全攻略(含离线安装与回退方案)
linux·运维·centos·ssh
最最菜的菜鸟12 天前
本地环境通过 SSH 协议访问 GitHub 上的仓库时,SSH 公钥认证失败,配置 Git SSH 密钥
git·ssh·github
ol木子李lo14 天前
Linux 命令备忘录
linux·运维·服务器·windows·编辑器·ssh·bash
m0_7381207214 天前
渗透测试——Momentum靶机渗透提取详细教程(XSS漏洞解密Cookie,SS获取信息,Redis服务利用)
前端·redis·安全·web安全·ssh·php·xss
小哈里16 天前
【工具】Linux远程开发核心工具,Git命令缩写与SSH常用命令
linux·git·ssh·工具·远程开发
推理幻觉16 天前
ssh远程连接错误
运维·ssh
能源革命16 天前
Ubuntu24.04修改ssh默认端口
linux·服务器·ssh