使用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();
  });
相关推荐
wd5i8kA8i12 小时前
自研多线程 SSH 极速文件传输助手(附 GitHub 源码)
运维·ssh·github
DoUfp0bgq18 小时前
解决RDK X5(ARM64架构)板卡Remote-SSH运行Antigravity AI崩溃(SIGILL):Samba网络盘本地挂载方案
人工智能·架构·ssh
程序猿编码1 天前
eBPF代理:让SSH进程“溯源”,找到背后的客户端IP
linux·tcp/ip·ssh·ebpf
Learn-Share_HY1 天前
[IT Network]如何在cisco packet tracer建立ssh的連線?
ubuntu·ssh·switch·cisco·networking·packettracer·networktutorial
数据知道1 天前
claw-code 源码详细分析:Remote / SSH / Teleport / Deep Link——运行时分支爆炸怎样用「模拟模式」先收束状态机?
运维·ai·ssh·claude code
hello_fengfeng1 天前
VSCode Remote-SSH 连接失败修复(权限问题)
ide·vscode·ssh
m0_738120721 天前
网络安全编程——如何用Python实现SSH 服务端和SSH 反向 Shell(突破内网)
python·web安全·ssh
bIo7lyA8v2 天前
如何用SSH访问远程服务器上的内网服务(如:MySQL、Redis、Kafka)?
服务器·mysql·ssh
扑火的小飞蛾2 天前
Windows 10 与 Kali Linux SSH 免密互信配置指南
linux·windows·ssh
Geoking.3 天前
GitHub 多账号生存指南:从 SSH 连接到 GPG 签名全流程
运维·ssh·github