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-03-30 15:37:12

    PM2下使用 npm run 命令

    npm run xxxx 是 node常用的启动方式之一,本文介绍下如何用PM2来实现该方式的启动。 下面是项目的package.json文件部分代码:

  • 2020-04-01 10:15:11

    vue-cli3.0中vue.config.js的基本配置(去除esLint提示和解决build后静态文件路径报错)

    vue-cli是开发vue项目必不可少的脚手架工具,3.0版本之前的目录结构是由config目录的,关于webpack配置的文件都放在该目录里面,而3.0版本之后做了一个大更新,webpack相关的默认配置都帮你集成到webpack的npm 包里面去了,官方提供的一种修改配置的方式就是开发者自己在项目的根目录(与src同层级的目录)下建立一个vue.config.js的文件,然后去覆盖自定义的配置,达到你想要的效果,下面写一个基本的默认配置

  • 2020-04-01 10:21:20

    Vue extend $mount 构造器详解

    本节介绍两个 Vue.js 内置但却不常用的 API——extend 和 $mount,它们经常一起使用。不常用,是因为在业务开发中,基本没有它们的用武之地,但在独立组件开发时,在一些特定的场景它们是至关重要的。

  • 2020-04-01 15:36:52

    CSS3中的transition属性详解

    transition: property duration timing-function delay transition属性是个复合属性,她包括以下几个子属性: transition-property :规定设置过渡效果的css属性名称 transition-duration :规定完成过渡效果需要多少秒或毫秒 transition-timing-function :指定过渡函数,规定速度效果的速度曲线 transition-delay :指定开始出现的延迟时间