【问题标题】:How to add ping module in aws lambda using python如何使用 python 在 aws lambda 中添加 ping 模块
【发布时间】:2021-10-30 12:31:44
【问题描述】:

我想使用 Python 3.9 版本的 AWS Lambda 函数监控服务器。

我正在使用 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

但我得到了一个错误:

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

为什么会出现该错误?使用 IP 监控服务器的正确实施方式是什么? 我只是一个初学者。请帮忙!

免责声明:代码上提供的IP只是虚拟IP。

【问题讨论】:

    标签: python-3.x aws-lambda


    【解决方案1】:

    我认为 AWS Lambda 不允许 ICMP 出站。它只允许 TCP 或 UDP 出站,因此由于 ping 不是它不起作用。即使您尝试制作自定义层并导入到 Lambda,您也会收到权限错误,因为不允许使用 ICMP。

    对不起,我的朋友,我认为最简单的解决方法是制作一个 T1.micro 来运行完整的 python 代码。

    【讨论】:

    • 您知道任何 AWS 文档说 lambda 不允许 ICMP 出站吗?
    猜你喜欢
    • 2021-07-25
    • 2016-03-30
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 2018-11-26
    相关资源
    最近更新 更多