Blairg23
Blairg23

Reputation: 12065

Is it possible to output text from a Python script to the terminal as an executable command?

To be specific, I want a Python script that accepts a string from the user and interprets that string as a command in the terminal. In other words, my script should be able to be used as follows:

python testScript.py "command -arg1 -arg2 -arg3"

And the output should be as follows:

command -arg1 -arg2 -arg3

which executes the command with 3 arguments: arg1, arg2, and arg3.

i.e.,

python testScript.py "ls -lah"

Outputs the permissions of the current directory.

Likewise,

python testScript.py "/testarea ls -lah"

Would output the permissions of the directory, "/testarea"

Any suggestions or modules?

Upvotes: 0

Views: 129

Answers (4)

Blairg23
Blairg23

Reputation: 12065

This is the best answer I came up with. I upvoted anyone who said to use the subprocess module or had a good alternative, as well.

import subprocess, threading

class Command(object):
    def __init__(self, cmd):
        self.cmd = cmd
        self.process = None

    def run(self, timeout):
        def target():
            print 'Thread started'
            self.process = subprocess.Popen(self.cmd, shell=True)
            self.process.communicate()
            print 'Thread finished'

        thread = threading.Thread(target=target)
        thread.start()

        thread.join(timeout)
        if thread.is_alive():
            print 'Terminating process'
            self.process.terminate()
            thread.join()
        print self.process.returncode

#This will run one command for 5 seconds:
command = Command("ping www.google.com")
command.run(timeout=5)

This will run the ping www.google.com command for 5 seconds and then timeout. You can add an arbitrary number of arguments to the list when you create command, separated by spaces.

This is an example of the command ls -lah:

command = Command("ls -lah")
command.run(timeout=5)

And an example of multiple commands in a single run:

command = Command("echo 'Process started'; sleep 2; echo 'Process finished'")
command.run(timeout=5)

Easy and robust, just how I like it!

Upvotes: 1

jcoppens
jcoppens

Reputation: 5440

Sure...

The most basic way is to use os:

import os, sys

os.system(sys.argv[1])

If you want do have better control over the calls, have a look at the subprocess module though. With that module you can do the same as above, but do a lot more, like capturing the output of the of the command and use it inside your program

Upvotes: 1

TheSoundDefense
TheSoundDefense

Reputation: 6945

The most robust way of doing this is to use the subprocess module. Take a look at all the possible options.

https://docs.python.org/2/library/subprocess.html

Upvotes: 1

Rodrigo Deodoro
Rodrigo Deodoro

Reputation: 1460

Running arbitrary user input can be generally considered a Bad Idea©, but if you really want to do it:

#testScript.py
import sys, os

if __name__ == "__main__":
    os.system(" ".join(sys.argv[1:]))

Upvotes: 1

Related Questions