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-05-25 14:54:50

    commit your changes or stash them before you can merge

    Your local changes to the following files would be overwritten by merge:         protected/config/main.php Please, commit your changes or stash them before you can merge. --------------------- 作者:陈小峰_iefreer 来源:CSDN 原文:https://blog.csdn.net/iefreer/article/details/7679631 版权声明:本文为博主原创文章,转载请附上博文链接!

  • 2019-05-27 10:43:34

    IntelliJ IDEA中C盘文件过大怎么办

    当我在D:\ 安装完IDEA9.0之后,建立了一个工程,发现C:\Users\Administrator\.IntelliJIdea90 竟然增大到了500+M,并且随着使用在逐渐增大,这样占用系统盘资源是非常让人不爽的,那么如何将其修改到其他路径呢?

  • 2019-05-28 13:33:20

    BRVAH+MTRVA,复杂?不存在的

    言归正传,这样的一个首页,我们需要做怎么样的基础工作呢?或者说,碰到以后更复杂的页面我们应该怎么做?这里小提示下,不要再用什么类似ScrollView的这种东西了,诶,好像说的有点绝对,尽量不要用,这不是谷歌想要看到的,5.0谷歌推出了RecyclerView,从它的整个设计架构来看,简直就是为这而生的。而RecyclerView的视图是通过Adapter来渲染的。原始的Adapter,让人很蛋疼,重复工作太多,我们应该要有封装的思想,把最需要的部分提供出来,其它不用管。

  • 2019-05-29 14:19:19

    解决Git中fatal: refusing to merge unrelated histories

    Git的报错 在使用Git的过程中有时会出现一些问题,那么在解决了每个问题的时候,都需要去总结记录下来,下次不再犯。 一、fatal: refusing to merge unrelated histories 今天在使用Git创建项目的时候,在两个分支合并的时候,出现了下面的这个错误。

  • 2019-05-29 15:47:51

    撤销commit

    在git push的时候,有时候我们会想办法撤销git commit的内容