OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 '''This spawns a sub-shell (bash) and gives the user interactive control. The |
| 4 entire shell session is logged to a file called script.log. This behaves much |
| 5 like the classic BSD command 'script'. |
| 6 |
| 7 ./script.py [-a] [-c command] {logfilename} |
| 8 |
| 9 logfilename : This is the name of the log file. Default is script.log. |
| 10 -a : Append to log file. Default is to overwrite log file. |
| 11 -c : spawn command. Default is to spawn the sh shell. |
| 12 |
| 13 Example: |
| 14 |
| 15 This will start a bash shell and append to the log named my_session.log: |
| 16 |
| 17 ./script.py -a -c bash my_session.log |
| 18 |
| 19 PEXPECT LICENSE |
| 20 |
| 21 This license is approved by the OSI and FSF as GPL-compatible. |
| 22 http://opensource.org/licenses/isc-license.txt |
| 23 |
| 24 Copyright (c) 2012, Noah Spurrier <noah@noah.org> |
| 25 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY |
| 26 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE |
| 27 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. |
| 28 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 29 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 30 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 31 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 32 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 33 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 34 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 35 |
| 36 ''' |
| 37 |
| 38 from __future__ import print_function |
| 39 |
| 40 from __future__ import absolute_import |
| 41 |
| 42 import os, sys, time, getopt |
| 43 import signal, fcntl, termios, struct |
| 44 import pexpect |
| 45 |
| 46 global_pexpect_instance = None # Used by signal handler |
| 47 |
| 48 def exit_with_usage(): |
| 49 |
| 50 print(globals()['__doc__']) |
| 51 os._exit(1) |
| 52 |
| 53 def main(): |
| 54 |
| 55 ###################################################################### |
| 56 # Parse the options, arguments, get ready, etc. |
| 57 ###################################################################### |
| 58 try: |
| 59 optlist, args = getopt.getopt(sys.argv[1:], 'h?ac:', ['help','h','?']) |
| 60 except Exception as e: |
| 61 print(str(e)) |
| 62 exit_with_usage() |
| 63 options = dict(optlist) |
| 64 if len(args) > 1: |
| 65 exit_with_usage() |
| 66 |
| 67 if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]: |
| 68 print("Help:") |
| 69 exit_with_usage() |
| 70 |
| 71 if len(args) == 1: |
| 72 script_filename = args[0] |
| 73 else: |
| 74 script_filename = "script.log" |
| 75 if '-a' in options: |
| 76 fout = open(script_filename, "ab") |
| 77 else: |
| 78 fout = open(script_filename, "wb") |
| 79 if '-c' in options: |
| 80 command = options['-c'] |
| 81 else: |
| 82 command = "sh" |
| 83 |
| 84 # Begin log with date/time in the form CCCCyymm.hhmmss |
| 85 fout.write ('# %4d%02d%02d.%02d%02d%02d \n' % time.localtime()[:-3]) |
| 86 |
| 87 ###################################################################### |
| 88 # Start the interactive session |
| 89 ###################################################################### |
| 90 p = pexpect.spawn(command) |
| 91 p.logfile = fout |
| 92 global global_pexpect_instance |
| 93 global_pexpect_instance = p |
| 94 signal.signal(signal.SIGWINCH, sigwinch_passthrough) |
| 95 |
| 96 print("Script recording started. Type ^] (ASCII 29) to escape from the scrip
t shell.") |
| 97 p.interact(chr(29)) |
| 98 fout.close() |
| 99 return 0 |
| 100 |
| 101 def sigwinch_passthrough (sig, data): |
| 102 |
| 103 # Check for buggy platforms (see pexpect.setwinsize()). |
| 104 if 'TIOCGWINSZ' in dir(termios): |
| 105 TIOCGWINSZ = termios.TIOCGWINSZ |
| 106 else: |
| 107 TIOCGWINSZ = 1074295912 # assume |
| 108 s = struct.pack ("HHHH", 0, 0, 0, 0) |
| 109 a = struct.unpack ('HHHH', fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ , s)) |
| 110 global global_pexpect_instance |
| 111 global_pexpect_instance.setwinsize(a[0],a[1]) |
| 112 |
| 113 if __name__ == "__main__": |
| 114 main() |
OLD | NEW |