Running a Linux Command from Python

Running a linux command from python

ps apparently limits its output to fit into the presumed width of the terminal. You can override this width with the $COLUMNS environment variable or with the --columns option to ps.

The commands module is deprecated. Use subprocess to get the output of ps -ef and filter the output in Python. Do not use shell=True as suggested by other answers, it is simply superfluous in this case:

ps = subprocess.Popen(['ps', '-ef', '--columns', '1000'], stdout=subprocess.PIPE)
output = ps.communicate()[0]
for line in output.splitlines():
if 'rtptransmit' in line:
print(line)

You may also want to take a look the pgrep command by which you can directly search for specific processes.

How to execute linux commands using python

The syntax for python is:

import os
os.system("command")

For bash .sh like Mac os

How do I execute a program or call a system command?

Use the subprocess module in the standard library:

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

The advantage of subprocess.run over os.system is that it is more flexible (you can get the stdout, stderr, the "real" status code, better error handling, etc...).

Even the documentation for os.system recommends using subprocess instead:

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

On Python 3.4 and earlier, use subprocess.call instead of .run:

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

Run linux command from python which needs user input

In his comment, Jean-François Fabre is right about this:

But the real issue is that piping input to a password entering routine doesn't always work.

But not about this:

You have to generate a keyboard event at a lower level.

Though it would be true in general, in the case at hand the command openssl genrsa … offers the option -passout stdin to read the password from standard input:

openssl genrsa -aes256 -out ise.key.pem -passout stdin 2048

So, this Python script works:

import subprocess
p=subprocess.Popen(["openssl","genrsa","-aes256","-out","ise.key.pem",
"-passout","stdin","2048"], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
inputdata="123456"
p.communicate(input=inputdata)

Note that one cannot call communicate() with timeout=None more than once, since it waits for process to terminate. But it isn't necessary either, because with -passout stdin the command doesn't ask for verifying, thus no need to enter the pass phrase a second time.

How to run a command as root in python?

This package requires root permissions and root access. So, it must run as root and the package must be installed as root.

sudo commands are useful for this.

sudo python selenium_script.py
will run it as root. If this alone does not solve, you must install it as root too. Navigate to your terminal and cd to working directory (or home directory). Then, run this command:

sudo pip3 install keyboard # use pip if on python 2



Related Topics



Leave a reply



Submit