Mercurial > hg > python
annotate newkey.py @ 41:900ff99ec749
less noisy if success
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Mon, 03 Jan 2022 19:26:11 +0000 |
parents | f13017bb4502 |
children | 59517f60826d |
rev | line source |
---|---|
39 | 1 #!/usr/bin/python3 |
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 | |
4 Usage: newkey.py [-v intermediary] keytype [-i] [-c client | client list filename] | |
5 client list filename defaults to /etc/sshclients _on Paul_ | |
6 If -i, read tar with new key pair from stdin, otherwise from ~/.ssh/id_$keytype.pub | |
7 | |
8 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 2) Using ssh-copy-id via intermediary (defaults to $DESKTOP) | |
11 [change this to maritain once it's worked for everbody''' # ' | |
12 | |
13 import sys, os | |
14 from socket import gethostname | |
41 | 15 from subprocess import run, PIPE |
39 | 16 |
17 hack=os.environ.copy() | |
18 hack['SSH_AUTH_SOCK']='/home/ht/.gnupg/S.gpg-agent.ssh' | |
19 | |
20 def ssh_copy_id(keyfile,client): | |
21 print(["ssh-copy-id","-i",keyfile,client],'...',sep='',end='') | |
41 | 22 res=run(["ssh-copy-id","-i",keyfile,client],env=hack,text=True, |
23 stdout=PIPE,stderr=PIPE) | |
24 if res.returncode: | |
25 print('failed %s'%res.returncode) | |
26 outs,errs=res.communicate() | |
27 print(errs) | |
28 else: | |
29 print('succeeded') | |
39 | 30 |
31 ihost=None | |
32 if sys.argv[1]=='-v': | |
33 sys.argv.pop(1) | |
34 ihost=sys.argv.pop(1).lower() | |
35 | |
36 keytype=sys.argv.pop(1) | |
37 | |
38 if len(sys.argv)>1 and sys.argv[1]=='-i': | |
39 # Working for some other client | |
40 sys.argv.pop(1) | |
41 res=os.system("cd /tmp ; rm -rf keys ; tar -xzf -") | |
42 if res: | |
43 print("untar failed",res) | |
44 exit(res) | |
45 keydir="/tmp/keys" | |
46 else: | |
47 keydir=os.path.expanduser("~/.ssh") | |
48 | |
49 keyfile="%s/id_%s.pub"%(keydir,keytype) | |
50 | |
51 try: | |
52 clients=sys.argv.pop(1) | |
53 if clients=='-c': | |
54 clients="-c %s"%sys.argv.pop(1) | |
55 except: | |
56 clients='/etc/sshclients' | |
57 | |
58 host=gethostname().lower() | |
59 | |
60 print(keytype,keyfile,ihost,clients) | |
61 | |
62 if host!='paul': | |
63 if clients[0]!='-': | |
64 print("Note, %s will be dereferenced on paul, not locally"%clients, | |
65 file=sys.stderr) | |
40
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
66 cmd="/home/ht/bin/newkey.py %s -i %s"%(keytype, |
39 | 67 '%s'%clients if clients is not None else '') |
68 print(cmd) | |
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: | |
71 print("tar failed",res) | |
72 exit(res) | |
73 with open("/tmp/keys.tar.gz","rb") as keytar: | |
40
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
74 if ihost is None: |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
75 res=run(['ssh','paul',cmd],stdin=keytar).returncode |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
76 if res: |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
77 print("paul failed",res) |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
78 exit(res) |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
79 else: |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
80 res=run(['ssh',ihost,'/afs/inf.ed.ac.uk/user/h/ht/share/bin/goHome',cmd],stdin=keytar).returncode |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
81 if res: |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
82 print("going via %s failed"%ihost,res) |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
83 exit(res) |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
84 |
39 | 85 |
86 if clients[0]=='-': | |
87 ssh_copy_id(keyfile,clients[3:]) | |
88 else: | |
89 with open(clients) as cfile: | |
90 for l in cfile: | |
91 ssh_copy_id(keyfile,l.rstrip()) | |
92 | |
93 |