Now we all are aware about “What is Python?” and “How to write a Hello World code?” No problem, if you do not know, please read this post >> Introduction to Python – Say Hello World! << before starting about “subprocess.” You will get a brief idea from there.
There are a lot of websites are available on internet to refer Python. Here I would like to integrate directly Python and Linux which will be helpful for Linux SysAdmins! Bookmark the Index page and read more about Python.
What is subprocess module? Why this module has a great job with Linux?
As we are SysAdmins, we need the advantageous of Python in Shell. We need it to simplify our jobs with Python. Yup, I am here to help you with my little knowledge 🙂
Okay, let’s move with the module “subprocess.” This is a Python module for administration tasks. This module should be used for accessing system commands. This is the simplest way of running Linux commands Yeah, that’s really what we need!
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.
This module intends to replace several older modules and functions such as listed below:
os.system
os.spawn*
os.popen*
popen2.*
commands.*
Syntax
subprocess.call
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
This is the default syntax of subprocess module. It runs the command described by args. Wait for command to complete, then return the returncode attribute. The default value for shell is False and is not recommended to use True value if you are not very much confidence about the input.
What is the difference between shell=False and shell=True in Python subprocess.call?
In shell=True, the specified command in the syntax will be executed through the Shell. This should be helpful, if you want to integrate other shell features like shell pipes, filename wildcards, environment variable expansion, and expansion of ~ to a user’s home directory into the existing code.
When shell=True is dangerous?
If we execute shell commands that might include unsanitized input from an untrusted source, it will make a program vulnerable to shell injection, a serious security flaw which can result in arbitrary command execution. For this reason, the use of shell=True is strongly discouraged in cases where the command string is constructed from external input.
Refer Stack Overflow for more questions related with shell=True
Creating Python scripts for Linux SysAdmins – usages of Python module subprocess
Okay, we need scripts to execute in Shell. In Python, you need to import the module subprocess (or other like “os”) before writing the commands in your code.
This can be done in many ways!
First I am illustrating this with the help of ipython.
Method I : Create scripts as string
Step 1 : SSH to server
Step 2 : Access ipython
Step 3 : Execute the following:
To get Kernel details: Eg 1
u = "uname"
arg = "-a"
print "Gathering system information with %s command:\n" % u
subproccess.call ([u, arg])
To display disk usage details: Eg 2
disk = "df"
usage = "-h"
print "Total disk details using %s command:" % disk
subprocess.call([disk, usage])