Skip to content

Instantly share code, notes, and snippets.

@jasonkeene
Last active April 21, 2024 20:09
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 30 You must be signed in to fork a gist
  • Save jasonkeene/2ac5e552f2ba75d0295b to your computer and use it in GitHub Desktop.
Save jasonkeene/2ac5e552f2ba75d0295b to your computer and use it in GitHub Desktop.
Get started with writing your own python scripts to automate system tasks.

Python for System Admins / Operators

Installing Python

Most Unix/Linux systems come with python pre-installed:

$ python -V
Python 2.7.6

If not you can install it.

Debian

$ apt-get install python2.7

Centos/RHEL
iuscommunity.org
Extra Packages for Enterprise Linux
compile from source
yum depends on system python so don't mess with it
Mac OSX
$ port install python27
Binary installation from python.org
Windows
Binary installation from python.org

What version should you use? 2.6, 2.7, 3.4

Installing pip

pip is python's package manager, you also may have easy_install.

Debian

$ apt-get install python-pip

Centos/RHEL

pip should be in base install (according to fritz)

Mac OSX

pip should be included with binary package

Windows

pip should be included with binary package

Once you have pip installed you can install/remove packages from the system:

$ pip install requests  # install the package requests
$ pip install requests==2.2.1  # install a specific version of requests
$ pip uninstall requests  # remove requests
$ pip freeze  # shows currently installed packages
$ pip search <string>  # search pypi.python.org for packages

Reference Material

Discussion Outline

  • Audience experience with python/scripting?
  • My level of experience.
  • Why Python?
    • Python was designed to be a middle layer between the shell and systems programming
    • Fast prototyping/quick turn around/no compilation
  • Language overview
  • Pragmatic Stuff

Language Overview

  • Interactive Interpreter
    • Read/eval/print loop:

      $ python
      >>> print 5 + 5
      10
      >>> ^D
    • similar to a shell environment:

      $ bash
      $ expr 5 + 5
      10
      $ ^D
  • Writing and Running Scripts/Modules
    • using python binary:

      $ vim script.py
      
      print 1 + 1
      name = "Jason"
      print "Hello", name
      
      $ python script.py
      $ python -i script.py
    • hashbang + execution bit:

      $ vim script.py
      
      #!/usr/bin/env python
      
      $ chmod +x script.py
      $ ./script.py
    • importing modules:

      $ python
      >>> import script
      >>> script.name
    • running modules:

      python -m script
      python -m SimpleHTTPServer
  • Variables
    • Case sensitive
    • Variables are simply labels that point to objects
    • Variables do not have types, objects have types
    • Types are not coerced:

      >>> 1 + "123"
      >>> str(1) + "123"
      >>> 1 + int("123")
  • Builtin Types
    • numeric
      • int (different forms of literals)
      • bool
      • float
      • complex (imaginary)
    • strings
      • str
      • unicode
    • collections
      • list
      • dict
      • tuple
      • set
      • indexing and slicing notation
      • comprehensions
  • Arithmetic operators
    • + / - * **
  • Whitespace
  • Conditionals / Logic operators
    • and or not == is in
  • Loops
  • Functions
    • Definition
    • Calling
    • First class
    • lambda
    • script boilerplate:

      if __name__ == '__main__':
          main()
  • Builtin Functions
    • dir/help/vars
    • len/min/max/sum
    • range/enumerate
    • locals/globals
    • eval/input/raw_input
    • repr/str/unicode
    • shadowing
  • Package/Module/Statement/Expression
  • The standard library

Pragmatic Stuff

  • Working with files :

    f = open('myfile.txt', 'w')  # open for writing
    f = open('myfile.txt')  # open for reading
    
    f.read()  # read the entire file
    f.readlines()  # read the file line by line
    # iterate over the file line by line
    for line in f:
        line
    
    f.write('new data data')
    
    f.close()
    
    with open('myfile.txt', 'w') as f:
        f.write("data")
  • Shelling out
  • stdin/stdout/stderr:

    $ vim stdin_test.py
    
        import sys
        data = sys.stdin.read()
        print data * 4
    
    $ echo "this is test data" | python stdin_test.py
  • regex
  • screen scraping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment