python code execution bash commands related - python3 cook book
Execute external command and get its output
problem
You want to execute an external command and get the results as a Python string.
solution
Use subprocess.check_output()
functions. E.g:
import subprocess
out_bytes = subprocess.check_output(['netstat','-a'])
This code performs a specified command and returns an execution result as a byte string. If you need to return the form of text, plus a decoding steps. E.g:
out_text = out_bytes.decode('utf-8')
If a command is executed to a non-zero return code will throw an exception. The following examples will trap and obtain return code:
try:
out_bytes = subprocess.check_output(['cmd','arg1','arg2'])
except subprocess.CalledProcessError as e:
out_bytes = e.output # Output generated before error
code = e.returncode # Return code
By default, check_output()
only the return value input to the standard output. If you need to collect standard output and error output at the same time, use stderr
parameters:
out_bytes = subprocess.check_output(['cmd','arg1','arg2'],
stderr=subprocess.STDOUT)
If you need to use a timeout mechanism to execute commands, use timeout
parameters:
try:
out_bytes = subprocess.check_output(['cmd','arg1','arg2'], timeout=5)
except subprocess.TimeoutExpired as e:
...
Generally speaking, it does not require execution of the command to the underlying shell environment (such as sh, bash). A list of strings are passed to a low-level system commands, such as os.execve()
. If you want a shell command is executed, passing a string parameter, and set the parameters shell=True
. Sometimes you want to perform a complex Python shell command when this is very useful, such as pipeline flow, I / O redirection and other features. E.g:
out_bytes = subprocess.check_output('grep python | wc > out', shell=True)
Note that the command execution there will be a security risk in the shell, especially when parameter input from the user. This time you can use the shlex.quote()
function to correct parameters in double quote quotes.
discuss
Use check_output()
function is to perform an external command and easiest way to get its return value. However, if a child process you need to do more complex interactions, such as sending input to it, you have to use another method. This time can be used directly subprocess.Popen
category. E.g:
import subprocess
# Some text to send
text = b'''
hello world
this is a test
goodbye
'''
# Launch a command with pipes
p = subprocess.Popen(['wc'],
stdout = subprocess.PIPE,
stdin = subprocess.PIPE)
# Send the data and get the output
stdout, stderr = p.communicate(text)
# To interpret as text, decode
out = stdout.decode('utf-8')
err = stderr.decode('utf-8')
subprocess
TTY module dependent external commands for inappropriate use. For example, you can not use it to automate tasks a user to enter a password (for example a ssh session). At this time, you need to use a third-party modules, such as based on the famous expect
family of tools (pexpect or similar)