Python subprocess linux command

Executing Shell Commands with Python

A sysadmin would need to execute shell commands in Python scripts. Learn how to execute shell commands in Python.

Python is an excellent scripting language. More and more sysadmins are using Python scripts to automate their work.

Since the sysadmin tasks involve Linux commands all the time, running Linux commands from the Python script is a great help.

In this tutorial, I’ll show you a couple of ways you can run shell commands and get its output in your Python program.

Execute Shell command in Python with os module

Let me create a simple python program that executes a shell command with the os module.

import os myCmd = 'ls -la' os.system(myCmd)

Now, if I run this program, here’s what I see in the output.

python prog.py total 40 drwxr-xr-x 3 abhishek abhishek 4096 Jan 17 15:58 . drwxr-xr-x 49 abhishek abhishek 4096 Jan 17 15:05 .. -r--r--r-- 1 abhishek abhishek 456 Dec 11 21:29 agatha.txt -rw-r--r-- 1 abhishek abhishek 0 Jan 17 12:11 count -rw-r--r-- 1 abhishek abhishek 14 Jan 10 16:12 count1.txt -rw-r--r-- 1 abhishek abhishek 14 Jan 10 16:12 count2.txt --w-r--r-- 1 abhishek abhishek 356 Jan 17 12:10 file1.txt -rw-r--r-- 1 abhishek abhishek 356 Dec 17 09:59 file2.txt -rw-r--r-- 1 abhishek abhishek 44 Jan 17 15:58 prog.py -rw-r--r-- 1 abhishek abhishek 356 Dec 11 21:35 sherlock.txt drwxr-xr-x 3 abhishek abhishek 4096 Jan 4 20:10 target

That’s the content of the directory where prog.py is stored.

If you want to use the output of the shell command, you can store it in a file directly from the shell command:

import os myCmd = 'ls -la > out.txt' os.system(myCmd)

You can also store the output of the shell command in a variable in this way:

import os myCmd = os.popen('ls -la').read() print(myCmd)

If you run the above program, it will print the content of the variable myCmd and it will be the same as the output of the ls command we saw earlier.

Now let’s see another way of running Linux command in Python.

Execute shell command in Python with subprocess module

A slightly better way of running shell commands in Python is using the subprocess module.

If you want to run a shell command without any options and arguments, you can call subprocess like this:

import subprocess subprocess.call("ls")

The call method will execute the shell command. You’ll see the content of the current working directory when you run the program:

python prog.py agatha.txt count1.txt file1.txt prog.py target count count2.txt file2.txt sherlock.txt

If you want to provide the options and the arguments along with the shell command, you’ll have to provide them in a list.

import subprocess subprocess.call(["ls", "-l", "."])

When you run the program, you’ll see the content of the current directory in the list format.

Читайте также:  Java check windows or linux

Now that you know how to run shell command with subprocess, the question arises about storing the output of the shell command.

For this, you’ll have to use the Popen function. It outputs to the Popen object which has a communicate() method that can be used to get the standard output and error as a tuple. You can learn more about the subprocess module here.

import subprocess MyOut = subprocess.Popen(['ls', '-l', '.'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout,stderr = MyOut.communicate() print(stdout) print(stderr)

When you run the program, you’ll see the stdout and stderr (which is none in this case).

python prog.py total 32 -r--r--r-- 1 abhishek abhishek 456 Dec 11 21:29 agatha.txt -rw-r--r-- 1 abhishek abhishek 0 Jan 17 12:11 count -rw-r--r-- 1 abhishek abhishek 14 Jan 10 16:12 count1.txt -rw-r--r-- 1 abhishek abhishek 14 Jan 10 16:12 count2.txt --w-r--r-- 1 abhishek abhishek 356 Jan 17 12:10 file1.txt -rw-r--r-- 1 abhishek abhishek 356 Dec 17 09:59 file2.txt -rw-r--r-- 1 abhishek abhishek 212 Jan 17 16:54 prog.py -rw-r--r-- 1 abhishek abhishek 356 Dec 11 21:35 sherlock.txt drwxr-xr-x 3 abhishek abhishek 4096 Jan 4 20:10 target None

I hope this quick tip helped you to execute shell command in Python programs. In a related quick tip, you can learn to write list to file in Python.

If you have questions or suggestions, please feel free to drop a comment below.

Источник

How to Execute Linux Commands in Python

Linux is one of the most popular operating systems used by software developers and system administrators. It is open-source, free, customizable, very robust, and adaptable. Making it an ideal choice for servers, virtual machines (VMs), and many other use cases.

Therefore, it is essential for anyone working in the tech industry to know how to work with Linux because it is used almost everywhere. In this tutorial, we are going to look at how we can automate and run Linux commands in Python.

Table of contents

Prerequisites

Introduction

Python has a rich set of libraries that allow us to execute shell commands.

A naive approach would be to use the os library:

import os cmd = 'ls -l' os.system(cmd) 

The os.system() function allows users to execute commands in Python. The program above lists all the files inside a directory. However, we can’t read and parse the output of the command.

In some commands, it is imperative to read the output and analyze it. The subprocess library provides a better, safer, and faster approach for this and allows us to view and parse the output of the commands.

OS subprocess
os.system function has been deprecated. In other words, this function has been replaced. The subprocess module serves as a replacement to this and Python officially recommends using subprocess for shell commands.
os.system directly executes shell commands and is susceptible to vulnerabilities. The subprocess module overcomes these vulnerabilities and is more secure.
The os.system function simply runs the shell command and only returns the status code of that command. The subprocess module returns an object that can be used to get more information on the output of the command and kill or terminate the command if necessary. This cannot be done in the os module.
Читайте также:  Pidgin для linux установка

Although you can execute commands using the OS module, the subprocess library provides a better and newer approach and is officially recommended. Therefore, we are going to use subprocess in this tutorial. This documentation explores the motivation behind creating this module.

Building an application to ping servers

Let’s use the subprocess library to write a script that pings multiple servers to see whether they are reachable or not. This would be a good use case when you have multiple hosts, servers, or VMs(AWS ec2 instances) and want to check if they are up and running without any problems.

A simple solution is to just ping these servers and see if they respond to the request. However, when you have a considerable amount of machines, it will be extremely tedious and time-consuming to manually ping them. A better approach is to use Python to automate this process.

Code

According to the official documentation, 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. The subprocess library has a class called Popen() that allows us to execute shell commands and get the output of the command.

Create a Python file and add the following code. We also need to create a file called “servers.txt”, where we can add a list of all the servers we need to ping. The Python script will read from this file and ping each server listed in it.

Servers

I have added 4 servers, out of which two exist and the other two do not. Only the servers that exist can be “pinged”.

import subprocess  def ping(servers):   # The command you want to execute  cmd = 'ping'   # send one packet of data to the host  # this is specified by '-c 1' in the argument list  outputlist = []  # Iterate over all the servers in the list and ping each server  for server in servers:  temp = subprocess.Popen([cmd, '-c 1', server], stdout = subprocess.PIPE)  # get the output as a string  output = str(temp.communicate())  # store the output in the list  outputlist.append(output)  return outputlist  if __name__ == '__main__':   # Get the list of servers from the text file  servers = list(open('servers.txt'))  # Iterate over all the servers that we read from the text file  # and remove all the extra lines. This is just a preprocessing step  # to make sure there aren't any unnecessary lines.  for i in range(len(servers)):  servers[i] = servers[i].strip('\n')  outputlist = ping(servers)   # Uncomment the following lines to print the output of successful servers  # print(outputlist) 

Output

As you can see in the output, we get the message “name or service not known” for the two servers that did not exist.

Читайте также:  Linux list all usb device will

In the program above, the ping() function takes a list of servers and returns the output of each running ping command on each server. If a server is unreachable, it displays an output saying “ping: somethingthatdoesntexist: Name or service not known”.

The Popen() is a constructor method of the Popen class and takes in the following arguments:

  • A list of commands and any additional options these commands might require. For example, the ls command can be used with ‘-l’ option. To execute the ls -l command, the argument list would look like this: [‘ls’, ‘-l’] . The commands are specified as strings. In the example above, we use the ping command with the option -c 1 so that it only sends one packet of data, and the server replies with a single packet . Without this limit, the command would run forever until an external process stops it.
  • The stdout argument is optional and can be used to set where you want the subprocess to display the output. By default, the output is sent to the terminal. However, if you don’t want to dump a large output onto the terminal, you can use subprocess.PIPE to send the output of one command to the next. This corresponds to the | option in Linux.
  • The stderr argument is also optional and is used to set where you want the errors to be displayed. By default, it sends the errors to the terminal. Since we need to get a list of servers that cannot be reached, we don’t need to change this. The servers that cannot be reached (error) will be displayed to us on the terminal.

The output of the command is stored in a variable called temp . The communicate() function allows us to read the output and the str function can be used to convert it to a string. Once we get the output, we can parse it to extract only the essential details or just display it as it is. In this example, I am storing the output in a list for future use.

Conclusion

In conclusion, automation is one of the hottest topics in the industry, and almost every company is investing huge amounts of money to automate various manual tasks. In this tutorial, we explored the process of automatically running and analyzing Linux commands on multiple hosts using Python.

An old way of doing this is by using shell scripts. However, using Python gives developers more power and control over the execution and output of the commands. Now that you have understood the basics of executing Linux commands, you can go ahead and experiment with different commands and build more complex and robust applications.

Источник

Оцените статью
Adblock
detector