简体   繁体   English

从AWS Lambda(NodeJS)发送SSH命令

[英]Send SSH commands from AWS Lambda (NodeJS)

I want to create a function in AWS Lambda Nodejs that can send some SSH commands to a linux machine. 我想在AWS Lambda Nodejs中创建一个可以向Linux机器发送一些SSH命令的函数。 Is it possible? 可能吗?

I know there are some nodejs modules to do this, but AWS Lambda Nodejs doesn't have them, and I don't want to involve any EC2 to this Lambda. 我知道有一些nodejs模块可以做到这一点,但AWS Lambda Nodejs没有它们,我不想让任何EC2参与这个Lambda。

I just want to know how can I, from AWS Lambda function, execute some commands in a linux machine, either by SSH or some other methods that I couldn't think of. 我只想知道如何通过AWS Lambda函数在Linux机器上执行某些命令,无论是通过SSH还是其他一些我无法想到的方法。

This post is relatively old now but for whoever finds this, AWS made a blog post describing mostly what you're looking for: https://aws.amazon.com/fr/blogs/compute/scheduling-ssh-jobs-using-aws-lambda/ 这篇文章现在相对较旧,但对于发现这一点的人来说,AWS发表了一篇博文,主要描述了你所寻找的内容: https//aws.amazon.com/fr/blogs/compute/scheduling-ssh-jobs-using- AWS-的λ/

Obviously the AWS blog post describes how to use this for AWS Services but the example still stands using python. 显然,AWS博客文章描述了如何将其用于AWS服务,但该示例仍然使用python。

选项数量:您可以从Python开始使用ssh,使用来自nodejs的https://www.npmjs.com/package/simple-ssh等软件包。

Can confirm this is working on lambda with Alexa and simple-ssh. 可以通过Alexa和simple-ssh确认这是在使用lambda。 Couple things of note: ssh.on("close" is required because otherwise the lambda function will shut down before the SSH command is finished working. (Lambda shuts down on alexa.execute call.) 注意事项:ssh.on(“关闭”是必需的,否则lambda函数将在SSH命令完成工作之前关闭。(Lambda关闭alexa.execute调用。)

This is one of the top Google results for "ssh lambda aws" so I'm posting this here in hopes of saving some time for others that need to do this. 这是“ssh lambda aws”的谷歌搜索结果之一,所以我在这里发帖,希望能为其他需要这样做的人节省一些时间。

Working example: https://github.com/PockyBum522/alexa_nodejs_send_ssh_commands_lambda 工作示例: https//github.com/PockyBum522/alexa_nodejs_send_ssh_commands_lambda

var SSH = require('simple-ssh');
var Alexa = require("alexa-sdk");

var ssh = new SSH({
    host: 'yourserver.com',
    user: 'username',
    key: `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEApQtRxugrDMU8YNBy2j2Lyk6yAxMSKaiusrNUamXKLxFvdlZ1
6HCN+jjaE7q8OYFEmq9l2B5U8GCYMFldXWBxIv7fvRWyi1ZTw3olaZ8DmGYwPKLM
TOQ3MOm/zcJZbiTY1/cx2CW6NupPX78O42hLKM2iJwp6epgxC5t2Vw==
-----END RSA PRIVATE KEY-----`
});

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.registerHandlers(handlers);

    ssh.on("close", function () {alexa.execute()});
    ssh
        .exec('nohup /home/username/script.sh > /dev/null 2>&1 &', {  // Nohup runs script in BG, > /dev/null redirects output. & also runs in BG. Trying to keep things fast to make alexa response time good.
            out: console.log.bind(console)
        })
        .exec('exit', {                                               // Also for trying to keep things fast.
            out: console.log.bind(console)
        }).start(); 
};

var handlers = {
    'LaunchRequest': function(){
        this.emit(':tell', 'Now opening the garage door.'); 
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM