Python | Execute and parse Linux commands
Prerequisite: Introduction to Linux Shell and Shell Scripting
Linux is one of the most popular operating systems and is a common choice for developers. It is popular because it is open source, it’s free and customizable, it is very robust and adaptable.
An operating system mainly consists of two parts: The kernel and the Shell. The kernel basically handles communication between the software and the hardware. The shell takes inputs or commands from the user and produces an output. Most Linux distributions nowadays use the BASH shell (Bourne again shell). Shell commands and scripts are very powerful and are used commonly by developers.
In this article, we shall look at executing and parsing Linux commands using python.
Subprocess is a module in Python that allows us to start new applications or processes in Python. This module intends to replace several older modules in python. We can use this module to run other programs or execute Linux commands.
Starting a process –
A new process can be spawned by using the Popen function defined in the subprocess module. It is a constructor for the Popen class that takes arguments to set up the new process. The underlying process creation and management in this module is handled by the Popen class.
- The first parameter is a list that contains the commands and their options if any.
the above example is equivalent to typing ‘ls -l’ in the terminal
- The second parameter is the stdout value. it specifies the standard output.
stdout = subprocess.PIPE
This indicates that a new pipe or redirection should be created. The default value is
“None”, which means that no redirection will occur.
We can retrieve the output of a command by using the communicate function. It reads data from stdout and stderr until it reaches the end-of-file and waits for the process to terminate. It returns a tuple that contains the output data and the error if any.
data = subprocess.Popen(['ls', '-l', filename], stdout = subprocess.PIPE)
output = data.communicate()
The output of the executed command is stored in data. Using these functions, we can execute Linux commands and fetch their output.
Listing the directories –
We can use the ‘ls’ command with options such as ‘-l’, ‘-al’, etc to list all the files in the current directory. We can then parse this output and print it in a presentable format. The
get_permissions() function parses the output of the list command and retrieves only the names of the files and their corresponding permissions.
Ping command –
The ping command stands for Packet Internet Groper. It is most commonly used to check the connectivity between two systems or nodes. Using the ping command, we can check whether the connection between one node and another is healthy or not. It exchanges packets of data between two nodes and also calculates the round trip time.
Changing permissions –
chmod command can be used to change file permissions. It is an abbreviation of change mode. More info can be found here