Kud*_*udu 150 python ping icmp python-3.x
在Python中,有没有办法通过ICMP ping服务器,如果服务器响应则返回TRUE,如果没有响应则返回FALSE?
10f*_*low 150
如果您不需要支持Windows,这里有一个非常简洁的方法:
import os
hostname = "google.com" #example
response = os.system("ping -c 1 " + hostname)
#and then check the response...
if response == 0:
print hostname, 'is up!'
else:
print hostname, 'is down!'
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为如果连接失败,ping将返回非零值.(返回值实际上因网络错误而异.)您还可以使用'-t'选项更改ping超时(以秒为单位).注意,这会将文本输出到控制台.
ePi*_*314 91
此函数适用于Python 2和Python 3.它适用于任何操作系统(Unix,Linux,macOS和Windows),但在Windows上,os.system如果出现subprocess.call错误,它仍将返回.
import platform # For getting the operating system name
import subprocess # For executing a shell command
def ping(host):
"""
Returns True if host (str) responds to a ping request.
Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
"""
# Option for the number of packets as a function of
param = '-n' if platform.system().lower()=='windows' else '-c'
# Building the command. Ex: "ping -c 1 google.com"
command = ['ping', param, '1', host]
return subprocess.call(command) == 0
Run Code Online (Sandbox Code Playgroud)
该命令适用subprocess.run()于Windows和类Unix系统.选项-n(Windows)或-c(Unix)控制在此示例中为1的数据包数.
True返回平台名称.防爆.Destination Host Unreachable在macOS上
ping执行系统调用.防爆.-n.请注意,-c如果您使用的是Python 3.5+ ,则文档建议使用.
小智 37
有一个名为pyping的模块可以做到这一点.它可以用pip安装
pip install pyping
Run Code Online (Sandbox Code Playgroud)
使用它非常简单,但是,使用此模块时,由于它正在制作原始数据包,因此需要root访问权限.
import pyping
r = pyping.ping('google.com')
if r.ret_code == 0:
print("Success")
else:
print("Failed with {}".format(r.ret_code))
Run Code Online (Sandbox Code Playgroud)
roh*_*lla 34
在python3中使用socket包:
import socket
def ping_server(server: str, port: int, timeout=3):
"""ping server"""
try:
socket.setdefaulttimeout(timeout)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((server, port))
except OSError as error:
return False
else:
s.close()
return True
Run Code Online (Sandbox Code Playgroud)
mlu*_*bke 28
import subprocess
ping_response = subprocess.Popen(["/bin/ping", "-c1", "-w100", "192.168.0.1"], stdout=subprocess.PIPE).stdout.read()
Run Code Online (Sandbox Code Playgroud)
kra*_*etz 16
由于发送原始 ICMP 数据包所需的特权提升,程序化 ICMP ping 很复杂,并且调用ping二进制文件很丑陋。对于服务器监控,您可以使用称为TCP ping的技术实现相同的结果:
# pip3 install tcping
>>> from tcping import Ping
# Ping(host, port, timeout)
>>> ping = Ping('212.69.63.54', 22, 60)
>>> ping.ping(3)
Connected to 212.69.63.54[:22]: seq=1 time=23.71 ms
Connected to 212.69.63.54[:22]: seq=2 time=24.38 ms
Connected to 212.69.63.54[:22]: seq=3 time=24.00 ms
Run Code Online (Sandbox Code Playgroud)
在内部,这只是建立到目标服务器的 TCP 连接并立即断开它,测量经过的时间。这个特定的实现有点受限,因为它不处理关闭的端口,但对于您自己的服务器,它运行得很好。
小智 13
因为我喜欢在2.7和3.x版本以及平台Linux,Mac OS和Windows上使用Python程序,所以我不得不修改现有示例.
# shebang does not work over all platforms
# ping.py 2016-02-25 Rudolf
# subprocess.call() is preferred to os.system()
# works under Python 2.7 and 3.4
# works under Linux, Mac OS, Windows
def ping(host):
"""
Returns True if host responds to a ping request
"""
import subprocess, platform
# Ping parameters as function of OS
ping_str = "-n 1" if platform.system().lower()=="windows" else "-c 1"
args = "ping " + " " + ping_str + " " + host
need_sh = False if platform.system().lower()=="windows" else True
# Ping
return subprocess.call(args, shell=need_sh) == 0
# test call
print(ping("192.168.17.142"))
Run Code Online (Sandbox Code Playgroud)
小智 8
我用以下方法解决这个问题:
def ping(self, host):
res = False
ping_param = "-n 1" if system_name().lower() == "windows" else "-c 1"
resultado = os.popen("ping " + ping_param + " " + host).read()
if "TTL=" in resultado:
res = True
return res
Run Code Online (Sandbox Code Playgroud)
“TTL”是判断 ping 是否正确的方法。萨卢多斯
如果您的服务器不支持 ICMP(防火墙可能会阻止它),它很可能仍然在 TCP 端口上提供服务。在这种情况下,您可以执行TCP ping 1(独立于平台且无需安装额外的 python 模块),如下所示:
import socket
def isReachable(ipOrName, port, timeout=2):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
try:
s.connect((ipOrName, int(port)))
s.shutdown(socket.SHUT_RDWR)
return True
except:
return False
finally:
s.close()
Run Code Online (Sandbox Code Playgroud)
该代码取自此处,仅稍加修改。
1 TCP ping并不真正存在,因为 ping 是在 ISO/OSI 第 3 层上使用 ICMP 执行的。TCP ping 是在 ISO/OSI 第 4 层上执行的。它只是尝试以最基本的方式连接到 TCP 端口,即它不传输任何数据,而是在连接后立即关闭连接。
小智 6
#!/usr/bin/python3
import subprocess as sp
def ipcheck():
status,result = sp.getstatusoutput("ping -c1 -w2 " + str(pop))
if status == 0:
print("System " + str(pop) + " is UP !")
else:
print("System " + str(pop) + " is DOWN !")
pop = input("Enter the ip address: ")
ipcheck()
Run Code Online (Sandbox Code Playgroud)
小智 6
环顾四周之后,我最终编写了自己的ping模块,该模块用于监控大量地址,是异步的,不会占用大量系统资源.你可以在这里找到它:https://github.com/romana/multi-ping/它是Apache许可的,所以你可以以你认为合适的任何方式在你的项目中使用它.
实现我自己的主要原因是其他方法的限制:
我的 ping 功能版本:
import platform, subprocess
def ping(host_or_ip, packets=1, timeout=1000):
''' Calls system "ping" command, returns True if ping succeeds.
Required parameter: host_or_ip (str, address of host to ping)
Optional parameters: packets (int, number of retries), timeout (int, ms to wait for response)
Does not show any output, either as popup window or in command line.
Python 3.5+, Windows and Linux compatible (Mac not tested, should work)
'''
# The ping command is the same for Windows and Linux, except for the "number of packets" flag.
if platform.system().lower() == 'windows':
command = ['ping', '-n', str(packets), '-w', str(timeout), host_or_ip]
# run parameters: capture output, discard error messages, do not show window
result = subprocess.run(command, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, creationflags=0x08000000)
# 0x0800000 is a windows-only Popen flag to specify that a new process will not create a window.
# On Python 3.7+, you can use a subprocess constant:
# result = subprocess.run(command, capture_output=True, creationflags=subprocess.CREATE_NO_WINDOW)
# On windows 7+, ping returns 0 (ok) when host is not reachable; to be sure host is responding,
# we search the text "TTL=" on the command output. If it's there, the ping really had a response.
return result.returncode == 0 and b'TTL=' in result.stdout
else:
command = ['ping', '-c', str(packets), '-w', str(timeout), host_or_ip]
# run parameters: discard output and error messages
result = subprocess.run(command, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return result.returncode == 0
Run Code Online (Sandbox Code Playgroud)
随意使用它。
小智 5
#!/usr/bin/python3
import subprocess as sp
ip = "192.168.122.60"
status,result = sp.getstatusoutput("ping -c1 -w2 " + ip)
if status == 0:
print("System " + ip + " is UP !")
else:
print("System " + ip + " is DOWN !")
Run Code Online (Sandbox Code Playgroud)
确保安装了Pyping或安装它pip install pyping
#!/usr/bin/python
import pyping
response = pyping.ping('Your IP')
if response.ret_code == 0:
print("reachable")
else:
print("unreachable")
Run Code Online (Sandbox Code Playgroud)
我使用这篇文章中答案的想法进行了缩减,但仅使用了较新的推荐子流程模块和 python3:
import subprocess
import platform
operating_sys = platform.system()
nas = '192.168.0.10'
def ping(ip):
# ping_command = ['ping', ip, '-n', '1'] instead of ping_command = ['ping', ip, '-n 1'] for Windows
ping_command = ['ping', ip, '-n', '1'] if operating_sys == 'Windows' else ['ping', ip, '-c 1']
shell_needed = True if operating_sys == 'Windows' else False
ping_output = subprocess.run(ping_command,shell=shell_needed,stdout=subprocess.PIPE)
success = ping_output.returncode
return True if success == 0 else False
out = ping(nas)
print(out)
Run Code Online (Sandbox Code Playgroud)
对于python3有一个非常简单和方便的Python模块ping3:(pip install ping3)。
from ping3 import ping, verbose_ping
ping('example.com') # Returns delay in seconds.
>>> 0.215697261510079666
Run Code Online (Sandbox Code Playgroud)
该模块还允许自定义一些参数。
| 归档时间: |
|
| 查看次数: |
408416 次 |
| 最近记录: |