From: Eric S. Raymond Date: Sat, 23 Nov 2002 06:57:50 +0000 (+0000) Subject: Added -d option. X-Git-Tag: 1-1~7 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=2b0a5462a0815b27df939e7d42696affbdbd31d7;p=ssh-installkeys.git Added -d option. --- diff --git a/ssh-installkeys b/ssh-installkeys index 5b261d0..ec11e5c 100755 --- a/ssh-installkeys +++ b/ssh-installkeys @@ -11,6 +11,7 @@ Options: -h Print usage summary and exit -l login Specify login name to be used at the remote site (default '%s') -c Check local and remote configuration only, dn't try to fix problems. +-d Delete remote .ssh configuration. -v Report all commands sent to host and output received. """ @@ -46,8 +47,8 @@ License: Python Software Foundation License Noah Spurrier -$Revision: 1.8 $ -$Date: 2002/11/23 02:20:19 $ +$Revision: 1.9 $ +$Date: 2002/11/23 04:29:43 $ """ import os, sys import select @@ -68,7 +69,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.8 $' +__revision__ = '$Revision: 1.9 $' __all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', '__version__', '__revision__'] @@ -596,23 +597,18 @@ def split_command_line(command_line): # ############################################################################### -def die(msg): - print msg - print "Goodbye." - raise SystemExit, 1 - class ssh_session: "Session with extra state including the password to be used." - def __init__(self, user, host, verbose=0): + def __init__(self, user, host, password=None, verbose=0): self.user = user self.host = host self.verbose = verbose - self.password = None - def remote(self, command): + self.password = password + def __exec(self, command): "Execute a command on the remote host. Return the output." child = spawn(command) if self.verbose: - print "-> " + command + sys.stderr.write("-> " + command + "\n") seen = child.expect(['assword:', EOF]) if seen == 0: if not self.password: @@ -620,41 +616,49 @@ class ssh_session: child.sendline(self.password) seen = child.expect(EOF) if self.verbose: - print "<- " + child.before + sys.stderr.write("<- " + child.before + "\n") return child.before def ssh(self, command): - return self.remote("ssh -l %s %s \"%s\""%(self.user,self.host,command)) + return self.__exec("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)) + return self.__exec("scp %s %s@%s:%s" % (src, session.user, session.host, dst)) def exists(self, file): "Retrieve file permissions of specified remote file." - seen = session.ssh("/bin/ls -ld %s" % file) + seen = self.ssh("/bin/ls -ld %s" % file) if string.find(seen, "No such file") > -1: return None # File doesn't exist else: return seen.split()[0] # Return permission field of listing. - def fixperms(self, file, checkonly=None): - "Force perms of remote file to user read on, group/other write off." - perms = session.exists(file) - print "Remote %s permissions are %s" % (file, perms) - if not (perms[1] == 'r' and perms[5] == '-' and perms[8] == '-'): - print "User read should be on, Group and Other write permissions should be off." - if not checkonly: - print "Correcting permissions..." - 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.exists(file) - if perms is None: - print "Remote %s doesn't exist." % file - if check: - die("Bailing out...") - else: - print "Creating remote %s" % file - perms = session.exists(file) + +def die(msg): + print msg + print "Goodbye." + raise SystemExit, 1 + +def fixperms(session, file, checkonly=None): + "Force perms of remote file to user read on, group/other write off." + perms = session.exists(file) + print "Remote %s permissions are %s" % (file, perms) + if not (perms[1] == 'r' and perms[5] == '-' and perms[8] == '-'): + print "User read should be on, Group and Other write permissions should be off." + if not checkonly: + print "Correcting permissions..." + session.ssh("/bin/chmod u+r,g-w,o-w %s" % file) + +def forcefile(session, file, command): + "Create file with command if it does not already exist." + perms = session.exists(file) + if perms is None: + print "Remote %s doesn't exist." % file + if check: + die("Bailing out...") else: - print file, "already exists." - return perms + print "Creating remote %s" % file + session.ssh(command) + perms = session.exists(file) + else: + print file, "already exists." + return perms if __name__ == '__main__': # Process options @@ -675,6 +679,9 @@ if __name__ == '__main__': for (switch, val) in options: if (switch == '-c'): check = 1 + elif (switch == 'd') + os.system('ssh -l esr %s "rm -fr .ssh"' % host) + raise SystemExit, 0 elif (switch == '-h'): print __doc__ % login raise SystemExit, 0 @@ -738,12 +745,12 @@ if __name__ == '__main__': # Get session to remote host. print "Starting session to %s..." % host - session = ssh_session(login, host, verbose) + session = ssh_session(login, host, verbose=verbose) # Permissions checks - session.fixperms("~", check) - session.forcefile("~/.ssh", "mkdir ~/.ssh") - session.fixperms("~/.ssh", check) + fixperms(session, "~", check) + forcefile(session, "~/.ssh", "mkdir ~/.ssh") + fixperms(session, "~/.ssh", check) # OK, install keys remotely if they don't exist if not session.exists("~/.ssh/authorized_keys"):