nodejs ssh2 基本功能的封装

2019-03-21 14:49:10

参考地址  nodejs ssh2 基本功能的封装


封装nodejs的基本功能,封装为一个类,实现的功能能够如下:


对于反复连接断开1000次,测试运行无错,也没有内存泄露

对于运行shell命令,测试在一次连接中运行8000+条命令,无错

对于上传下载文件夹,上传下载一个文件夹中包含800+的文件夹,共7000+文件无问题

在单连接测试下,运行状况良好,多连接同时运行测试时,当同时连接数量超过20台机器,会问题有的机器连接不上的问题,不过对于单连接和控制连接数来说封装的功能已经足够了

封装代码的下载链接(不要积分的) 

1.连接远程linux主机


/**

* 描述:连接远程机器

* 参数:server,远程机器凭证;

*       then,回调函数

*/

ssh2.connect(server, then) 

1

2

3

4

5

6

2.断开远程主机


/**

* 描述:断开远程连接

* 参数:then,回调函数

*/

ssh2.disconnect(then)

1

2

3

4

5

3.执行shell命令


/**

* 描述:执行shell命令

* 参数:cmd,要执行的命令;

*       then,回调函数

* 回调:then(err, data):data 运行命令之后的返回信息

*/

ssh2.exec(cmd, then)

1

2

3

4

5

6

7

4.上传文件


/**

* 描述:上传文件

* 参数:localPath,本地路径

*       remotePath,远程路径

*       then,回调函数

* 回调:then(err, result)

*/

ssh2.uploadFile(localPath, remotePath, then)

1

2

3

4

5

6

7

8

5.下载文件


/**

* 描述:下载文件

* 参数:localPath,本地路径

*       remotePath,远程路径

*       then,回调函数

* 回调:then(err, result)

*/

ssh2.downloadFile(remotePath, localPath, then)

1

2

3

4

5

6

7

8

6.创建目录


/**

 * 描述:创建目录

 * 参数: remoteDir 远程路径;

 *      then 回调函数

 * 回调:then(err, date) : data创建目录之后返回的信息

 */

ssh2.mkdir(remoteDir, then)

1

2

3

4

5

6

7

7.删除目录


/**

 *  描述:删除目录

 *  参数:remoteDir 远程路径

 *       then 回调函数

 *  回调:then(err, date) : data 留下的接口,无任何返回数据

 */

ssh2.rmdir(remoteDir, then)

1

2

3

4

5

6

7

9.上传文件夹


/**

* 描述:上传文件到远程linux机器

* 参数: remotePath 远程路径;

*       localDir 本地路径,

*       then 回调函数

* 回调:then(err)

*/

ssh2.uploadDir(localDir, remoteDir, then)


10.下载文件夹


/**

* 描述:下载目录到本地

* 参数: remotePath 远程路径;

*       localDir 本地路径,

*       then 回调函数

* 回调:then(err)

*/

ssh2.downloadDir(remoteDir, localDir, then)



  • 2020-11-05 23:20:29

    mac更新node版本

    initializer function 0x0 not in mapped image for /usr/local/bin/node,除了上面的问题 你或许还出现过 no such file or directory 这样的提示,总之更新完以后node直接不能用了。

  • 2020-11-07 16:31:02

    nginx配置X-Frame-Options允许多个域名iframe嵌套

    有时候我们需要允许多个url的来源,但是又不能全部开放,所以应该匹配第三种方法ALLOW-FROM url,那么多个url该如何配置呢,百度了所有网站都没有找到,那么这里写给大家,其实很简单: add_header X-Frame-Options 'ALLOW-FROM https://xxx.xxxxxx.com https://xxx2.xxxxxxx.com'; 就是使用空格隔开就好了!

  • 2020-11-08 08:31:51

    meteor在不同端口启动服务

    当没有任何参数时,run是默认行为,在幕后,它3000端口开启node.js服务器实例,同时开启监听3001端口的MongoDB服务

  • 2020-11-11 15:05:39

    nuxt如何在其它js文件中使用store

    在新建的js文件中想用store里面的数据,比如token想在封装的axios里面,请求头里面去使用,亦或者通过app的JS接口获取token并存储在store里面。我们都知道如何在vue中如何使用。