pycon 2008: python command-line tools *nix

17
Creating Agile Unix Command Line Tools With Python (Using Subprocess, Optparse, Doctest and more) Noah Gift PyCon 2008

Upload: noah-gift

Post on 10-May-2015

2.343 views

Category:

Documents


9 download

DESCRIPTION

Writing Unix Command-line tools in Python

TRANSCRIPT

Page 1: Pycon 2008:  Python Command-line Tools *Nix

Creating Agile Unix Command Line Tools With Python

(Using Subprocess, Optparse, Doctest and more)

Noah Gift

PyCon 2008

Page 2: Pycon 2008:  Python Command-line Tools *Nix

QUICK WARNING!!!

PLEASE DO NOT SATURATE THE PYCON

NETWORK WITH ICMP REQUESTS

WAIT UNTIL YOU GET HOME AND DO IT ON

YOUR NEIGHBOR’S OPEN WIRELESS ACCESS

POINT

Page 3: Pycon 2008:  Python Command-line Tools *Nix

Python Standard Library:

A command line tool’s dream come true

Optparse: Option Handling

Subprocess: Elegant, One Stop, System Call API

ConfigParser: Read/Write config files

Threading/Queue: Concurrency

Generators: http://www.dabeaz.com/generators/Generators.pdf

Doctest: Example Driven Testing

Python Egg’s w/Entry Point: Simple way to install scripts

Page 4: Pycon 2008:  Python Command-line Tools *Nix

Ideas for Command Line Tools

Pure Python:

No systems calls, but do “Unix stuff”

Unix Mashups

Mix Python + System to solve new problems.

Network Tools

Wrapping

Use existing tools, change the API, or behavior

Page 5: Pycon 2008:  Python Command-line Tools *Nix

The Basics: Subprocess + Optparse

Subprocess and Optparse:

Just like chocolate and peanut butter.

Alone they are good, but together they make a

tasty treat.

Page 6: Pycon 2008:  Python Command-line Tools *Nix

Subprocess: Tips for lazy people

If you don’t care about stdout, use subprocess.call

If you want to do something with stdout, use

subprocess.Popen

Page 7: Pycon 2008:  Python Command-line Tools *Nix

subprocess.call Wrap

Evaluate return code, send output to /dev/null

Page 8: Pycon 2008:  Python Command-line Tools *Nix

Optparse

Optparse is easy, all the tricky stuff is solved for you

Page 9: Pycon 2008:  Python Command-line Tools *Nix

Creating a Python/Unix Mashup ToolMulti-Threaded Ping Sweep

Wrap system ping command in subprocess

Create a queue

Spawn some threads

It is that easy

Page 10: Pycon 2008:  Python Command-line Tools *Nix

Multi-Threaded Ping Sweep

Page 11: Pycon 2008:  Python Command-line Tools *Nix

Convert It To A Command Line Tool

Page 12: Pycon 2008:  Python Command-line Tools *Nix

Adding SNMP Discovery

(another queue, some more threads)

Page 13: Pycon 2008:  Python Command-line Tools *Nix

Doctest Example

Page 14: Pycon 2008:  Python Command-line Tools *Nix

A Pure Python Tool

Scapy+Storm

+Optparse

Page 15: Pycon 2008:  Python Command-line Tools *Nix

Integrating Config Files With Your Tool

Page 16: Pycon 2008:  Python Command-line Tools *Nix

Using Setuptools To Distribute Tool

Make an egg

Create an entry point

Entry points install your script into Operating

System script directory, “for free”

Create a --upgrade flag and integrate upgrades into

your tool by using the Python Package Index