diff --git a/lib/lan_connection.js b/lib/lan_connection.js index 4dd9600c..dac891cd 100644 --- a/lib/lan_connection.js +++ b/lib/lan_connection.js @@ -37,6 +37,11 @@ LAN.Connection.prototype.exec = function(command) { var self = this; return new Promise(function(resolve, reject) { + // Ensure this connection hasn't been closed + if (self.closed) { + return reject(new Error('Remote SSH connection has already been closed')); + } + // Execute the command if (!Array.isArray(command)) { return reject(new Error('Command to execute must be an array of args.')); @@ -87,6 +92,9 @@ LAN.Connection.prototype.open = function() { // Reject with error resolve(self); }).connect(self.auth); + self.ssh.once('close', function() { + self.closed = true; + }); }); };