teambition / gulp-ssh

SSH and SFTP tasks for gulp
184 stars 38 forks source link

Sudo for Exec #31

Closed Rommero closed 8 years ago

Rommero commented 9 years ago

Orignal ssh2 supports sudo comands by allocating pseudo-tty session via passing {pty : true}. The required password is then written to the stream returned via callback of exec function. e.g:

...exec('<command>', function(err, stream) {
      stream.write(password + "\n")
}). 

To be able to do so I offer to pipe outstream in gulp-ssh exec function (outStream.pipe(stream)).

function execCommand() {
...gutil.log(packageName + ' :: Executing :: ' + command)
        ssh.exec(command, options, function (err, stream) {
            if (err) return outStream.emit('error', new gutil.PluginError(packageName, err))           
            outStream.pipe(stream)  //possible fix
            stream
                   .on('data',....
}

The described pipe will allow to simply write smth like this

gulp.task("restart-pm2-task", function(callback) {
    ssh.exec(['sudo <your command>'],{pty : true}).write(password + "\n")

I suppose it's a bit of a crutch, so I am just reporting of a problem and giving you a simple solution. If ok, i can make a pull request with the described changes

zensh commented 9 years ago

I will look forward to your PR :+1: