]> thyrsus.com Git - ssh-installkeys.git/commitdiff
Added -d option.
authorEric S. Raymond <esr@thyrsus.com>
Sat, 23 Nov 2002 06:57:50 +0000 (06:57 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Sat, 23 Nov 2002 06:57:50 +0000 (06:57 +0000)
ssh-installkeys

index 5b261d0cfbf7bc4f5ce52ae38a010662a3cc1e57..ec11e5c114316f861f2efff4f84c3894efc1ee2e 100755 (executable)
@@ -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"):