gpt4 book ai didi

python - 如何在 python 中运行 bash 'tc' 命令?

转载 作者:太空宇宙 更新时间:2023-11-04 08:59:01 24 4
gpt4 key购买 nike

我想用 Python 对 Linux 内核进行流量控制,以模拟丢失、损坏和重复的包。我已经能够使用 Linux 终端进行配置,但我必须使用 python。

bash cmd 工作:

tc filter show dev eth1

python 不工作:

>>> subprocess.call(["tc", "filter", "show", "dev", "eth1"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/subprocess.py", line 470, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.6/subprocess.py", line 623, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1141, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

谢谢。

最佳答案

python 子进程不知道您的 shell 环境。因此,为您的命令提供绝对路径,例如:

subprocess.call(["/sbin/tc", "filter", "show", "dev", "eth1"])

在您的 shell 中使用命令 which tc 查找确切位置。

关于python - 如何在 python 中运行 bash 'tc' 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25287125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com