简体   繁体   English

如何使用python在aws lambda中添加ping模块

[英]How to add ping module in aws lambda using python

I want to monitor a server using AWS Lambda function with Python 3.9 version.我想使用带有 Python 3.9 版本的 AWS Lambda 函数来监控服务器。

I'm using ping test connection and here's my code我正在使用 ping 测试连接,这是我的代码

import subprocess
import platform

def lambda_handler(event, context):
    SERVERS = [
        ('203.124.136.164', 'Local Host 1')
    ]

    for (server, name) in SERVERS:
        check_connection(server, name)
            
def check_connection(server, name):
    if ping(server):
        print("%s is UP" % (name))
    else:
        print("%s is DOWN" % (name))

def ping(server):
    try:
        output = subprocess.check_output("ping -{} 1 {}".format('n' if platform.system().lower() == "windows" else 'c', server ), shell=True, universal_newlines=True)
        if 'unreachable' in output:
            print('unreachable')
            return False
        elif 'timed out' in output:
            print('timed out')
            return False
        else:
            print('success')
            return True
    except Exception as err:
        print("An error occurred: %s" % (err.__str__()))
        return False

But I got an error:但我得到了一个错误:

/bin/sh: ping: command not found
An error occurred: Command 'ping -c 1 203.124.136.164' returned non-zero exit status 127.

Why I got that error and what is the right implementation to monitor a server using IP?为什么我得到这个错误,使用 IP 监控服务器的正确实现是什么? I'm just a beginner.我只是一个初学者。 Please help!请帮忙!

Disclaimer: the IP provided on the code is just dummy.免责声明:代码中提供的 IP 只是虚拟的。

I think AWS lambda doesnt allow for ICMP to go outbound, it only allows for TCP or UDP outbound so since ping is neither it doesnt work.我认为 AWS lambda 不允许 ICMP 出站,它只允许 TCP 或 UDP 出站,因此因为 ping 既不是它也不起作用。 Even if you try to make a custom layer and import to Lambda you will get permissions error since ICMP is not allowed.即使您尝试创建自定义层并导入到 Lambda,您也会收到权限错误,因为不允许使用 ICMP。

Sorry my friend, the easiest work around i believe would to make a T1.micro to run the full python code.对不起,我的朋友,我认为最简单的工作是制作一个 T1.micro 来运行完整的 python 代码。

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

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