The document discusses the advantages of using Python for Linux system administration compared to traditional scripting languages like Bash, Perl, and Awk. It highlights Python's readability, built-in libraries, and versatility while addressing some limitations, such as less common regular expressions and indentation syntax. Additionally, it mentions practical examples and external resources for further learning, as well as the upcoming transition to Python 3.
In this document
Powered by AI
Introduction to Python for Linux system administration presented by Vern Ceder at Fort Wayne LUG.
Comparison of scripting languages for system administration. Bash, Perl, AWK are mentioned as traditional tools.
Python is highlighted for its string handling, readability, expressiveness, extensive libraries, and popularity in various platforms.
Despite being popular, regular expressions in Python are not built-in and it is less common than Perl.
Discussion on Python's use of indentation for code organization, enhancing readability.
Overview of built-in string methods in Python, including split, lower, upper, and more.
Examples of scripting tasks performed in Python, showcasing its ease compared to Bash.
Usage of the re module in Python, which offers Perl-like regular expression capabilities.
Explanation of exception handling in Python with a ZeroDivisionError example.
Python's ability to call other programs and handle input/output through modules like subprocess.
Usage of os and sys modules for file and directory operations, including os.walk for directory traversal.
Overview of handling command line arguments using sys.argv and optparse for robust input management.
Mention of other useful modules in Python for database handling, threading, and SSH connections.
Demonstration of using the python-daemon package for daemon processes in Python.
Introduction to ctypes for loading and calling functions from C libraries.
Code example for creating a basic HTTP server using Python's built-in http.server module.
Importance of Python 3 as the future of the language, noting it's not backward compatible with Python 2.
Introduction to IPython, a powerful shell with enhanced features for interactive programming.
Announcement of a new Python book covering Python 3, expected to be published soon.
Details about the PyCon 2010 event, including talks, tutorials, and activities available.
List of resources and books recommended for learning Python in the context of system administration.
Providing contact information for further questions or inquiries about the presentation.
A scripting languageshould handle input & output process text – search, replace, pattern matching, etc. traverse filesystems use system utilities and libraries (glue)
indentation yes, Python usesindentation to organize code it makes code more readable it's no weirder than {} or @$% get over it
16.
strings some built-in stringmethods split lower strip upper join isdigit replace swapcase find expandtabs count center startswith encode/decode endswith format
17.
for example to do what wc does: #!/usr/bin/env python import sys data = sys.stdin.read() chars = len(data) words = len(data.split()) lines = len(data.split('n')) print ("{0} {1} {2}".format(lines, words, chars)) doc@paladin:~/work/olf$ ./wc.py < wc.py 12 22 189
18.
or number ofoccurrences? in bash (not mine): doc@pal:~/olf$ tr " " "n" < wc.py | grep len | wc -w 3 in Python: #!/usr/bin/env python import sys data = sys.stdin.read() print data.count(sys.argv[1]) doc@paladin:~/work/olf$ ./num_occur.py len < wc.py 3
19.
regular expressions re module syntax similar to perl import re >>> re.findall("[Ll]en", "len is the Length") ['len', 'Len']
20.
exception handling y =10 try: x = y / 0 except ZeroDivisionError, e: print e integer division or modulo by zero
21.
glue multiple waysto call other programs and pipe the results sys.stdin, sys.stdout, sys.stderr os.system(), os.spawnl() subprocess.call() subprocess.Popen()
files, directories andmore the os and sys modules os.environ sys.argv os.getcwd sys.stdin os.chmod sys.stdout os.chown sys.stderr os.link sys.platform os.mkdir sys.exit os.remove os.rename
24.
Modules: os os.walk() import os >>> for x in os.walk('.'): ... print x ... ('.', ['emptydir'], [ 'chinese-python- poster.jpg', 'olf_proposal.txt', 'wc.py', 'olf.odp', 'shell.png', 'olf.txt', 'Pil.gif', 'adminscripting.png', 'num_occur.py']) ('./emptydir', [], [])
daemons python-daemon import daemon from spam import main_program with daemon.DaemonContext(): main_program
30.
ctypes load and use C libraries also works with Windows DLL's >>> from ctypes import * >>> libc = CDLL("libc.so.6") >>> libc.printf("hello %sn", "Python") hello Python 13 >>> print libc.time(None) 1253757776 >>> import datetime >>> datetime.datetime.fromtimestamp(libc.time(None)) datetime.datetime(2009, 9, 23, 22, 5, 56)
31.
A 2 lineHTTP server from http.server import HTTPServer, SimpleHTTPRequestHandler server = HTTPServer(("",8000), SimpleHTTPRequestHandler) server.serve_forever()
32.
What about Python3? it's a better language than 2.x it's not backward compatible it's supported by the developers it's the future it's not here (for sysadmins) yet
33.
ipython, the ubershell extensive history usable as a system shell http://ipython.scipy.org In [1]: print "hello" ------> print("hello") hello In [2]: ls adminscripting.png olf.odp Pil.gif
34.
nd Quick Python Book,2 ed covering Python 3 due out late this year http://www.manning.com/ceder
35.
World's largest Pythonconference Talks PyCon 2010 Open Space Tutorials Hands-On Lab Lightning Talks Feb. 17-25 Exhibit Hall Keynotes Atlanta, GA Sprints NOW with Poster sessions! us.pycon.org Photo: james.rintamaki License: Attribution- Share Alike 2.0 Generic
36.
Resources & contact info Python for Unix and Linux System Administration, Noah Gift, Jeremy M. Jones, O'Reilly Media 2008 Pro Python System Administration, Rytis Sileika, Apress, (not yet published) “Python for system administrators”, James Knowlton, IBM DeveloperWorks, 2007 http://www.ibm.com/developerworks/aix/library/au-python/ Python Cookbook, Martelli, Ravenscroft & Ascher, O'Reilly Media 2005