annotate newkey.py @ 43:f67f9ea191b7

from http://sourceforge.net/projects/pdf2xml/files/misc/vec2svg-2.py/download
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Thu, 24 Feb 2022 15:43:26 +0000
parents 59517f60826d
children
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
42
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
4 Usage: newkey.py [-d] [-v intermediary] keytype [-i] [-c client | client list filename]
39
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_
42
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
6 If -d, add -v to ssh
39
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7 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
8
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 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
10 1) Using ssh-copy-id directly;
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 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
12 [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
13
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14 import sys, os
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15 from socket import gethostname
41
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
16 from subprocess import run, PIPE
39
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
17
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18 hack=os.environ.copy()
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
19 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
20
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
21 def ssh_copy_id(keyfile,client):
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
22 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
23 res=run(["ssh-copy-id","-i",keyfile,client],env=hack,text=True,
42
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
24 capture_output=True)
41
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
25 if res.returncode:
900ff99ec749 less noisy if success
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 40
diff changeset
26 print('failed %s'%res.returncode)
42
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
27 print(res.stderr,res.stdout)
41
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
42
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
31 debug=False
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
32 if sys.argv[1]=='-d':
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
33 sys.argv.pop(1)
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
34 debug=True
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
35
39
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
36 ihost=None
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
37 if sys.argv[1]=='-v':
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
38 sys.argv.pop(1)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
39 ihost=sys.argv.pop(1).lower()
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
40
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
41 keytype=sys.argv.pop(1)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
42
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
43 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
44 # Working for some other client
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
45 sys.argv.pop(1)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
46 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
47 if res:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
48 print("untar failed",res)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
49 exit(res)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
50 keydir="/tmp/keys"
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
51 else:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
52 keydir=os.path.expanduser("~/.ssh")
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
53
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
54 keyfile="%s/id_%s.pub"%(keydir,keytype)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
55
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
56 try:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
57 clients=sys.argv.pop(1)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
58 if clients=='-c':
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
59 clients="-c %s"%sys.argv.pop(1)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
60 except:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
61 clients='/etc/sshclients'
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
62
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
63 host=gethostname().lower()
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
64
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
65 print(keytype,keyfile,ihost,clients)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
66
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
67 if host!='paul':
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
68 if clients[0]!='-':
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
69 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
70 file=sys.stderr)
40
f13017bb4502 works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 39
diff changeset
71 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
72 '%s'%clients if clients is not None else '')
42
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
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))
39
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
74 if res:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
75 print("tar failed",res)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
76 exit(res)
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
77 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
78 if ihost is None:
42
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
79 res=run((['ssh','-v','paul',cmd] if debug else ['ssh','paul',cmd]),
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
80 stdin=keytar).returncode
40
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("paul failed",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 else:
42
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
85 res=run(['ssh',ihost,
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
86 '/afs/inf.ed.ac.uk/user/h/ht/share/bin/goHome %s'%(
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
87 '-v' if debug else ''),
59517f60826d quiet working, -d to use ssh -v
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 41
diff changeset
88 cmd],stdin=keytar).returncode
40
f13017bb4502 works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 39
diff changeset
89 if res:
f13017bb4502 works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 39
diff changeset
90 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
91 exit(res)
f13017bb4502 works on markup with -v ecclerig...
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 39
diff changeset
92
39
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
93
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
94 if clients[0]=='-':
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
95 ssh_copy_id(keyfile,clients[3:])
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
96 else:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
97 with open(clients) as cfile:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
98 for l in cfile:
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
99 ssh_copy_id(keyfile,l.rstrip())
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
100
a0b702a76872 works from maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
101