【问题标题】:ping through linux and get output in python通过 linux ping 并在 python 中获取输出
【发布时间】:2016-10-16 02:16:23
【问题描述】:

我正在尝试创建提供 whois 和外部 ip 并 ping 站点以查找其雕像的小脚本。我的代码运行良好,除了 ping 部分。它正在ping,但不是我要求的3个限制。我正在尝试在 ubuntu 服务器上运行它有什么建议吗?

import os

os.system("clear") # clear the screen

inp = input("Enter your choice from the menu: \n1) To find your external IP address\n2) To check domain name whois information\n3) To check if the website or ip address is up\n")

if (inp == "1"):
 print("Your external ip address is: ") # me trying to be smart XD
 ip = os.system("dig +short myip.opendns.com @resolver1.opendns.com")
 #print("Your external ip address is: %d" % ip)

elif (inp == "2"):

 domain = input("Enter the domain you want to whois it: \n")

 info = os.system("whois %s" % domain)
 print(info)

elif (inp == "3"):
 target = input("Enter the url or ip address you want to check:\n")

 for x in range(3):
     response = os.system("ping %s" % target)

 if (response == 1): # how can I decide based on response result ?
     print("%s is up" % target)
 else:
     print("%s is down" % target)

【问题讨论】:

  • 你试过在ubuntu命令行上运行ping命令吗?
  • stackoverflow.com/help/mcve 阅读有关用于 StackOverflow Qs 的最小代码示例。也就是说,我们不需要所有其他代码,只需要 ping 的东西。并给出下面的答案,学习使用 Linux/unix 的文档,即man ping。祝你好运。

标签: python linux if-statement for-loop os.system


【解决方案1】:

尝试将 count -c 选项添加到您的命令中:

target = "stackoverflow.com"
response = os.system("ping %s -c 3" % target)

if response is not 0:
    print("failed")
else:
    print("success")

你也可以取消循环...

【讨论】:

  • 不客气,请将此答案标记为“正确”。
猜你喜欢
  • 2015-03-15
  • 1970-01-01
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 2014-08-03
  • 2021-05-27
相关资源
最近更新 更多