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)



  • 2019-01-29 14:33:07

    child_process中spawn和exec方法的使用

    child_process是nw.exe的一个内置模块,通过它可以实现创建多线程,并可实现主线程和子线程之间的通信。child_process模块中主要使用有两个方法spawn和exec,这两个方法都可以用来创建子线程。除了spawn和exec外,child_process模块还有execFile,fork,spawnSync,execFileSync,execSync,它们都是基于spawn的不同封装。 --------------------- 作者:黄泽平 来源:CSDN 原文:https://blog.csdn.net/zeping891103/article/details/52230175 版权声明:本文为博主原创文章,转载请附上博文链接!

  • 2019-01-29 14:50:51

    Node.js 编写跨平台 spawn 语句

    Node.js 是跨平台的,也就是说它能运行在 Windows、OSX 和 Linux 平台上。很多 Node.js 开发者都是在 OSX 上做开发的,然后再将代码部署到 Linux 服务器上。由于 OSX 和 Linux 都是基于 Unix 的,因此两者共性很多。Windows 也是 Node.js 官方支持的平台,只要你通过正确的方式写代码,就能在各个平台上毫无压力的跑起来。

  • 2019-01-30 17:53:21

    视图与临时表

    视图与表的不同之处:视图是一个虚表,即视图所对应的数据不进行实际存储,数据库只存储视图的定义,对视图的数据进行操作时,系统根据视图的定义去操作与视图相关联的基本表。

  • 2019-02-01 08:43:59

    JS 随机排序算法

    使用JS编写一个方法 让数组中的元素每次刷新随机排列