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)
Related
I have wifi problems frequently, so I decided to create a Python 3 script to execute the following commands on a Windows 10 command line tool:
ipconfig/flushdns
ipconfig/release
ipconfig/renew
As I understand, I need to use either the os module, or the subprocess module to make it work. I just don't know how to execute the commands after invoking ipconfig.
Thanks for the help.
You can use the subprocess.call method for this
import subprocess
print subprocess.call(["ipconfig", "/flushdns"], shell=True)
print subprocess.call(["ipconfig", "/release"], shell=True)
print subprocess.call(["ipconfig", "/renew"], shell=True)
In python, we can call any process using subprocess.
I have a situation where I have to work with interactive terminal where I need output of few commands, using python code.
How can I use subprocess module that will open the interactive terminal and I can further bypass few command and get their out to parse them further?
I am able to use subprocess module for 2 different command that where 2nd one is dependent on output of first one like
ps -aux | grep python
first ps -aux can be passed to 1 subprocess obj and that obj will be used as stdin of another subprocess command where grep python will be processed....
you question is not much clear , so i would answer the part which i understand
How can I use subprocess module that will open the interactive terminal and I can further bypass few command and get their out to parse them further?
i have a ubuntu machine and this is the way i invoke a separate terminal and pass command to them
from subprocess import Popen,PIPE
command='who'
command ='"'+command+' '+';read -n1" '
#subitem = Popen(['gnome-terminal','--disable-factory','-x','bash','-c',command],stdin =PIPE)
subitem = Popen(['gnome-terminal','--disable-factory','-x','bash'],stdin =PIPE)
subitem.communicate(input='your command')
You can further play with this using stdin,stdout,communicate method depending on your requirment
I need to launch an external program from my python script.
This program crashes, so I need to get a core dump from it.
what can i do?
Check out the python resource module. It will let you set the size of core files, etc., just like the ulimit command. Specifically, you want to do something like
resource.setrlimit(resource.RLIMIT_CORE, <size>)
before launching your target program.
My guess at usage (I haven't done this myself) is:
import resource
import subprocess
resource.setrlimit(resource.RLIMIT_CORE,
(resource.RLIM_INFINITY,
resource.RLIM_INFINITY))
command = 'command line to be launched'
subprocess.call(command)
# os.system(command) would work, but os.system has been deprecated
# in favor of the subprocess module
I may not at all understand this correctly, but I am trying to allow a Python program to interface with a subprocess that runs commands as if on a Linux shell.
For example, I want to be able to run "cd /" and then "pwd later in the program and get "/".
I am currently trying to use subprocess.Popen and the communicate() method to send and receive data. The first command, sent with the Popen constructor, runs fine and gives proper output. But I cannot send another command via communicate(input="pwd").
My code so far:
from subprocess i
term=Popen("pwd", stdout=PIPE, stdin=PIPE)
print(flush(term.communicate()))
term.communicate(input="cd /")
print(flush(term.communicate(input="pwd")))
Is there a better way to do this? Thanks.
Also, I am running Python 3.
First of all, you need to understand that running a shell command and running a program aren't the same thing.
Let me give you an example:
>>> import subprocess
>>> subprocess.call(['/bin/echo', '$HOME'])
$HOME
0
>>> subprocess.call(['/bin/echo $HOME'], shell=True)
/home/kkinder
0
Notice that without the shell=True parameter, the text of $HOME is not expanded. That's because the /bin/echo program doesn't parse $HOME, Bash does. What's really happening in the second call is something analogous to this:
>>> subprocess.call(['/bin/bash', '-c', '/bin/echo $HOME'])
/home/kkinder
0
Using the shell=True parameter basically says to the subprocess module, go interpret this text using a shell.
So, you could add shell=True, but then the problem is that once the command finishes, its state is lost. Each application in the stack has its own working directory. So what the directory is will be something like this:
bash - /foo/bar
python - /foo
bash via subprocess - /
After your command executes, the python process's path stays the same and the subprocess's path is discarded once the shell finishes your command.
Basically, what you're asking for isn't practical. What you would need to do is, open a pipe to Bash, interactively feed it commands your user types, then read the output in a non-blocking way. That's going to involve a complicated pipe, threads, etc. Are you sure there's not a better way?
Now,here is an executable program that I run in a shell. But It needs parameter.How can I use python to pass parameter to the program in a shell. I know a tool in unix named 'expect' which can interact with existing software. I want to know if python can do the same thing! My english is not good~sorry~
Use the subprocess module. Basic example:
>>> import subprocess as sub
>>> sub.call(["ls", "-l"])
Basically you can pass the command and its parameters as a list of strings.
EDIT: Reading again your question I wonder if pexpect is indeed what you want.