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)
Related
I need to launch external program from python script, which can be run in command prompt. I've been searching through python documentations and stack overflow but can't find anything helpful to me. I successfully launch cmd with following script:
But I still need to write down more commands like that:
mkdir data
copy data.txt c:\data
I think this is a very easy job with subprocess module but I can't find the way. How can I do this?
Try to use call
subprocess.call(['cmd.exe', 'mkdir data']);
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 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)
I'm currently running an OpenELEC (XBMC) installation on a Raspberry Pi and installed a tool named "Hyperion" which takes care of the connected Ambilight. I'm a total noob when it comes to Python-programming, so here's my question:
How can I run a script that checks if a process with a specific string in its name is running and:
kill the process when it's running
start the process when it's not running
The goal of this is to have one script that toggles the Ambilight. Any idea how to achieve this?
You may want to have a look at the subprocess module which can run shell commands from Python. For instance, have a look at this answer. You can then get the stdout from the shell command to a variable. I suspect you are going to need the pidof shell command.
The basic idea would be along the lines of:
import subprocess
try:
subprocess.check_output(["pidof", "-s", "-x", "hyperiond"])
except subprocess.CalledProcessError:
# spawn the process using a shell command with subprocess.Popen
subprocess.Popen("hyperiond")
else:
# kill the process using a shell command with subprocess.call
subprocess.call("kill %s" % output)
I've tested this code in Ubuntu with bash as the process and it works as expected. In your comments you note that you are getting file not found errors. You can try putting the complete path to pidof in your check_output call. This can be found using which pidof from the terminal. The code for my system would then become
subprocess.check_output(["/bin/pidof", "-s", "-x", "hyperiond"])
Your path may differ. On windows adding shell=True to the check_output arguments fixes this issue but I don't think this is relevant for Linux.
Thanks so much for your help #will-hart, I finally got it working. Needed to change some details because the script kept saying that "output" is not defined. Here's how it now looks like:
#!/usr/bin/env python
import subprocess
from subprocess import call
try:
subprocess.check_output(["pidof", "hyperiond"])
except subprocess.CalledProcessError:
subprocess.Popen(["/storage/hyperion/bin/hyperiond.sh", "/storage/.config/hyperion.config.json"])
else:
subprocess.call(["killall", "hyperiond"])
I am trying to launch a python script from another python script in a new shell window. So far I'm unable to do it. Does anyone knows how can I accomplish this?
for example
import subprocess
process = subprocess.Popen('test.py', shell=True, stdout=subprocess.PIPE)
process.wait()
print (process.returncode)
when I'll run this script, it should launch 'test.py' in a new a new shell window.
I'm using linux, but it will be very helpful if you can provide solution for windows too.
Here's how you could do it on Debian-like systems:
import subprocess
import shlex
process = subprocess.Popen(
shlex.split("""x-terminal-emulator -e 'bash -c "test.py"'"""), stdout=subprocess.PIPE)
process.wait()
print (process.returncode)
Something like it should work for any *nix system.
Many thanks to eudoxos for pointing out x-terminal-emulator!
Instead of launching a shell, launch a terminal running your script. On Linux, xterm -e test.py; the Windows equivalent would be cmd.exe test.py I believe (but I could be wrong).