]> thyrsus.com Git - ssh-installkeys.git/commitdiff
This looks good. More testing needed.
authorEric S. Raymond <esr@thyrsus.com>
Sat, 23 Nov 2002 02:20:19 +0000 (02:20 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Sat, 23 Nov 2002 02:20:19 +0000 (02:20 +0000)
ssh-installkeys

index 1a4470a9131083fb53f0afc60c048263a8c0f41c..581bed6c1a1ca3ca40ba84eeaa4fa7ebefbf1542 100755 (executable)
@@ -46,8 +46,8 @@ License: Python Software Foundation License
 
 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
@@ -68,7 +68,7 @@ A critical module was not found. Probably this OS does not support it.
 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__']
 
@@ -695,11 +695,8 @@ class ssh_session:
         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
@@ -712,9 +709,13 @@ class ssh_session:
         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:
@@ -727,7 +728,7 @@ class ssh_session:
             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)
@@ -737,7 +738,6 @@ class ssh_session:
                 die("Bailing out...")
             else:
                 print "Creating remote %s" % file
-                session.remote(command)
                 perms = session.checkfile(file)
         else:
             print file, "already exists."
@@ -814,23 +814,31 @@ if __name__ == '__main__':
             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