首页 > 解决方案 > How to connect to external SSH server by node js

问题描述

want to connect to another linux server that need SSH connection directly from my Node JS code and to fire some commands like to stop a service and etc using some npm package like "cmd-npm".

But when I trying to do it I can't handling the password authentication immediately after connecting.

I've tried to using npm ssh2 package but I can't pass commands inside the server only to connect to it.

Is there any ideal solution for my use case?

This is the code:

 cmd.get(
    `
    ssh username@host_ip
    cd ..
    ls
`, function (err, data, stderr) {
         await console.log('the current working dir is : ', data)
         if (err) throw await err;
    }
);

标签: javascriptnode.jslinuxsshautomation

解决方案


推荐阅读