summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornoah <noah@656d521f-e311-0410-88e0-e7920216d269>2007-01-23 17:12:08 +0000
committernoah <noah@656d521f-e311-0410-88e0-e7920216d269>2007-01-23 17:12:08 +0000
commit27a40623ca1a7ea6877d9c209ad788e8ce674513 (patch)
tree7c8dc85fd2722cb33a055fcf04e2b88967ce061c
parentaebd6c69eacf21434696524e18d95768fa2a567e (diff)
downloadpexpect-27a40623ca1a7ea6877d9c209ad788e8ce674513.tar.gz
check point. added CWD and fixing hive.py.
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@447 656d521f-e311-0410-88e0-e7920216d269
-rw-r--r--pexpect/LICENSE2
-rw-r--r--pexpect/README2
-rwxr-xr-xpexpect/examples/hive.py105
-rwxr-xr-xpexpect/examples/rippy.py2
-rwxr-xr-x[-rw-r--r--]pexpect/examples/ssh_session.py0
-rwxr-xr-x[-rw-r--r--]pexpect/examples/ssh_tunnel.py0
-rwxr-xr-x[-rw-r--r--]pexpect/examples/uptime.py0
-rw-r--r--pexpect/pexpect.py19
-rw-r--r--pexpect/pxssh.py4
-rwxr-xr-xpexpect/tests/test_misc.py10
-rwxr-xr-xpexpect/tests/test_run.py12
-rwxr-xr-xpexpect/tools/testall.py4
12 files changed, 97 insertions, 63 deletions
diff --git a/pexpect/LICENSE b/pexpect/LICENSE
index 27bb809..3a98e21 100644
--- a/pexpect/LICENSE
+++ b/pexpect/LICENSE
@@ -1,5 +1,5 @@
Free, open source, and all that good stuff.
-Pexpect Copyright (c) 2006 Noah Spurrier
+Pexpect Copyright (c) 2007 Noah Spurrier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
diff --git a/pexpect/README b/pexpect/README
index 7271c0d..4f55af6 100644
--- a/pexpect/README
+++ b/pexpect/README
@@ -17,7 +17,7 @@ extensions to be compiled. It should work on any platform that supports the
standard Python pty module. The Pexpect interface was designed to be easy to use.
Free, open source, and all that good stuff.
-Pexpect Copyright (c) 2006 Noah Spurrier
+Pexpect Copyright (c) 2007 Noah Spurrier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
diff --git a/pexpect/examples/hive.py b/pexpect/examples/hive.py
index 319f1b8..51e7b81 100755
--- a/pexpect/examples/hive.py
+++ b/pexpect/examples/hive.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-"""hivesh -- Hive Shell
+"""hive -- Hive Shell
This lets you ssh to a group of servers and control them as if they were one.
Each command you enter is sent to each host in parallel. The response of each
@@ -8,11 +8,11 @@ each host to return the shell command line prompt. The shell prompt is used
to synch output.
Example:
- $ ./hivesh.py --samepw host1.example.com host2.example.net
+ $ hive.py --sameauth host1.example.com host2.example.net
username: myusername
password:
connecting to host1.example.com - OK
- connecting to host2.example.net- OK
+ connecting to host2.example.net - OK
targetting hosts: 192.168.1.104 192.168.1.107
CMD (? for help) > uptime
=======================================================================
@@ -27,15 +27,30 @@ Example:
23:53:02 up 1 day, 13:36, 2 users, load average: 0.50, 0.40, 0.46
=======================================================================
-Usage:
- hivesh.py host1 host2 host3 ... hostN
-You will be asked for your username and password.
-It is assumed that these will be the same for all hosts
-or that you have key pairs registered for each host.
+Other Usage Examples:
+1. You will be asked for your username and password for each host.
+ hive.py host1 host2 host3 ... hostN
+2. You will be asked once for your username and password.
+ This will be used for each host.
+ hive.py --sameauth host1 host2 host3 ... hostN
+3. Give a username and password on the command-line:
+ hive.py user1:pass2@host1 user2:pass2@host2 ... userN:passN@hostN
- --samepw : This flag tells Hive that you want to be prompted just once
- for your username and password and to use these credentials
- for each host.
+You can use an extended host notation to specify username, password, and host
+instead of entering auth information interactively. Where you would enter
+a host name use this format:
+ username:password@host
+This assumes that ':' is not part of the password.
+You can use '\\:' to indicate a ':' and '\\\\' to indicate a single '\\'.
+Remember that this information will appear in the process listing.
+Anyone on your machine can see this auth information. This is not secure.
+
+ --sameauth : This flag tells Hive that you want to be prompted just once
+ for a username and password and to use this authentication
+ information for each host.
+ --sameuser : This flag tell Hive that you want to use the same username
+ for each host, but you will still be prompted for a
+ different password for each host.
$Id $
Noah Spurrier
@@ -99,36 +114,9 @@ CMD_HELP="""Hive commands are preceded by a colon : (just think of vi).
This will exit the hive shell.
"""
-def main ():
- global CMD_HELP
-
- try:
- optlist, args = getopt.getopt(sys.argv[1:], 'h?', ['help','h','?','sameuser','samepw', 'username=','password='])
- except Exception, e:
- print str(e)
- exit_with_usage()
- options = dict(optlist)
- # There are a million ways to cry for help. These are but a few of them.
- if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
- exit_with_usage(0)
-
- if '--sameuser' in options:
- sameuser = True
- else:
- sameuser = False
- if '--samepw' in options:
- samepw = True
- else:
- samepw = False
- cli_username = ''
- if '--username' in options:
- cli_username = options['--username']
- cli_password = ''
- if '--password' in options:
- cli_password = options['--password']
-
- hive = {}
+def login (args):
hive_names = []
+ hive = {}
for host_connect_string in args:
hcd = parse_host_connect_string (host_connect_string)
hostname = hcd['hostname']
@@ -141,10 +129,10 @@ def main ():
username = raw_input('username: ')
else:
username = cli_username
- if len(hcd['password']) > 0 and not samepw:
+ if len(hcd['password']) > 0 and not sameauth:
password = hcd['password']
elif cli_password == '':
- password = raw_input('password: ')
+ password = getpass.getpass('password: ')
else:
password = cli_password
print 'connecting to', hostname
@@ -160,6 +148,39 @@ def main ():
print 'Skipping', hostname
if hostname in hive:
del hive[hostname]
+ return (hive_names, hive)
+
+def main ():
+ global CMD_HELP
+
+ try:
+ optlist, args = getopt.getopt(sys.argv[1:], 'h?', ['help','h','?','sameuser','sameauth', 'username=','password='])
+ except Exception, e:
+ print str(e)
+ exit_with_usage()
+ options = dict(optlist)
+ # There are a million ways to cry for help. These are but a few of them.
+ if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
+ exit_with_usage(0)
+ if len(options)==0 and len(args)==0:
+ exit_with_usage(0)
+
+ if '--sameuser' in options:
+ sameuser = True
+ else:
+ sameuser = False
+ if '--sameauth' in options:
+ sameauth = True
+ else:
+ sameauth = False
+ cli_username = ''
+ if '--username' in options:
+ cli_username = options['--username']
+ cli_password = ''
+ if '--password' in options:
+ cli_password = options['--password']
+
+ (hive_names, hive) = login(args)
synchronous_mode = True
target_hostnames = hive_names[:]
diff --git a/pexpect/examples/rippy.py b/pexpect/examples/rippy.py
index edaabb3..9c9bdb4 100755
--- a/pexpect/examples/rippy.py
+++ b/pexpect/examples/rippy.py
@@ -52,7 +52,7 @@ import sys, os, re, math, stat, getopt, traceback, types, time
import pexpect
__version__ = '1.2'
-__revision__ = '$Revision$'
+__revision__ = '$Revision: 402 $'
__all__ = ['main', __version__, __revision__]
GLOBAL_LOGFILE_NAME = "rippy_%d.log" % os.getpid()
diff --git a/pexpect/examples/ssh_session.py b/pexpect/examples/ssh_session.py
index 6ff04aa..6ff04aa 100644..100755
--- a/pexpect/examples/ssh_session.py
+++ b/pexpect/examples/ssh_session.py
diff --git a/pexpect/examples/ssh_tunnel.py b/pexpect/examples/ssh_tunnel.py
index f665d01..f665d01 100644..100755
--- a/pexpect/examples/ssh_tunnel.py
+++ b/pexpect/examples/ssh_tunnel.py
diff --git a/pexpect/examples/uptime.py b/pexpect/examples/uptime.py
index 173e319..173e319 100644..100755
--- a/pexpect/examples/uptime.py
+++ b/pexpect/examples/uptime.py
diff --git a/pexpect/pexpect.py b/pexpect/pexpect.py
index 4e85b4e..ad0d0db 100644
--- a/pexpect/pexpect.py
+++ b/pexpect/pexpect.py
@@ -33,7 +33,7 @@ Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett, Robert Stone,
Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids vander Molen,
George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin,
Geoffrey Marshall, Francisco Lourenco, Glen Mabey, Karthik Gurusamy,
-Fernando Perez
+Fernando Perez, Corey Minyard
(Let me know if I forgot anyone.)
Free, open source, and all that good stuff.
@@ -56,7 +56,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-Pexpect Copyright (c) 2006 Noah Spurrier
+Pexpect Copyright (c) 2007 Noah Spurrier
http://pexpect.sourceforge.net/
$Id$
@@ -125,7 +125,7 @@ class TIMEOUT(ExceptionPexpect):
##class MAXBUFFER(ExceptionPexpect):
## """Raised when a scan buffer fills before matching an expected pattern."""
-def run (command, timeout=-1, withexitstatus=False, events=None, extra_args=None, logfile=None):
+def run (command, timeout=-1, withexitstatus=False, events=None, extra_args=None, logfile=None, cwd=None, env=None):
"""This function runs the given command; waits for it to finish;
then returns all output as a string. STDERR is included in output.
If the full path to the command is not given then the path is searched.
@@ -186,9 +186,9 @@ def run (command, timeout=-1, withexitstatus=False, events=None, extra_args=None
a callback function through run() through the locals dictionary passed to a callback.
"""
if timeout == -1:
- child = spawn(command, maxread=2000, logfile=logfile)
+ child = spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env)
else:
- child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile)
+ child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile, cwd=cwd, env=env)
if events is not None:
patterns = events.keys()
responses = events.values()
@@ -234,7 +234,7 @@ class spawn (object):
Use this class to start and control child applications.
"""
- def __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None, env=None):
+ def __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None, cwd=None, env=None):
"""This is the constructor. The command parameter may be a string
that includes a command and any arguments to the command. For example:
child = pexpect.spawn ('/usr/bin/ftp')
@@ -358,10 +358,11 @@ class spawn (object):
self.name = '<' + repr(self) + '>' # File-like object.
self.encoding = None # File-like object.
self.closed = True # File-like object.
+ self.cwd = cwd
self.env = env
- self.__irix_hack = sys.platform.lower().find('irix') >= 0 # This flags if we are running on irix
+ self.__irix_hack = (sys.platform.lower().find('irix')>=0) # This flags if we are running on irix
# Solaris uses internal __fork_pty(). All others use pty.fork().
- if sys.platform.lower().find('solaris') or sys.platform.lower().find('sunos5'):
+ if (sys.platform.lower().find('solaris')>=0) or (sys.platform.lower().find('sunos5')>=0):
self.use_native_pty_fork = False
else:
self.use_native_pty_fork = True
@@ -497,6 +498,8 @@ class spawn (object):
# (specifically, Tomcat).
signal.signal(signal.SIGHUP, signal.SIG_IGN)
+ if self.cwd is not None:
+ os.chdir(self.cwd)
if self.env is None:
os.execv(self.command, self.args)
else:
diff --git a/pexpect/pxssh.py b/pexpect/pxssh.py
index ce22736..3fce9c7 100644
--- a/pexpect/pxssh.py
+++ b/pexpect/pxssh.py
@@ -52,8 +52,8 @@ class pxssh (spawn):
# Python's super is super annoying.
# http://mail.python.org/pipermail/python-list/2006-February/325485.html
- def __init__ (self, timeout=30, maxread=2000, searchwindowsize=None, logfile=None, env=None):
- spawn.__init__(self, None, timeout=timeout, maxread=maxread, searchwindowsize=searchwindowsize, logfile=logfile, env=env)
+ def __init__ (self, timeout=30, maxread=2000, searchwindowsize=None, logfile=None, cwd=None, env=None):
+ spawn.__init__(self, None, timeout=timeout, maxread=maxread, searchwindowsize=searchwindowsize, logfile=logfile, cwd=cwd, env=env)
self.name = '<pxssh>'
#super(spawn, self).__init__()
diff --git a/pexpect/tests/test_misc.py b/pexpect/tests/test_misc.py
index 0e54c5c..64a5bb3 100755
--- a/pexpect/tests/test_misc.py
+++ b/pexpect/tests/test_misc.py
@@ -100,6 +100,16 @@ class TestCaseMisc(PexpectTestCase.PexpectTestCase):
child = pexpect.spawn('cat')
child.setwinsize(10,13)
assert child.getwinsize()==(10,13), "getwinsize() did not return (10,13)"
+ def test_env(self):
+ default = pexpect.run('env')
+ userenv = pexpect.run('env', env={'foo':'pexpect'})
+ assert default!=userenv, "'default' and 'userenv' should be different"
+ assert 'foo' in userenv and 'pexpect' in userenv, "'foo' and 'pexpect' should be in 'userenv'"
+ def test_cwd (self): # This assumes 'pwd' and '/tmp' exist on this platform.
+ default = pexpect.run('pwd')
+ tmpdir = pexpect.run('pwd', cwd='/tmp')
+ assert default!=tmpdir, "'default' and 'tmpdir' should be different"
+ assert ('tmp' in tmpdir), "'tmp' should be returned by 'pwd' command"
def test_which (self):
p = os.defpath
ep = os.environ['PATH']
diff --git a/pexpect/tests/test_run.py b/pexpect/tests/test_run.py
index 575bccf..b238599 100755
--- a/pexpect/tests/test_run.py
+++ b/pexpect/tests/test_run.py
@@ -5,13 +5,13 @@ import commands
import sys
import PexpectTestCase
-# Many of these test cases blindly assume that sequential
-# listing of the /bin directory will yield the same results.
-# This may not always be true, but seems adequate for testing now.
-# I should fix this at some point.
+# TODO Many of these test cases blindly assume that sequential
+# TODO listing of the /bin directory will yield the same results.
+# TODO This may not always be true, but seems adequate for testing for now.
+# TODO I should fix this at some point.
def timeout_callback (d):
- print d["event_count"],
+# print d["event_count"],
if d["event_count"]>5:
return 1
return 0
@@ -28,7 +28,7 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase):
assert the_old_way == the_new_way
assert exitstatus == 0
- def test_run_callback (self):
+ def test_run_callback (self): # TODO it seems like this test could block forever if run fails...
pexpect.run("cat", timeout=1, events={pexpect.TIMEOUT:timeout_callback})
def test_run_bad_exitstatus (self):
diff --git a/pexpect/tools/testall.py b/pexpect/tools/testall.py
index 58037ea..68b2a2b 100755
--- a/pexpect/tools/testall.py
+++ b/pexpect/tools/testall.py
@@ -15,8 +15,8 @@ import os, os.path
import sys
import pexpect
-print pexpect.__version__,
-print pexpect.__revision__
+print "Testing pexpect version:", pexpect.__version__
+print "Testing pexpect revision:", pexpect.__revision__
def add_tests_to_list (import_list, dirname, names):
# Only check directories named 'tests'.