Mercurial > hg > python
comparison newkey.py @ 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 |
comparison
equal
deleted
inserted
replaced
41:900ff99ec749 | 42:59517f60826d |
---|---|
1 #!/usr/bin/python3 | 1 #!/usr/bin/python3 |
2 '''Install a new ssh key on paul and in a list of hosts, | 2 '''Install a new ssh key on paul and in a list of hosts, |
3 confirm success, and if so comment out any older versions | 3 confirm success, and if so comment out any older versions |
4 Usage: newkey.py [-v intermediary] keytype [-i] [-c client | client list filename] | 4 Usage: newkey.py [-d] [-v intermediary] keytype [-i] [-c client | client list filename] |
5 client list filename defaults to /etc/sshclients _on Paul_ | 5 client list filename defaults to /etc/sshclients _on Paul_ |
6 If -d, add -v to ssh | |
6 If -i, read tar with new key pair from stdin, otherwise from ~/.ssh/id_$keytype.pub | 7 If -i, read tar with new key pair from stdin, otherwise from ~/.ssh/id_$keytype.pub |
7 | 8 |
8 If run on a machine other than paul, will try to get it to Paul to distribute: | 9 If run on a machine other than paul, will try to get it to Paul to distribute: |
9 1) Using ssh-copy-id directly; | 10 1) Using ssh-copy-id directly; |
10 2) Using ssh-copy-id via intermediary (defaults to $DESKTOP) | 11 2) Using ssh-copy-id via intermediary (defaults to $DESKTOP) |
18 hack['SSH_AUTH_SOCK']='/home/ht/.gnupg/S.gpg-agent.ssh' | 19 hack['SSH_AUTH_SOCK']='/home/ht/.gnupg/S.gpg-agent.ssh' |
19 | 20 |
20 def ssh_copy_id(keyfile,client): | 21 def ssh_copy_id(keyfile,client): |
21 print(["ssh-copy-id","-i",keyfile,client],'...',sep='',end='') | 22 print(["ssh-copy-id","-i",keyfile,client],'...',sep='',end='') |
22 res=run(["ssh-copy-id","-i",keyfile,client],env=hack,text=True, | 23 res=run(["ssh-copy-id","-i",keyfile,client],env=hack,text=True, |
23 stdout=PIPE,stderr=PIPE) | 24 capture_output=True) |
24 if res.returncode: | 25 if res.returncode: |
25 print('failed %s'%res.returncode) | 26 print('failed %s'%res.returncode) |
26 outs,errs=res.communicate() | 27 print(res.stderr,res.stdout) |
27 print(errs) | |
28 else: | 28 else: |
29 print('succeeded') | 29 print('succeeded') |
30 | |
31 debug=False | |
32 if sys.argv[1]=='-d': | |
33 sys.argv.pop(1) | |
34 debug=True | |
30 | 35 |
31 ihost=None | 36 ihost=None |
32 if sys.argv[1]=='-v': | 37 if sys.argv[1]=='-v': |
33 sys.argv.pop(1) | 38 sys.argv.pop(1) |
34 ihost=sys.argv.pop(1).lower() | 39 ihost=sys.argv.pop(1).lower() |
63 if clients[0]!='-': | 68 if clients[0]!='-': |
64 print("Note, %s will be dereferenced on paul, not locally"%clients, | 69 print("Note, %s will be dereferenced on paul, not locally"%clients, |
65 file=sys.stderr) | 70 file=sys.stderr) |
66 cmd="/home/ht/bin/newkey.py %s -i %s"%(keytype, | 71 cmd="/home/ht/bin/newkey.py %s -i %s"%(keytype, |
67 '%s'%clients if clients is not None else '') | 72 '%s'%clients if clients is not None else '') |
68 print(cmd) | 73 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)) |
69 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)) | |
70 if res: | 74 if res: |
71 print("tar failed",res) | 75 print("tar failed",res) |
72 exit(res) | 76 exit(res) |
73 with open("/tmp/keys.tar.gz","rb") as keytar: | 77 with open("/tmp/keys.tar.gz","rb") as keytar: |
74 if ihost is None: | 78 if ihost is None: |
75 res=run(['ssh','paul',cmd],stdin=keytar).returncode | 79 res=run((['ssh','-v','paul',cmd] if debug else ['ssh','paul',cmd]), |
80 stdin=keytar).returncode | |
76 if res: | 81 if res: |
77 print("paul failed",res) | 82 print("paul failed",res) |
78 exit(res) | 83 exit(res) |
79 else: | 84 else: |
80 res=run(['ssh',ihost,'/afs/inf.ed.ac.uk/user/h/ht/share/bin/goHome',cmd],stdin=keytar).returncode | 85 res=run(['ssh',ihost, |
86 '/afs/inf.ed.ac.uk/user/h/ht/share/bin/goHome %s'%( | |
87 '-v' if debug else ''), | |
88 cmd],stdin=keytar).returncode | |
81 if res: | 89 if res: |
82 print("going via %s failed"%ihost,res) | 90 print("going via %s failed"%ihost,res) |
83 exit(res) | 91 exit(res) |
84 | 92 |
85 | 93 |