• Nodejs中远程服务器下载文件时不支持SFTP协议下载时的操作


    最近遇到一个问题,将项目部署到服务器A,通过A服务器向服务器B上下载文件,但服务器不支持FTP、SFTPF协议,仅可使用SCP协议进行下载。

    在下载文件的过程中,曾使用过SCP2、SSH 等库下载文件,但一直提示"Received exit code 1 while establishing SFTP session",通过查看scp2、ssh源码发现,它们还是通过SFTP协议进行使用的。

    解决办法:

    1、在服务器A安装sshpass,因服务器B未使用免密登录,因此需要安装该工具

    yum install sshpass
    

     2、在服务器A中一个存在密码的文件,存放服务器的登录密码

    vi /root/xx.txt
    //在此中输入服务器B的密码,按wq保存并退出

    3、在node项目中安装child_process组件

    npm install child_process

    4、调用该方法即可实现文件下载

    var child_process = require('child_process');
    var command = "sshpass -f " + PSWPath + " scp " + ServerUserName + '@' + ServerHost + ":" + remotePath + " " + localPath;
    //格式为: sshpass -f /root/xx.txt scp root@xxx:/xxx/xxx/xx.log ./
    // sshpass -f 指读取指定文件,也可使用 sshpass -p
    //例: sshpass -p "123456" scp root@xxx:/xxx/xxx/xx.log ./
    // config.log(command);
    child_process.exec(command, function (error, stdout, stderr) {
        if (error) {
            console.log(error);
        } else {
            console.log(stdout);
        }
    });
  • 相关阅读:
    浏览器原理
    jQ插件编写
    [转]清理浮动的全家
    百度面试~~
    LeetCode-222. Count Complete Tree Nodes
    LeetCode-236. Lowest Common Ancestor of a Binary Tree
    LeetCode-235. Lowest Common Ancestor of a Binary Search Tree
    LeetCode-102. Binary Tree Level Order Traversal
    LeetCode-404. Sum of Left Leaves
    LeetCode-257. Binary Tree Paths
  • 原文地址:https://www.cnblogs.com/sanshengyanhuo/p/13724703.html
Copyright © 2020-2023  润新知