Search code examples
pythonlinuxtrafficshaping

How to run bash 'tc' command within python?


I want to us traffic control of the Linux kernel with Python to simulate lost, corrupt and duplicate packages. I'm already able to configure this with the Linux Terminal, but I have to use python.

bash cmd works:

tc filter show dev eth1

python doesn't work:

>>> 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

Thanks.


Solution

  • The python subprocess doesn't know about your shell environment. So provide absolute path to your command, something like:

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

    find the exact location with command which tc in your shell.