diff options
author | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2007-11-28 23:40:34 +0000 |
---|---|---|
committer | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2007-11-28 23:40:34 +0000 |
commit | eb46aa8d11978f102dbb88ca6ba7d9410dc7a37c (patch) | |
tree | 204c6d0109ae2c6e06bec5785eb82d1c74e20b05 /pexpect | |
parent | 1947b3f73dce75c3d298b67fd2445ca937af0a91 (diff) | |
download | pexpect-eb46aa8d11978f102dbb88ca6ba7d9410dc7a37c.tar.gz |
Updated examples with a cleaner script style and doc strings.
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@489 656d521f-e311-0410-88e0-e7920216d269
Diffstat (limited to 'pexpect')
-rwxr-xr-x | pexpect/examples/hive.py | 6 | ||||
-rwxr-xr-x | pexpect/examples/script.py | 20 | ||||
-rwxr-xr-x | pexpect/examples/sshls.py | 45 | ||||
-rwxr-xr-x | pexpect/examples/topip.py | 4 | ||||
-rwxr-xr-x | pexpect/examples/uptime.py | 8 |
5 files changed, 59 insertions, 24 deletions
diff --git a/pexpect/examples/hive.py b/pexpect/examples/hive.py index 0ab1837..f6fde60 100755 --- a/pexpect/examples/hive.py +++ b/pexpect/examples/hive.py @@ -1,4 +1,5 @@ #!/usr/bin/env python + """hive -- Hive Shell This lets you ssh to a group of servers and control them as if they were one. @@ -58,8 +59,9 @@ this auth information. This is not secure. --username=: This sets the username for all hosts. This implies --sameuser. --password=: This sets the password for all hosts. This implies --password. -$Id$ Noah Spurrier + +$Id$ """ # TODO add feature to support username:password@host combination @@ -350,6 +352,8 @@ if __name__ == "__main__": print time.asctime() print "TOTAL TIME IN MINUTES:", print (time.time() - start_time) / 60.0 + except SystemExit, e: + raise e except Exception, e: tb_dump = traceback.format_exc() print "==========================================================================" diff --git a/pexpect/examples/script.py b/pexpect/examples/script.py index 6df169a..908b912 100755 --- a/pexpect/examples/script.py +++ b/pexpect/examples/script.py @@ -1,18 +1,23 @@ #!/usr/bin/env python -"""script.py -This spawns a sub-shell (bash) and gives the user interactive control. -The entire shell session is logged to a file called script.log. -This behaves much like the classic BSD command "script". + +"""This spawns a sub-shell (bash) and gives the user interactive control. The +entire shell session is logged to a file called script.log. This behaves much +like the classic BSD command 'script'. ./script.py [-a] [-c command] {logfilename} + logfilename : This is the name of the log file. Default is script.log. -a : Append to log file. Default is to overwrite log file. -c : spawn command. Default is to spawn the sh shell. Example: - This will start a bash shell and append to the log named my_session.log + + This will start a bash shell and append to the log named my_session.log: + ./script.py -a -c bash my_session.log + """ + import os, sys, time, getopt import signal, fcntl, termios, struct import traceback @@ -21,10 +26,12 @@ import pexpect global_pexpect_instance = None # Used by signal handler def exit_with_usage(): + print globals()['__doc__'] os._exit(1) def main(): + ###################################################################### # Parse the options, arguments, get ready, etc. ###################################################################### @@ -72,6 +79,7 @@ def main(): return 0 def sigwinch_passthrough (sig, data): + # Check for buggy platforms (see pexpect.setwinsize()). if 'TIOCGWINSZ' in dir(termios): TIOCGWINSZ = termios.TIOCGWINSZ @@ -85,6 +93,8 @@ def sigwinch_passthrough (sig, data): if __name__ == "__main__": try: main() + except SystemExit, e: + raise e except Exception, e: print "ERROR" print str(e) diff --git a/pexpect/examples/sshls.py b/pexpect/examples/sshls.py index 6527a29..5fe7845 100755 --- a/pexpect/examples/sshls.py +++ b/pexpect/examples/sshls.py @@ -1,16 +1,22 @@ #!/usr/bin/env python -'''This runs "ls -l" on a remote host using SSH. - At the prompts enter hostname, user, and password. -''' + +"""This runs 'ls -l' on a remote host using SSH. At the prompts enter hostname, +user, and password. + +$Id$ +""" + import pexpect -import getpass +import getpass, os def ssh_command (user, host, password, command): - """This runs a command on the remote host. This returns a - pexpect.spawn object. This handles the case when you try - to connect to a new host and ssh asks you if you want to - accept the public key fingerprint and continue connecting. - """ + + """This runs a command on the remote host. This could also be done with the +pxssh class, but this demonstrates what that class does at a simpler level. +This returns a pexpect.spawn object. This handles the case when you try to +connect to a new host and ssh asks you if you want to accept the public key +fingerprint and continue connecting. """ + ssh_newkey = 'Are you sure you want to continue connecting' child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command)) i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: ']) @@ -31,11 +37,20 @@ def ssh_command (user, host, password, command): child.sendline(password) return child -host = raw_input('Hostname: ') -user = raw_input('User: ') -password = getpass.getpass('Password: ') +def main (): + + host = raw_input('Hostname: ') + user = raw_input('User: ') + password = getpass.getpass('Password: ') + child = ssh_command (user, host, password, '/bin/ls -l') + child.expect(pexpect.EOF) + print child.before -child = ssh_command (user, host, password, '/bin/ls -l') -child.expect(pexpect.EOF) -print child.before +if __name__ == '__main__': + try: + main() + except Exception, e: + print str(e) + traceback.print_exc() + os._exit(1) diff --git a/pexpect/examples/topip.py b/pexpect/examples/topip.py index ec38d32..4d5b190 100755 --- a/pexpect/examples/topip.py +++ b/pexpect/examples/topip.py @@ -55,6 +55,7 @@ TOPIP_LOG_FILE = '/var/log/topip.log' TOPIP_LAST_RUN_STATS = '/var/run/topip.last' def exit_with_usage(): + print globals()['__doc__'] os._exit(1) @@ -256,6 +257,9 @@ def main(): if __name__ == '__main__': try: main() + sys.exit(0) + except SystemExit, e: + raise e except Exception, e: print str(e) traceback.print_exc() diff --git a/pexpect/examples/uptime.py b/pexpect/examples/uptime.py index 173e319..d2c9e88 100755 --- a/pexpect/examples/uptime.py +++ b/pexpect/examples/uptime.py @@ -1,7 +1,9 @@ #!/usr/bin/env python -"""This displays uptime information using uptime. -This is redundant perhaps, but it demonstrates expecting for a -regular expression that uses subgroups. + +"""This displays uptime information using uptime. This is redundant, +but it demonstrates expecting for a regular expression that uses subgroups. + +$Id$ """ import pexpect |