Noah Spurrier
-$Revision: 1.6 $
-$Date: 2002/11/22 11:40:46 $
+$Revision: 1.7 $
+$Date: 2002/11/22 22:12:43 $
"""
import os, sys
import select
Currently pexpect is intended for UNIX operating systems (including OS-X)."""
__version__ = '0.94'
-__revision__ = '$Revision: 1.6 $'
+__revision__ = '$Revision: 1.7 $'
__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn',
'__version__', '__revision__']
self.host = host
self.verbose = verbose
self.password = None
- def remote(self, command, input=None):
+ def remote(self, command):
"Execute a command on the remote host. Return the output."
- command = "ssh -l %s %s %s"% (self.user,self.host,command)
- if input:
- command = input + " | " + command
child = spawn(command)
if self.verbose:
print "-> " + command
if self.verbose:
print "<- " + child.before
return child.before
+ def ssh(self, command):
+ return self.remote("ssh -l %s %s \"%s\""%(self.user,self.host,command))
+ def scp(self, src, dst):
+ return self.remote("scp %s %s@%s:%s" % (src, session.user, session.host, dst))
def checkfile(self, file):
"Retrieve file permissions of specified remote file."
- seen = session.remote("/bin/ls -ld %s" % file)
+ seen = session.ssh("/bin/ls -ld %s" % file)
if string.find(seen, "No such file") > -1:
return None # File doesn't exist
else:
print "User read should be on, Group and Other write permissions should be off."
if not check:
print "Correcting permissions..."
- session.remote("/bin/chmod u+r,g-w,o-w %s" % file)
+ session.ssh("/bin/chmod u+r,g-w,o-w %s" % file)
def forcefile(self, file, command):
"Create file with command if it does not already exist."
perms = session.checkfile(file)
die("Bailing out...")
else:
print "Creating remote %s" % file
- session.remote(command)
perms = session.checkfile(file)
else:
print file, "already exists."
print legend + "exists and is readable."
else:
die(legend + "does not exist or is unreadable.")
- print "Local configuration looks correct."
-
- # Get session to remote host.
- print "Starting session to %s..." % host
- session = ssh_session(login, host, verbose)
-
- # Permissions checks
- session.fixperms("~")
- session.forcefile("~/.ssh", "mkdir ~/.ssh")
- session.fixperms("~/.ssh")
-
- # OK, install keys remotely
- session.forcefile("~/.ssh/authorized_keys",
- "cat >~/.ssh/authorized_keys"
- "cat " + string.join(public_keys, " "))
+ try:
+ keyfile = os.tmpnam()
+ os.system("cat " + string.join(public_keys, " ") + " >" + keyfile)
+ if verbose:
+ print "Public keys consolidated in %s" % keyfile
+
+ print "Local configuration looks correct."
+
+ # Get session to remote host.
+ print "Starting session to %s..." % host
+ session = ssh_session(login, host, verbose)
+
+ # Permissions checks
+ session.fixperms("~")
+ session.forcefile("~/.ssh", "mkdir ~/.ssh")
+ session.fixperms("~/.ssh")
+
+ # OK, install keys remotely if they don't exist
+ if not session.checkfile("~/.ssh/authorized_keys"):
+ session.scp(keyfile, "~/.ssh/authorized_keys")
+ except SystemExit:
+ pass
# It's all over now...
+ os.remove(keyfile)
print "Done."
# End