DEPARTMENT OF COMPUTING

IT 3110 : Advanced System Administration

Python


Review (loops)

    for i in range(10):
      print i

    i=0
    while i<10:
      print i
      i+=1

Review (decisions)

    x=3
    if x>2:
      print ("greater")
    else:
      print ("less than")

Review command line arguments

    #!/usr/bin/python

    import sys

    if (len(sys.argv) != 2):
        print ("Not enough args")
        print sys.argv

Run the above like ./foo.py arg1 arg2 arg3


Review (loop through a file)

Here is my input file: (‘animals.txt’)

    cows
    chicken
    goats
    horses

    f = open('animals.txt')
    for line in f:
        line=line.strip()
        print(line)

Review (functions)

    def foo(arg1, arg2):
      print ("the contents of arg 1" + arg1)
      return 1

    returnvalue = foo(4,5)

Python (subprocess)

    import subprocess
    import sys
    HOST="ns2.mojojojo.ml"
    COMMAND="uname -a"

    ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
            shell=False,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)

    result = ssh.stdout.readlines()
    if result == []:
        error = ssh.stderr.readlines()
        print >>sys.stderr, "ERROR: %s" % error
    else:
        print result

Python (os)

Link

    import os

    for file in os.listdir("samples"):
        print file

Python (os)

    import os

    # where are we?
    cwd = os.getcwd()
    print ("1", cwd)

    # go down
    os.chdir("samples")
    print ("2", os.getcwd())

    # go back up
    os.chdir(os.pardir)
    print ("3", os.getcwd())

Python (os)

    import os

    os.mkdir("test")
    os.rmdir("test")

Python (os)

    import os
    import time

    file = "samples/sample.jpg"

    def dump(st):
        mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
        print ("- size:", size, "bytes")
        print ("- owner:", uid, gid)
        print ("- created:", time.ctime(ctime))
        print ("- last accessed:", time.ctime(atime))
        print ("- last modified:", time.ctime(mtime))
        print ("- mode:", oct(mode))
        print ("- inode/dev:", ino, dev)

    #
    # get stats for a filename

    st = os.stat(file)

    print ("stat", file)
    dump(st)
    print

    #
    # get stats for an open file

    fp = open(file)

    st = os.fstat(fp.fileno())

    print ("fstat", file)
    dump(st)

Python (os)

    import os

    if os.name == "nt":
        command = "dir"
    else:
        command = "ls -l"

    os.system(command)

Python (send email) (gmail will probably not like)

    import smtplib

    server = smtplib.SMTP('stumail.cs.utahtech.edu', 25)
    server.ehlo()

    msg = "I like candy"
    server.sendmail("joe@thegummibear.com", "foo@gmail.com", msg)
    server.quit()

Might also need to read some google fu documentation


Command line arguments

import argparse

parser = argparse.ArgumentParser(description=‘Need path’) parser.add_argument(‘-p’, dest=‘path’, required=True) parser.add_argument(‘-s’, dest=‘semester’, required=True) parser.add_argument(‘-c’, dest=‘course’, required=True) args = parser.parse_args() path = args.path semester = args.semester course = args.course

Last Updated 02/07/2025