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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/python3
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
2 '''Install a new ssh key on paul and in a list of hosts,
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3 confirm success, and if so comment out any older versions
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
4 Usage: newkey.py [-v intermediary] keytype [-i] [-c client | client list filename]
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
5 client list filename defaults to /etc/sshclients _on Paul_
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
6 If -i, read tar with new key pair from stdin, otherwise from ~/.ssh/id_$keytype.pub
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
8 If run on a machine other than paul, will try to get it to Paul to distribute:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 1) Using ssh-copy-id directly;
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
10 2) Using ssh-copy-id via intermediary (defaults to $DESKTOP)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 [change this to maritain once it's worked for everbody''' # '
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
12
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13 import sys, os
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14 from socket import gethostname
41
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
15 from subprocess import run, PIPE
39
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
16
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
17 hack=os.environ.copy()
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18 hack['SSH_AUTH_SOCK']='/home/ht/.gnupg/S.gpg-agent.ssh'
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
19
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
20 def ssh_copy_id(keyfile,client):
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
21 print(["ssh-copy-id","-i",keyfile,client],'...',sep='',end='')
41
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
22 res=run(["ssh-copy-id","-i",keyfile,client],env=hack,text=True,
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
23 stdout=PIPE,stderr=PIPE)
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
24 if res.returncode:
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
25 print('failed %s'%res.returncode)
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
26 outs,errs=res.communicate()
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
27 print(errs)
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
28 else:
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
29 print('succeeded')
39
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
30
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
31 ihost=None
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
32 if sys.argv[1]=='-v':
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
33 sys.argv.pop(1)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
34 ihost=sys.argv.pop(1).lower()
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
35
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
36 keytype=sys.argv.pop(1)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
37
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
38 if len(sys.argv)>1 and sys.argv[1]=='-i':
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
39 # Working for some other client
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
40 sys.argv.pop(1)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
41 res=os.system("cd /tmp ; rm -rf keys ; tar -xzf -")
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
42 if res:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
43 print("untar failed",res)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
44 exit(res)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
45 keydir="/tmp/keys"
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
46 else:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
47 keydir=os.path.expanduser("~/.ssh")
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
48
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
49 keyfile="%s/id_%s.pub"%(keydir,keytype)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
50
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
51 try:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
52 clients=sys.argv.pop(1)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
53 if clients=='-c':
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
54 clients="-c %s"%sys.argv.pop(1)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
55 except:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
56 clients='/etc/sshclients'
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
57
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
58 host=gethostname().lower()
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
59
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
60 print(keytype,keyfile,ihost,clients)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
61
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
62 if host!='paul':
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
63 if clients[0]!='-':
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
64 print("Note, %s will be dereferenced on paul, not locally"%clients,
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
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
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
67 '%s'%clients if clients is not None else '')
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
68 print(cmd)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
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))
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
70 if res:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
71 print("tar failed",res)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
72 exit(res)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
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
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
85
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
86 if clients[0]=='-':
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
87 ssh_copy_id(keyfile,clients[3:])
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
88 else:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
89 with open(clients) as cfile:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
90 for l in cfile:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
91 ssh_copy_id(keyfile,l.rstrip())
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
92
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
93