有谁知道如何使用python ping本地主机以查看它是否处于活动状态?我们(我的团队和我)已经尝试过使用
os.system("ping 192.168.1.*")
但目标无法访问的响应与主机响应的响应相同。
感谢您的帮助。
答案 0 :(得分:12)
使用此...
import os
hostname = "localhost" #example
response = os.system("ping -n 1 " + hostname)
#and then check the response...
if response == 0:
print(hostname, 'is up!')
else:
print(hostname, 'is down!')
如果在unix / Linux上使用此脚本,请将-n开关替换为-c!
多数民众赞成:)
答案 1 :(得分:6)
我发现使用os.system(...)导致误报(正如OP所说,'目标主机无法访问'== 0)。
如前所述,使用subprocess.Popen可以正常工作。为简单起见,我建议这样做,然后解析结果。您可以轻松地执行此操作:
if ('unreachable' in output):
print("Offline")
只需检查要从ping结果中检查的各种输出。在“那个”中制作一个'this'来检查它。
示例:
import subprocess
hostname = "10.20.16.30"
output = subprocess.Popen(["ping.exe",hostname],stdout = subprocess.PIPE).communicate()[0]
print(output)
if ('unreachable' in output):
print("Offline")
答案 2 :(得分:5)
我一段时间编写了一个小程序。它可能不是你想要的东西,但你总是可以在主机操作系统上运行一个程序,在启动时打开一个套接字。这是ping程序本身:
# Run this on the PC that want to check if other PC is online.
from socket import *
def pingit(): # defining function for later use
s = socket(AF_INET, SOCK_STREAM) # Creates socket
host = 'localhost' # Enter the IP of the workstation here
port = 80 # Select port which should be pinged
try:
s.connect((host, port)) # tries to connect to the host
except ConnectionRefusedError: # if failed to connect
print("Server offline") # it prints that server is offline
s.close() #closes socket, so it can be re-used
pingit() # restarts whole process
while True: #If connected to host
print("Connected!") # prints message
s.close() # closes socket just in case
exit() # exits program
pingit() #Starts off whole process
在这里你有一个可以接收ping请求的程序:
# this runs on remote pc that is going to be checked
from socket import *
HOST = 'localhost'
PORT = 80
BUFSIZ = 1024
ADDR = (HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)
while 1:
clientsock, addr = serversock.accept()
serversock.close()
exit()
要在没有实际显示的情况下运行程序,只需将文件另存为.pyw而不是.py。 在用户检查正在运行的进程之前,它会使其不可见。
希望它能帮到你
答案 3 :(得分:1)
试试这个:
ret = os.system("ping -o -c 3 -W 3000 192.168.1.10")
if ret != 0:
print "Host is not up"
-o只等待一个数据包
-W 3000只能回复数据包3000毫秒。
-c 3让它尝试几次,这样你的ping就不会永远运行
答案 4 :(得分:1)
为简单起见,我使用基于套接字的自制函数。
def checkHostPort(HOSTNAME, PORT):
"""
check if host is reachable
"""
result = False
try:
destIp = socket.gethostbyname(HOSTNAME)
except:
return result
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(15)
try:
conn = s.connect((destIp, PORT))
result = True
conn.close()
except:
pass
return result
如果Ip:端口可以访问,则返回True
如果你想模拟Ping,可以参考ping.py
答案 5 :(得分:0)
使用它并解析字符串输出
import subprocess
output = subprocess.Popen(["ping.exe","192.168.1.1"],stdout = subprocess.PIPE).communicate()[0]
答案 6 :(得分:0)
我可以在Windows上找到这样做的最好方法,如果你不想解析输出就是像这样使用Popen:
num = 1
host = "192.168.0.2"
wait = 1000
ping = Popen("ping -n {} -w {} {}".format(num, wait, host),
stdout=PIPE, stderr=PIPE) ## if you don't want it to print it out
exit_code = ping.wait()
if exit_code != 0:
print("Host offline.")
else:
print("Host online.")
这可以按预期工作。退出代码没有误报。我在Windows 7和Windows 10上用Python 2.7和3.4测试过它。