【发布时间】: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