【发布时间】:2019-04-15 15:59:54
【问题描述】:
很抱歉再次询问此问题,但我一直无法找到消除不断发生的误报的方法。
当我收到“无法到达目的地”的回复时,它显示所有返回的数据包和 0 个数据包丢失...所以它显示 SERVER UP 而不是 down。
天哪,我该如何解决这个问题?
# Server up/down Script
# - Module Import section
import socket
import sys
import os
import subprocess
# - IP Address input request
hostname1 = input (" Please Enter IP Address: ")
# - Command to run ping request, but also hides ping info
response = subprocess.run(["ping", "-c", "1", hostname1], stdout=subprocess.PIPE)
response.returncode
#___ ORIGINAL CODE ___
#if (response == "Reply from", hostname1):
if response.returncode == 0:
print ( 50 * "-")
print ("[ **SERVER IS ALIVE** ]")
print ( 50 * "-")
elif response.returncode == 0 and (str("Destination host unreachable.")):
print( 50 * "-")
print(hostname1, "[ **SERVER DOWN** ] ")
print( 50 * "-")
else:
print( 50 * "-")
print(hostname1, "[ **SERVER DOWN** ] ")
print( 50 * "-")
【问题讨论】:
-
您没有将
"Destination host unreachable"与任何东西进行比较。 -
不应该
elif说!= 0吗?0代码已由if块处理。 -
不用调用
str(),"Destination host unreachable"已经是字符串了。 -
您需要从管道中读取并测试输出是否包含该流。
标签: python subprocess ping