Mercurial > hg > python
annotate newkey.py @ 40:f13017bb4502
works on markup with -v ecclerig...
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Mon, 03 Jan 2022 19:19:20 +0000 |
parents | a0b702a76872 |
children | 900ff99ec749 |
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 | |
15 from subprocess import run | |
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='') | |
22 res=run(["ssh-copy-id","-i",keyfile,client],env=hack).returncode | |
23 print('failed %s'%res if res else 'succeeded') | |
24 | |
25 ihost=None | |
26 if sys.argv[1]=='-v': | |
27 sys.argv.pop(1) | |
28 ihost=sys.argv.pop(1).lower() | |
29 | |
30 keytype=sys.argv.pop(1) | |
31 | |
32 if len(sys.argv)>1 and sys.argv[1]=='-i': | |
33 # Working for some other client | |
34 sys.argv.pop(1) | |
35 res=os.system("cd /tmp ; rm -rf keys ; tar -xzf -") | |
36 if res: | |
37 print("untar failed",res) | |
38 exit(res) | |
39 keydir="/tmp/keys" | |
40 else: | |
41 keydir=os.path.expanduser("~/.ssh") | |
42 | |
43 keyfile="%s/id_%s.pub"%(keydir,keytype) | |
44 | |
45 try: | |
46 clients=sys.argv.pop(1) | |
47 if clients=='-c': | |
48 clients="-c %s"%sys.argv.pop(1) | |
49 except: | |
50 clients='/etc/sshclients' | |
51 | |
52 host=gethostname().lower() | |
53 | |
54 print(keytype,keyfile,ihost,clients) | |
55 | |
56 if host!='paul': | |
57 if clients[0]!='-': | |
58 print("Note, %s will be dereferenced on paul, not locally"%clients, | |
59 file=sys.stderr) | |
40
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
60 cmd="/home/ht/bin/newkey.py %s -i %s"%(keytype, |
39 | 61 '%s'%clients if clients is not None else '') |
62 print(cmd) | |
63 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)) | |
64 if res: | |
65 print("tar failed",res) | |
66 exit(res) | |
67 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
|
68 if ihost is None: |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
69 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
|
70 if res: |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
71 print("paul failed",res) |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
72 exit(res) |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
73 else: |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
74 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
|
75 if res: |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
76 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
|
77 exit(res) |
f13017bb4502
works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
78 |
39 | 79 |
80 if clients[0]=='-': | |
81 ssh_copy_id(keyfile,clients[3:]) | |
82 else: | |
83 with open(clients) as cfile: | |
84 for l in cfile: | |
85 ssh_copy_id(keyfile,l.rstrip()) | |
86 | |
87 |