changeset 42:59517f60826d

quiet working, -d to use ssh -v
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Tue, 04 Jan 2022 10:42:06 +0000
parents 900ff99ec749
children f67f9ea191b7
files newkey.py
diffstat 1 files changed, 16 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/newkey.py	Mon Jan 03 19:26:11 2022 +0000
+++ b/newkey.py	Tue Jan 04 10:42:06 2022 +0000
@@ -1,8 +1,9 @@
 #!/usr/bin/python3
 '''Install a new ssh key on paul and in a list of hosts,
 confirm success, and if so comment out any older versions
-Usage: newkey.py [-v intermediary] keytype [-i] [-c client | client list filename]
+Usage: newkey.py [-d] [-v intermediary] keytype [-i] [-c client | client list filename]
 client list filename defaults to /etc/sshclients _on Paul_
+If -d, add -v to ssh
 If -i, read tar with new key pair from stdin, otherwise from ~/.ssh/id_$keytype.pub
 
 If run on a machine other than paul, will try to get it to Paul to distribute:
@@ -20,14 +21,18 @@
 def ssh_copy_id(keyfile,client):
    print(["ssh-copy-id","-i",keyfile,client],'...',sep='',end='')
    res=run(["ssh-copy-id","-i",keyfile,client],env=hack,text=True,
-	   stdout=PIPE,stderr=PIPE)
+	   capture_output=True)
    if res.returncode:
       print('failed %s'%res.returncode)
-      outs,errs=res.communicate()
-      print(errs)
+      print(res.stderr,res.stdout)
    else:
       print('succeeded')
 
+debug=False
+if sys.argv[1]=='-d':
+   sys.argv.pop(1)
+   debug=True
+
 ihost=None
 if sys.argv[1]=='-v':
    sys.argv.pop(1)
@@ -65,19 +70,22 @@
 	    file=sys.stderr)
    cmd="/home/ht/bin/newkey.py %s -i %s"%(keytype,
 					     '%s'%clients if clients is not None else '')
-   print(cmd)
-   res=os.system("mkdir -p /tmp/keys ; cd /tmp ; bash -c 'cp -a %s keys' ; tar -czf keys.tar.gz keys"%os.path.expanduser("~/.ssh/id_%s{,.pub}"%keytype))
+   res=os.system("mkdir -p /tmp/keys ; cd /tmp ; bash -c 'cp -aH %s keys' ; tar -czf keys.tar.gz keys"%os.path.expanduser("~/.ssh/id_%s{,.pub}"%keytype))
    if res:
       print("tar failed",res)
       exit(res)
    with open("/tmp/keys.tar.gz","rb") as keytar:
       if ihost is None:
-         res=run(['ssh','paul',cmd],stdin=keytar).returncode
+         res=run((['ssh','-v','paul',cmd] if debug else ['ssh','paul',cmd]),
+		 stdin=keytar).returncode
          if res:
             print("paul failed",res)
          exit(res)
       else:
-         res=run(['ssh',ihost,'/afs/inf.ed.ac.uk/user/h/ht/share/bin/goHome',cmd],stdin=keytar).returncode
+         res=run(['ssh',ihost,
+		  '/afs/inf.ed.ac.uk/user/h/ht/share/bin/goHome %s'%(
+		     '-v' if debug else ''),
+		  cmd],stdin=keytar).returncode
          if res:
             print("going via %s failed"%ihost,res)
          exit(res)