Python Scripting For System Administration: Rebeka Mukherjee
Python Scripting For System Administration: Rebeka Mukherjee
E-mail: [email protected]
• Python 2.0 was released in October 2000, with many new features
including a full garbage collector and Unicode. Python 3.0 was
released in December 2008.
>>> 2+3*5
17
>>> 2**8
256
>>> x, y = 2, ‘hello’
>>> x
2
>>> y
‘hello’
Data Types
Basic Operators
• Arithmetic operators: +, -, *, /, //, %
o + is also used for concatenation
o - is also used for set difference
o * is also used for string repetition
o // is also used for floor division
o % is also used for string formatting
• Assignment operator: =
• Comparison operator: ==
>>> 1/2
0.5
Numbers
>>> from fractions import Fraction
• You can define, modify, view, lookup, and delete the key-
value pairs in the dictionary.
>>> d={'user':'scott','pswd':12345}
>>> d['user']
'scott'
>>> d['pswd']
12345
Dictionary
>>> d['id']=45 # insert another key with value
>>> d
{'user': 'tiger', 'pswd': 12345, 'id': 45}
>>> x=1
>>> while(x<10):
print(x)
x=x+1
Iterative Statements
• For-loops:
>>> x = 5
>>> y = 9
>>> max (x, y)
9 is greater
Functions
>>> def fact(n):
if n < 1:
return 1
else:
return n * fact(n - 1)
>>> x = 5
>>> y = fact (x)
>>> print y
120
Modules
• Python’s extensive library contains built-in modules that may be
used to simplify and reduce development time.
def pop(self):
x = self.items[-1]
del self.items[-1]
return x
def empty(self):
return len(self.items) == 0
Exception
• An event occurs during the execution of program
• Disrupts normal flow of program’s instruction
• It must be handled
• Readability
Steps:
1. Get the search pattern from the user.
2. Perform the search.
3. Print a listing of files found.
4. Using the stat module, get permissions for
each file found.
5. Present the results to the user.
Python Code
import stat, sys, os, string, commands
Steps:
1. Open the tar file.
2. Present the menu and get the user selection.
3. If you press 1, the program prompts you for the file
name in the archive to extract the current directory
to and then extracts the file.
4. If you press 2, the program prompts you for the file
name and then displays the file information.
5. If you press 3, the program lists all the files in the
archive.
Python Code
import tarfile, sys
try:
#open tarfile
tar = tarfile.open(sys.argv[1], "r:tar")
Steps:
1. Get the name of a process to check and
assign it to a variable.
2. Run the ps command and assign the results
to a list.
3. Display detailed information about the
process with English terms.
Python Code
import commands, os, string
try:
#perform a ps command and assign results to a
list
output = commands.getoutput("ps -f|grep " +
program)
proginfo = string.split(output)
Python Code (contd.)
#display results
print "\n\
Full path:\t\t", proginfo[5], "\n\
Owner:\t\t\t", proginfo[0], "\n\
Process ID:\t\t", proginfo[1], "\n\
Parent process ID:\t", proginfo[2], "\n\
Time started:\t\t", proginfo[4]
except:
print "There was a problem with the
program."
Other Uses of Scripts
• Managing servers: Checks patch levels for a
particular application across a set of servers and
updates them automatically.