javascript - Transfer entire directory using ssh2 in Nodejs - Stack Overflow

I'm just wondering whether it is at all possible to transfer a directory from a unix server to my

I'm just wondering whether it is at all possible to transfer a directory from a unix server to my local machine using the ssh2 module in node.js. I have connected to the remote host and can read the directory as well as transfer single files, but there are 28 folders in the directory which each contain files and sub directories. What I'd like to do is take an exact copy of the main directory from the server to my local machine.

I was using fastGet with single files, but transferring a directory gives: Error: EISDIR, open __dirname/../localdirectory/ which I think implies I can't use fastGet to get an entire directory. I also tried using the exec mand to try and scp it over, but I couldn't work out the syntax for the local directory:

// c is an active connection
c.exec('scp filethatexists.extension /../filepath/newname.extension', function(err, stream) {
    if (err) {
        console.log("error: " + err);
        stream.end;
    };
    stream.on('data', function(data, extended) {
        console.log((extended === 'stderr' ? 'STDERR: ' : 'STDOUT: ') + data);
    });
    stream.on('end', function() {
        console.log('Stream :: EOF');
    });
    stream.on('close', function() {
        console.log('Stream :: close');
    });
    stream.on('exit', function(code, signal) {
        console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal);
        c.end();
    });
});

This just results in the EOF calling. This code was just me testing If I could get a single file transferring.

Can anyone provide me with any assistance? Thank you in advance.

I'm just wondering whether it is at all possible to transfer a directory from a unix server to my local machine using the ssh2 module in node.js. I have connected to the remote host and can read the directory as well as transfer single files, but there are 28 folders in the directory which each contain files and sub directories. What I'd like to do is take an exact copy of the main directory from the server to my local machine.

I was using fastGet with single files, but transferring a directory gives: Error: EISDIR, open __dirname/../localdirectory/ which I think implies I can't use fastGet to get an entire directory. I also tried using the exec mand to try and scp it over, but I couldn't work out the syntax for the local directory:

// c is an active connection
c.exec('scp filethatexists.extension /../filepath/newname.extension', function(err, stream) {
    if (err) {
        console.log("error: " + err);
        stream.end;
    };
    stream.on('data', function(data, extended) {
        console.log((extended === 'stderr' ? 'STDERR: ' : 'STDOUT: ') + data);
    });
    stream.on('end', function() {
        console.log('Stream :: EOF');
    });
    stream.on('close', function() {
        console.log('Stream :: close');
    });
    stream.on('exit', function(code, signal) {
        console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal);
        c.end();
    });
});

This just results in the EOF calling. This code was just me testing If I could get a single file transferring.

Can anyone provide me with any assistance? Thank you in advance.

Share Improve this question asked May 29, 2014 at 14:03 Andrew WhalleyAndrew Whalley 1331 gold badge2 silver badges6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

A couple of solutions:

  1. You could recursively traverse the directory (making directories and transferring files as needed) using the sftp methods

  2. Tar the directory (press it too if you want) to stdout (e.g. tar cf - mydir) and then process that ining stdout data with the tar module (and the built-in zlib module first if you end up pressing the directory).

    // Requires:
    //   * `npm install tar-fs`
    //   * `ssh2` v0.5.x or newer
    var tar = require('tar-fs');
    var zlib = require('zlib');
    
    function transferDir(conn, remotePath, localPath, pression, cb) {
      var cmd = 'tar cf - "' + remotePath + '" 2>/dev/null';
    
      if (typeof pression === 'function')
        cb = pression;
      else if (pression === true)
        pression = 6;
    
      if (typeof pression === 'number'
          && pression >= 1
          && pression <= 9)
        cmd += ' | gzip -' + pression + 'c 2>/dev/null';
      else
        pression = undefined;
    
      conn.exec(cmd, function(err, stream) {
        if (err)
          return cb(err);
    
        var exitErr;
    
        var tarStream = tar.extract(localPath);
        tarStream.on('finish', function() {
          cb(exitErr);
        });
    
        stream.on('exit', function(code, signal) {
          if (typeof code === 'number' && code !== 0) {
            exitErr = new Error('Remote process exited with code '
                                + code);
          } else if (signal) {
            exitErr = new Error('Remote process killed with signal '
                                + signal);
          }
        }).stderr.resume();
    
        if (pression)
          stream = stream.pipe(zlib.createGunzip());
    
        stream.pipe(tarStream);
      });
    }
    
    // USAGE ===============================================================
    var ssh = require('ssh2');
    
    var conn = new ssh();
    conn.on('ready', function() {
      transferDir(conn,
                  '/home/foo',
                  __dirname + '/download',
                  true, // uses pression with default level of 6
                  function(err) {
        if (err) throw err;
        console.log('Done transferring');
        conn.end();
      });
    }).connect({
      host: '192.168.100.10',
      port: 22,
      username: 'foo',
      password: 'bar'
    });
    

i m also trying to downlaod folders using ssh. It took me more than 10 days and i'm still trying to do that. But in the mean time i found some other code which will do the same thing for me.This code below will download every folder and file inside a directory

enter image description here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745281136a4620285.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信