Execute shell commands in subprocess COMMAN D LIN E AUTOMATION IN - - PowerPoint PPT Presentation

execute shell commands in subprocess
SMART_READER_LITE
LIVE PREVIEW

Execute shell commands in subprocess COMMAN D LIN E AUTOMATION IN - - PowerPoint PPT Presentation

Execute shell commands in subprocess COMMAN D LIN E AUTOMATION IN P YTH ON Noah Gift Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs Using subprocess.run Simplest way to run shell commands using Python


slide-1
SLIDE 1

Execute shell commands in subprocess

COMMAN D LIN E AUTOMATION IN P YTH ON

Noah Gift

Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

slide-2
SLIDE 2

COMMAND LINE AUTOMATION IN PYTHON

Using subprocess.run

Simplest way to run shell commands using Python 3.5+ T akes a list of strings

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

slide-3
SLIDE 3

COMMAND LINE AUTOMATION IN PYTHON

Dealing with Byte Strings

Byte Strings are default in subprocess

res = b'repl 24 0.0 0.0 36072 3144 pts/0 R+ 03:15 0:00 ps aux\n' print(type(res)) bytes

Byte Strings decode

regular_string = res.decode("utf-8") 'repl 24 0.0 0.0 36072 3144 pts/0 R+ 03:15 0:00 ps aux\n' print(type(regular_string))

slide-4
SLIDE 4

COMMAND LINE AUTOMATION IN PYTHON

Unix status codes

Successful completion returns 0 ls -l echo $? Unsuccessful commands return non-zero values ls --bogus-flag echo $? 1

slide-5
SLIDE 5

COMMAND LINE AUTOMATION IN PYTHON

Checking status codes

Run shell command and assign output

  • ut = run(["ls", "-l"])

CompletedProcess object

subprocess.CompletedProcess

Check status code

print(out.returncode)

slide-6
SLIDE 6

COMMAND LINE AUTOMATION IN PYTHON

Non-zero status codes in subprocess.run

Successful status code

  • ut = run(["ls", "-l"])

print(out.returncode)

Unsuccessful status code

bad_out = run(["ls", "--turbo"]) print(bad_out.returncode) 1

slide-7
SLIDE 7

COMMAND LINE AUTOMATION IN PYTHON

Control ow for status codes

Handling user input good_user_input = "-l"

  • ut = run(["ls", good_user_input])

Controlling ow based on response

if out.returncode == 0: print("Your command was a success") else: print("Your command was unsuccesful")

slide-8
SLIDE 8

Practicing executing shell commands

COMMAN D LIN E AUTOMATION IN P YTH ON

slide-9
SLIDE 9

Capture output of shell commands

COMMAN D LIN E AUTOMATION IN P YTH ON

Noah Gift

Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

slide-10
SLIDE 10

COMMAND LINE AUTOMATION IN PYTHON

Using the subprocess.Popen module

Captures the output of shell commands In bash a directory listing using ls

bash-3.2$ ls some_file.txt some_other_file.txt

In Python output can be captured with Popen

with Popen(["ls"], stdout=PIPE) as proc:

  • ut = proc.readlines()

print(out) ['some_file.txt','some_other_file.txt']

slide-11
SLIDE 11

COMMAND LINE AUTOMATION IN PYTHON

"with" statement

Context manager handles closing le

with open("somefile.txt", "r") as output: # uses context manager with Popen(["ls", "/tmp"], stdout=PIPE) as proc: # perform file operations

Simplies using Popen Also simplies other Python statements like reading les.

slide-12
SLIDE 12

COMMAND LINE AUTOMATION IN PYTHON

Breaking down a real example

# import Popen and PIPE to manage subprocesses from subprocess import (Popen, PIPE) with Popen(["ls", "/tmp"], stdout=PIPE) as proc: result = proc.stdout.readlines()

slide-13
SLIDE 13

COMMAND LINE AUTOMATION IN PYTHON

Using communicate

communicate : A way of communicating with streams of a process, including waiting. proc = subprocess.Popen(...) # Attempt to communicate for up to 30 seconds try:

  • ut, err = proc.communicate(timeout=30)

except TimeoutExpired: # kill the process since a timeout was triggered proc.kill() # capture both standard output and standard error

  • ut, error = proc.communicate()
slide-14
SLIDE 14

COMMAND LINE AUTOMATION IN PYTHON

Using PIPE

PIPE : Connects a standard stream (stdin, stderr, stdout)

One intuition about PIPE is to think of it as tube that connect to other tubes

slide-15
SLIDE 15

COMMAND LINE AUTOMATION IN PYTHON

Required components of subprocess.Popen

stdout : Captures output of command stdout.read() : returns output as a string stdout.readlines() : returns outputs as an interator shell=False

is default and recommended

# Unsafe! with Popen("ls -l /tmp", shell=True, stdout=PIPE) as proc:

slide-16
SLIDE 16

COMMAND LINE AUTOMATION IN PYTHON

Using stderr

stderr: Captures shell stderr (error output)

with Popen(["ls", "/a/bad/path"], stdout=PIPE, stderr=PIPE) as proc: print(proc.stderr.read())

stderr output

b'ls: /a/bad/path: No such file or directory\n'

slide-17
SLIDE 17

COMMAND LINE AUTOMATION IN PYTHON

Analyzing Results

# Printing raw result print(result) [b'bar.txt\n', b'foo.txt\n'] #print each file for file in result: print(file.strip()) b'bar.txt' b'foo.txt'

slide-18
SLIDE 18

Practicing with the subprocess.Popen Class

COMMAN D LIN E AUTOMATION IN P YTH ON

slide-19
SLIDE 19

Sending input to processes

COMMAN D LIN E AUTOMATION IN P YTH ON

Noah Gift

Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

slide-20
SLIDE 20

COMMAND LINE AUTOMATION IN PYTHON

Using Unix Pipes as input

Two ways of connecting input

Popen method proc1 = Popen(["process_one.sh"], stdout=subprocess.PIPE) Popen(["process_two.sh"], stdin=proc1.stdout) run method (Higher Level Abstraction) proc1 = run(["process_one.sh"], stdout=subprocess.PIPE) run(["process_two.sh"], input=proc1.stdout)

slide-21
SLIDE 21

COMMAND LINE AUTOMATION IN PYTHON

Input Pipe from Unix

Contents of the directory

ls -l total 160

  • rw-r--r-- 1 staff staff 13 Apr 15 06:56
  • rw-r--r-- 1 staff staff 12 Apr 15 06:56 file_9.txt

Sends output of one command to another

ls | wc 20 20 220

slide-22
SLIDE 22

COMMAND LINE AUTOMATION IN PYTHON

The string language of Unix Pipes

Strings are the language of shell pipes Pass strings via STDOUT

echo "never odd or even" | rev neve ro ddo reven

slide-23
SLIDE 23

COMMAND LINE AUTOMATION IN PYTHON

Translating between objects and strings

Python objects contain data methods Unix strings are data only

  • ften columnar
slide-24
SLIDE 24

COMMAND LINE AUTOMATION IN PYTHON

User input

Bash uses read . Python uses input . Python can also accept input from command-line libraries. Subprocess can pipe input to scripts that wait for user input.

slide-25
SLIDE 25

Practicing Input

COMMAN D LIN E AUTOMATION IN P YTH ON

slide-26
SLIDE 26

Passing arguments safely to shell commands

COMMAN D LIN E AUTOMATION IN P YTH ON

Noah Gift

Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

slide-27
SLIDE 27

COMMAND LINE AUTOMATION IN PYTHON

User input is unpredictable

Expected input to a script

"/some/dir"

Actual input to a script

"/some/dir && rm -rf /all/your/dirs"

slide-28
SLIDE 28

COMMAND LINE AUTOMATION IN PYTHON

Understanding shell=True in subprocess

By default shell=False

shell=True allows arbitrary code

Best practice is to avoid shell=True

#shell=False is default run(["ls", "-l"],shell=False)

slide-29
SLIDE 29

COMMAND LINE AUTOMATION IN PYTHON

Using the shlex module

shlex can sanitize strings

shlex.split("/tmp && rm -rf /all/my/dirs")

['/tmp', '&&', 'rm', '-rf', '/all/my/dirs'] directory = shlex.split("/tmp") cmd = ["ls"] cmd.extend(directory) run(cmd, shell=True) CompletedProcess(args=['ls', '/tmp'], returncode=0)

slide-30
SLIDE 30

COMMAND LINE AUTOMATION IN PYTHON

Defaulting to items in a list

Best practice is using a list Limits mistakes with subprocess.Popen(["find", user_input, "-type", "f"], stdout=subprocess.PIPE) as find: #do something else in Python....

slide-31
SLIDE 31

COMMAND LINE AUTOMATION IN PYTHON

The problem with security by obscurity

House key under the doormat Key cards for every door Integrated security is best

slide-32
SLIDE 32

COMMAND LINE AUTOMATION IN PYTHON

Security best practices for subprocess

Always use shell=False Assume all users are malicious Never use security by obscurity Always use the principle of least privilege Reduce complexity

slide-33
SLIDE 33

Security focused practice!

COMMAN D LIN E AUTOMATION IN P YTH ON