Nodejs SSH2连接多次

时间:2014-04-17 03:07:17

标签: javascript node.js express ssh sftp

我希望连接到远程计算机并在该计算机上执行shell命令。我可以上传文件并成功执行命令。但我只能做一次。我希望能够多次这样做。

下面是我使用SSH2模块的JS文件。

                var Connection = require('ssh2');
            var fs = require('fs');

            c = new Connection();
            c.on('ready', function() {
              console.log('Connection :: ready');

              c.sftp(
                        function (err, sftp) {
                            if ( err ) {
                                console.log( "Error, problem starting SFTP: %s", err );
                                //process.exit( 2 );
                            }

                            console.log( "- SFTP started" );


                            sftp.unlink( "testfile.py", function(err){ 

                                if ( err ) {
                                    console.log( "Error, problem starting SFTP: %s", err );
                                    //process.exit( 2 );
                                }
                                else
                                {
                                    console.log( "file unlinked" );
                                }

                            });

                            // upload file
                            var readStream = fs.createReadStream( "testfile.py" );
                            var writeStream = sftp.createWriteStream( "testfile.py" );


                            writeStream.on('end', function () {

                                console.log( "sftp connection closed" );

                              });


                            // what to do when transfer finishes
                            writeStream.on(
                                'close',
                                function () {

                                    console.log( "- file transferred" );

                                    sftp.chmod( "testfile.py", 777, function(err){ 

                                        if ( err ) {
                                            console.log( "Error, problem starting SFTP: %s", err );
                                            //process.exit( 2 );
                                        }
                                        else
                                        {
                                            console.log( "Mode changed" );
                                        }

                                    });



                                    //execute the program
                                    c.exec('python testfile.py', function(err, stream) {

                                        if (err) throw err;
                                        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');

                                            // close the ftp connection
                                            sftp.end();
                                            // fs.end();
                                            //process.exit( 0 );

                                        });
                                        stream.on('exit', function(code, signal) {
                                          console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal);
                                          //c.end();
                                        });
                                      });





                                }
                            );

                            // initiate transfer of file
                            readStream.pipe( writeStream );
                        }
                    );





            });
            c.on('keyboard-interactive', function(name, instructions, instructionsLang, prompts, finish) {
              console.log('Connection :: keyboard-interactive');
              finish(['pwd']);
            });
            c.on('error', function(err) {
              console.log('Connection :: error :: ' + err);
            });
            c.on('end', function() {
              console.log('Connection :: end');
            });
            c.on('close', function(had_error) {
              console.log('Connection :: close');
            });
            c.connect({
              host: 'host',
              port: 22,
              username: 'uname',
              password: 'pwd',
              tryKeyboard: true
            });

1 个答案:

答案 0 :(得分:0)

ssh2 v0.2.22应该更好地支持连接实例的重用。如果您遇到任何问题,请随时发布issue