From caa009665f607b4bc0ad1b3f0cfab8ebc76ee37d Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Sat, 23 Nov 2002 02:20:19 +0000 Subject: [PATCH] This looks good. More testing needed. --- ssh-installkeys | 58 ++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/ssh-installkeys b/ssh-installkeys index 1a4470a..581bed6 100755 --- a/ssh-installkeys +++ b/ssh-installkeys @@ -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 -- 2.43.0