-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_sshkeys_from_ini_passwordauth.py
79 lines (53 loc) · 2.81 KB
/
copy_sshkeys_from_ini_passwordauth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#run this script and pass your ssh password to copy the specified ssh keys to all systems in the
#ansible inventory
def parse_ini(filename):
#open our .ini file for reading
f = open(filename, 'r')
content = f.readlines()
#what we will store our final output in
ini_file_output = []
for line in content:
#filter out commented lines and group headers from input file
if not (line.lstrip().startswith("#") or line.lstrip().startswith("[")):
array_including_newlines = []
array_including_newlines.append(line)
#removing \n characters from input
for object in array_including_newlines:
array_including_blank_entries = []
newlines = object.split('\n')
#newlines[0] is to select the string from the list that returns from the split. The split
#is (data, blank) or (blank, blank) which is dealt which below
array_including_blank_entries.append(newlines[0])
#removes all blank entries from final list (these are left from removing new lines above)
array_including_blank_entries = filter(None, array_including_blank_entries)
#at this point array_including_blank_entries is a list of hostnames or ip addresses and ready for use.
#building our output array
for system in array_including_blank_entries:
ini_file_output.append(system)
return ini_file_output
def password_auth_ssh_copy_id_from_array_input( sshpassword , input_file, svcacc, sshpubkey ):
#copy public ssh keys to systems in /etc/ansible/hosts
import subprocess
input_array = parse_ini(input_file)
for system in input_array:
import os
#check if the system is responding
pingcheck = os.system("ping -c 1 " + system )
if pingcheck == 0:
import time
# we define the bash command this was vs directly on subprocess because subprocess cannot
# handle white space inside quotes.
# Note: StrictHostKeyChecking is what 'auto accepts' the SSH authenticity check,
# otherwise it would silently fail when called below.
bashcmd = 'sshpass -p %s ssh-copy-id -o StrictHostKeyChecking=no -i %s %s@%s' % ( sshpassword, sshpubkey, svcacc, system )
# intiating the copy of ssh keys to systems in the inventory
subprocess.call(['bash','-c', bashcmd])
else:
print 'WARN: %s is not responding, skipping!' % ( system )
import sys
svcacc='root'
sshpassword=sys.argv[1]
sshpubkey='~/.ssh/id_rsa.pub'
input_file='/etc/ansible/hosts'
#show time
password_auth_ssh_copy_id_from_array_input(sshpassword, input_file, svcacc, sshpubkey)