【问题标题】:How to properly get the results from subprocess.run()?如何正确获取 subprocess.run() 的结果?
【发布时间】: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


【解决方案1】:

您在代码中的表达式(str("Destination host unreachable.") 将始终计算为True——任何非空字符串的真实性。

如果您想查看特定字符串是否在由调用的子流程产生的stdout 输出中,您需要使用类似这样的表达式:

("Destination host unreachable." in response.stdout.decode("utf-8"))

response = subprocess.run(...) 通话之后。

这里有一个article 描述如何使用subprocess 模块(特别注意Capturing Output 部分。

【讨论】:

  • 感谢您的建议。捕获输出是我的下一个攻击计划......但我仍然是一个新手,我正在网上拖网以找出方法。
  • Alan:您问题中的代码已经捕获了stdout。我的答案显示了如何通过 response.stdout.decode("utf-8") 表达式将捕获的内容转换为字符串,然后检查该字符串是否在其中。请看What should I do when someone answers my question?
  • 我理解你所说的并且标准输出已经被捕获,但我想先了解它是如何工作的,所以我了解它是如何工作的。无论如何,我已经学到了足够的知识来让代码工作并获得正确的响应,即使显示“无法到达目标”也是如此。
  • 艾伦:好的。我只是想确保您理解我的答案中的代码,听起来像您一样。如果您觉得我的回答有帮助,请阅读What should I do when someone answers my question?
【解决方案2】:
# - Import section - below are the modules I will need to call for this script
import socket
import sys
import os
import subprocess
import ctypes
import ipaddress

# - User input request for IP or hostanme
hostname1 = input (" Please Enter IP Address: ")

ip_net = ipaddress.ip_network(hostname1)

# Configure subprocess to hide the console window
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE

output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(hostname1)], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]

if "Destination host unreachable" in output.decode('utf-8'):
    print ( 50 * "-")
    print (ctypes.windll.user32.MessageBoxW(0, hostname1 +  " [ SERVER OFFLINE ] ", " *** SERVER STATUS - ALERT *** ", 0))
    print ( 50 * "-")
elif "Request timed out" in output.decode('utf-8'):
    print( 50 * "-")
    print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " [ SERVER OFFLINE ] ", "*** SERVER STATUS - ALERT ***", 0))
    print( 50 * "-")
else:
    print( 50 * "-")
    print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " *** SERVER ALIVE *** ", "** SERVER STATUS - ALERT **", 0))
    print( 50 * "-")


#------------------------------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-08
    • 2020-09-22
    • 2014-06-17
    • 2017-11-19
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多