1#! /usr/bin/env python 2 3# script.py -- Make typescript of terminal session. 4# Usage: 5# -a Append to typescript. 6# -p Use Python as shell. 7# Author: Steen Lumholt. 8 9 10import os, time, sys, getopt 11import pty 12 13def read(fd): 14 data = os.read(fd, 1024) 15 script.write(data) 16 return data 17 18shell = 'sh' 19filename = 'typescript' 20mode = 'w' 21if os.environ.has_key('SHELL'): 22 shell = os.environ['SHELL'] 23 24try: 25 opts, args = getopt.getopt(sys.argv[1:], 'ap') 26except getopt.error, msg: 27 print '%s: %s' % (sys.argv[0], msg) 28 sys.exit(2) 29 30for o, a in opts: 31 if o == '-a': 32 mode = 'a' 33 elif o == '-p': 34 shell = 'python' 35 36script = open(filename, mode) 37 38sys.stdout.write('Script started, file is %s\n' % filename) 39script.write('Script started on %s\n' % time.ctime(time.time())) 40pty.spawn(shell, read) 41script.write('Script done on %s\n' % time.ctime(time.time())) 42sys.stdout.write('Script done, file is %s\n' % filename) 43