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.
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.
The basic way if you don't need any special control is to use os.system() to launch a command as if you were in your shell command line (no need to specify the full path if in your $PATH):
import os
os.system("tc filter show dev eth1")
This should work exactly as if you did in your cmd:
$ tc filter show dev eth1
Related
I can't run the wonderful PypeR (r to python interface) anymore.
I can import it, but as I try to run it it crashes.
I suspect it is because I installed El Capitan OSX.
I tried to install update pypeR with no success.
when I run it with:
e.g.
r = R()
that's the error that I get.
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
a = R()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyper.py", line 600, in __init__
self.__dict__['prog'] = Popen(RCMD, stdin=PIPE, stdout=PIPE, stderr=return_err and _STDOUT or childstderr, startupinfo=info)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
does anyone have a clue on how to solve this problem?
update:
if I run it from a shell instead of Idle it works.
I really can't see why.
Python's version is exactly the same, built at the same time.
It looks like PypeR cannot find R to run. Most likely the R command is not in the search path for command ($PATH) when you are using idle. One way is to explicitly point out which R command to use, e.g., if the R command is located in /usr/local/bin, you may use
r = R(RCMD="/usr/local/bin/R")
Of course, it is best if you can add R's path for idle environment.
When i try to run the scan operation using the wifi library as mentioned in the documentation, i get the following error.
(lsbaws)Keshav:bin root# wifi scan
Traceback (most recent call last):
File "/Users/Keshav/Documents/Github/Webserver/webserver/lsbaws/bin/wifi", line 202, in <module>
args.func(args)
File "/Users/Keshav/Documents/Github/Webserver/webserver/lsbaws/bin/wifi", line 51, in scan_command
print_table([[cell.signal, cell.ssid, 'protected' if cell.encrypted else 'unprotected'] for cell in Cell.all(args.interface)])
File "/Users/Keshav/Documents/Github/Webserver/webserver/lsbaws/lib/python2.7/site-packages/wifi/scan.py", line 29, in all
stderr=subprocess.STDOUT)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 566, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I figured that this python wifi library does not work on mac.
An alternative to do wifi scanning is using the tool that comes in mac called airports.
To start using the tool -
$ cd /usr/sbin
$ sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport
The airports -I flag is useful
Also just typing airports will open up the partly explicit manual page
Refrence link - Airports Example
Here's my copy.py:
from subprocess import call
call("copy p2.txt p3.txt")
If in command prompt I use
copy p2.txt p3.txt it copies fine.
but when I use python copy.py it gives me:
Traceback (most recent call last):
File "copy.py", line 2, in <module>
call("copy p2.txt p3.txt")
File "C:\Python27\lib\subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
If I replace the python call to copy with xcopy, it works fine.
Why would this be?
When subprocess.call()ing a command like in a shell, you'll need to specify shell=True as well.
from subprocess import call
call("copy p2.txt p3.txt", shell=True)
The reason you need to use shell=True in this case is that the copy command in Windows is not actually an executable but a built-in command of the shell (if memory serves right). xcopy on the other hand is a real executable (in %WINDIR%\System32, which is usually in the %PATH%), so it can be called outside of a cmd.exe shell.
In this particular instance, shutil.copy or shutil.copy2 might be viable alternatives.
Please note that using shell=True can lead to security hazards, or as the docs put it:
Warning: Using shell=True can be a security hazard. See the warning under Frequently Used Arguments for details.
I have a python script that will read the temperature of from a probe on the GPIO pins of a Raspberry-Pi, and will append that temperature to a log file. Running the script form terminal with sudo permissions works fine:
sudo python /home/pi/temp.py
I've attempted to run the script every 15 minutes from sudo's crontab file with the line:
*/15 * * * * python /home/pi/temp.py
This fails, with the output being
Traceback (most recent call last):
File "/home/pi/temp.py", line 8, in <module>
subprocess.call(['modprobe', 'w1-gpio'])
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I know the issue is with the modprobe subprocess call, but I can't identify what exactly. In my script, I have the following code related to the issue:
import subprocess
subprocess.call(['modprobe', 'w1-gpio'])
subprocess.call(['modprobe', 'w1-therm'])
This is because cron has its own PATH variable and doesn't use the same path that you do.
For that reason, it would be advisable to call any programs that you use (especially through python's subprocess) with an absolute path to the executable
You could do which modprobe on the commandline to find where modprobe lives (probably in /bin/), and then change your call in subprocess.py to subprocess.call(['/bin/modprobe', 'w1-gpio'])
I want to delete bash history with a python script on my Macbook Pro.
I know two ways to delete bash history with bash shell
1.rm ~/.bash_history
2.history -c
But these command does not work in python script with subprocess:
1.rm ~/.bash_history
import subprocess
subprocess.call([‘rm’, ‘~/.bash_history'])
error:
rm: ~/.bash_history: No such file or directory
2.history -c
import subprocess
subprocess.call(['history', '-c'])
error:
File "test.py", line 8, in
subprocess.call(['history', '-c'])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line >524, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line >711, in init
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line >1308, in _execute_child
raise child_exception
Any ideas?
You have two questions here:
First, python doesn't understand ~, you need to expand it:
subprocess.call(['rm', os.path.expanduser('~/.bash_history')])
Second, history is a shell built-in. Use the shell to invoke it:
subprocess.call(['bash', '-c', 'history -c'])