Simple Python shell wrapper script (using os.execv?) - python

I want to add a wrapper around a specific shell command. This will run in Linux only and I don't care about cross platform support. This code works, but is there a better way to achieve this? Or am I opening myself up to any weird behavior?
import os
import sys
# Do my personal validation here
do_some_validation(sys.argv)
# Now call the real program
os.execv('/usr/bin/some-command', sys.argv)
Thanks!

You may use subprocess
import subprocess
subprocess.call(['/usr/bin/some-command', arg1, arg2])
subprocess is better than os in a way, in that it has more control over execution of a command, whereas os just throws it to bash.

Related

Preventing my script from os command injection python

i am using python 2.7.x
I automating my stuffs and in there i need run to another python program from my python script for that i am using the system function from the 'os' library.
for e.g:
import os
os.system("python anotherscript.py --data <USER_INPUT_FROM_MY_SCRIPT_HERE>")
so i know if any user inputs some other command in place of expected user input that will be converting to os command injection and that's what i want prevent in this case.
Thank you.
Since you need to run a Python script from Python, just import it the Python way and invoke the needed function normally
import anotherscript
anotherscript.<function>("<user_input>")
#Tenchi2xh's answer is the better way to do it, but if that doesn't work (e.g. your script only works on Python 2.x and the other one only works on Python 3.x) then you should use the subprocess module, passing the arguments as a list:
import subprocess
subprocess.call(['python', 'anotherscript.py', '--data', '<USER INPUT>'])
Also take a look at subprocess.check_call and subprocess.check_output to see if they are closer to what you need.
https://docs.python.org/2/library/subprocess.html#subprocess.call

executing OS commands from python using modules

I want to run a python script that can execute OS (linux) commands , I got few modules that helps me in doing that like os, subprocess . In OS module am not able to redirect the output to a variable . In subprocess.popen am not able to use variable in the arguments. Need someone help in finding the alternative .
Am trying to run some OS commands from python script . for example df -h output. It works fine with by using some modules like os or subprocess .But am not able to store those output to any variable .
Here am not able to save this output to a variable . How do I save this to a variable.
i saw multiple other options like subprocess.Popen but am not getting proper output.
Below program i used subprocess module but here I have another issue , as the command is big am not able to use variables in subprocess.Popen.
You can use the subprocess method check_output
import subprocess
output = subprocess.check_output("your command", shell=True)
see previously answered SO question here for more info: https://stackoverflow.com/a/8659333/3264217
Also for more info on check_output, see python docs here:
https://docs.python.org/3/library/subprocess.html#subprocess.check_output
Use either subprocess or pexpect depending on what is your exact use case.
subprocess can do what os.system does and much more. If you need to start some command, wait for it to exit and then get the output, subprocess can do it:
import subprocess
res = subprocess.check_output('ls -l')
But if you need to interact with some command line utility, that is repeatedly read/write, then have a look at pexpect module. It is written for Unix systems but if you ever want to go cross-platform, there is a port for Windows called winpexpect.
spawn's attribute 'before' is probably what you need:
p = pexpect.spawn('/bin/ls')
p.expect(pexpect.EOF)
print p.before
(see the docs)

python open applications through your program

I want my program to open iTunes during runtime. How do I implement this ?
I looked around for answers but didn't get any concrete complete answers. Till now, all I know is I could use the os module and then call the os.system() function to open iTunes. If this is right, what goes into the brackets ?
I have a Mac OS X machine.
One straightforward way to do this on Mac OS X will be to use the open command:
os.system("open -a iTunes")
There are undoubtedly other ways of doing this (e.g, using the Cocoa/Python bridge), but this is the simplest.
This should be able to help you.
From the article linked above...
import sys, string, os, arcgisscripting
os.chdir( 'c:\\documents and settings\\flow_model' )
os.system( '"C:\\Documents and Settings\\flow_model\\flow.exe"' )
Use subprocess.call() if you want to simply run an executable.
os.system() run the command in a subshell, which generates an unnecessary extra process and slightly different behavior depending on the operating system/console used (for example cmd.exe have different escaping than bash)
Read subprocess, is better than os.system in your case.
Subprocess module: http://docs.python.org/2/library/subprocess.html

Running command lines within your Python script

So I have a bunch of aliases and Command Line prompt programs, and my main program works by inputting b into the cmd.exe, followed by some filepath names and what not. How would I run those arguments in my python script? So that it mimics the action i am doing in the cmd?
You should use the subprocess module. In particular, subprocess.call will run command line programs for you.
or you can use
import os
os.system('your_command')
for example:
import os
os.system('notepad')
will launch the notepad with the command line behind.
hope this helps
You can do this using subprocess
For example, this call bellow gets the output of the program and stores it as a string, using .call will help with calling it and for more accurate control use .Popen
subprocess.check_output(["ipconfig"])
Check out Sarge - a wrapper for subprocess which aims to make life easier for anyone who needs to interact with external applications from their Python code. and Plumbum - a small yet feature-rich library for shell script-like programs in Python.

Should i continue clearing the screen with the os module or use subprocess?

So, my question is whether i should use the os.system('cls') for clearing command line output in a python command line program. I usually use the os module way, however, i have read that using the subprocess modules are a better choice for doing calls to the command line. In general, which should i use? And if i do use the subprocess way, how would i go about doing it, as i have very little experience with the module, even though i have tried reading the doc's. Thank you in advance for your replies.
If you want to use the subprocess module to invoke cls instead of os.system(), just:
import subprocess
subprocess.call("cls", shell=True)
If you want something more complex see the subprocess documentation.

Categories