【问题标题】:node js set timeout in ssh in ssh2节点js在ssh2中的ssh中设置超时
【发布时间】:2016-11-21 18:40:19
【问题描述】:

如何设置超时以连接到 ssh 服务器?

目前,我正在使用以下代码连接到服务器:

var Client = require('ssh2');

var conn = new Client();
conn.on('ready', function(){
    console.log("connected");
});
conn.on('error', function(){
    console.log("fails");
});
conn.connect({
    host: ip,
    port: 22,
    username: user,
    password: password
});

【问题讨论】:

    标签: node.js ssh


    【解决方案1】:

    您可以像这样在connect 选项中设置readyTimeout

    conn.connect({
        host: ip,
        port: 22,
        username: user,
        password: password,
        readyTimeout: 5000
    });
    

    这将取消任何在 5 秒内未准备好的连接。

    您可以在documentation of ssh2 中了解更多信息。

    【讨论】:

    • 它确实有效(默认超时为 20 秒),但不清楚如何处理超时错误。它只会使 Node 应用程序崩溃。
    猜你喜欢
    • 1970-01-01
    • 2017-11-03
    • 2014-12-22
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2022-01-23
    • 2011-06-23
    • 2012-05-01
    相关资源
    最近更新 更多