当我们尝试运行并使用mtrpacket get below error获取traceroute输出时
import asyncio
import mtrpacket
# A simple coroutine which will start an mtrpacket session and
# ping localhost
async def probe():
async with mtrpacket.MtrPacket() as mtr:
return await mtr.probe('10.11.12.13')
# Use asyncio's event loop to start the coroutine and wait for the probe
loop = asyncio.get_event_loop()
try:
result = loop.run_until_complete(probe())
finally:
loop.close()
# Print the probe result
print(result)
Error: mtrpacket.ProcessError: failure to communicate with subprocess "mtr-packet" (is it installed and in the PATH?)
mtr-packet: Failure to open IPv4 sockets: Permission denied
mtr-packet: Failure to open IPv6 sockets: Permission denied
有什么建议吗?
可能是套接字权限,尝试使用sudo访问,可能会成功。
mtr-packet
设计为使用原始套接字访问运行。如果使用的是Linux,可以使用功能授予二进制套接字访问权限。在其他类unix操作系统上,包括MacOS,它应该是suid root。
下面的命令将授予它访问原始套接字的权限。
Linux:
sudo chown root $(which mtr-packet)
sudo setcap cap_net_raw+ep $(which mtr-packet)
对于其他操作系统:
sudo chown root $(which mtr-packet)
sudo chmod u+s $(which mtr-packet)