node-ssh icon indicating copy to clipboard operation
node-ssh copied to clipboard

Run a git clone that requires a username and password?

Open dtboy1995 opened this issue 6 years ago • 6 comments

Code

const SSH = require('node-ssh')
const ssh = new SSH()

async function test () {
    await ssh.connect({
        host: 'myIP',
        username: 'root',
        port: 22,
        password: 'MyPassword',
        tryKeyboard: true,
        onKeyboardInteractive: (name, instructions, instructionsLang, prompts, finish) => {
            console.log(name)
            console.log(instructions)
            console.log(instructionsLang)
            console.log(prompts)
            console.log(finish)
        }
    })
    let res = await ssh.execCommand('git clone https://gitee.com/dtboy/platform.git')
    console.log(res);

    // ssh.requestShell
    console.log('#')
    // ssh.dispose()
}

test()
  • This function, onKeyboardInteractive, is never called
  • res
{ code: 128,
  signal: undefined,
  stdout: 'Cloning into \'platform\'...',
  stderr:
   'fatal: could not read Username for \'https://gitee.com\': No such device or address' }

What should I do, any help would be appreciated!

dtboy1995 avatar Jun 24 '19 09:06 dtboy1995

Many commands are required to interact with the keyboard, how to achieve this operation, for some examples, would be grateful

like the lib node-suppose

dtboy1995 avatar Jun 24 '19 11:06 dtboy1995

While most of your setup is correct, the error from what you've posted seems to be a domain resolution one. Try running a ping for gitee.com from ssh instead.

steelbrain avatar Jun 24 '19 20:06 steelbrain

@steelbrain, Thank you for your reply But you don't seem to understand my question And I ended up doing it this way(Not very pretty) use ssh2 package

conn.shell(function (err, stream) {
        if (err) throw err;
        stream.on('close', function () {
            console.log('Stream::close');
            conn.end();
        }).on('data', function (data) {
            console.log('[OUTPUT]: ' + data);
            if (data.indexOf('Username') != -1) {
                console.log('The user name is matched');
                stream.write('myusername\n')
            }
            if (data.indexOf('The password was matched') != -1) {
                console.log('');
                stream.write('mypass\n')
            }
        });
        stream.write('git clone https://gitee.com/dtboy/platform.git\n');
        setTimeout(() => {
            stream.close() 
        }, 3000);
    });

Can this library do something similar

dtboy1995 avatar Jun 25 '19 01:06 dtboy1995

We do have requestShell that returns the same stream, so you can use the same API while using a nicer connection API

steelbrain avatar Jun 25 '19 01:06 steelbrain

Ugh, should've included this in my above message, but if you want a nicer API to write/read from streams, see https://github.com/steelbrain/expected-write

It's been a while since it was updated, but it should work still

steelbrain avatar Jun 25 '19 01:06 steelbrain

@steelbrain ,Thank you very much Can node-ssh implement similar functions? This is actually a common requirement

dtboy1995 avatar Jun 25 '19 01:06 dtboy1995