Skip to content

Commit 6dd555c

Browse files
implement csv table with logs
1 parent aac2299 commit 6dd555c

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

SSHKeyDistribut0r/key_distribut0r.py

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import re
99
import socket
1010
import sys
11-
import yaml
11+
import csv
1212

1313
import paramiko
1414
import scp
15+
import yaml
1516

1617
logging.raiseExceptions = False
1718

@@ -26,6 +27,9 @@
2627
YAML_EXT = re.compile("^\\.ya?ml$")
2728
JSON_EXT = re.compile("^\\.json$")
2829

30+
ERROR_STATUS = '✗ Error'
31+
SUCCESS_STATUS = '✓ Success'
32+
2933

3034
def remove_special_chars(original_string):
3135
return ''.join(e for e in original_string if e.isalnum())
@@ -63,10 +67,21 @@ def read_config(config_file):
6367
sys.exit(1)
6468

6569

70+
def create_result_csv_table(messages):
71+
messages.sort(key=lambda m: m[0] == ERROR_STATUS)
72+
try:
73+
with open('ssh_keys_distributor_result.csv', 'w', encoding='utf-8') as file:
74+
writer = csv.writer(file, delimiter='|')
75+
writer.writerows(messages)
76+
except OSError as e:
77+
print(e)
78+
79+
6680
def main(args):
6781
# Load config files
6882
servers = read_config(args.server)
6983
keys = read_config(args.keys)
84+
messages = [('Status', 'Ip', 'Comment', 'Description')]
7085

7186
for server in servers:
7287
if server['authorized_users']:
@@ -85,7 +100,9 @@ def main(args):
85100
key_stream.write('%s\n' % key)
86101

87102
if args.dry_run:
88-
server_info_log(server['ip'], server['comment'], ', '.join(server_users))
103+
msg = server['ip'], server['comment'], ', '.join(server_users)
104+
server_info_log(*msg)
105+
messages.append((SUCCESS_STATUS, *msg))
89106
else:
90107
# Configure SSH client
91108
ssh_client = paramiko.SSHClient()
@@ -102,25 +119,37 @@ def main(args):
102119
scp_client.putfo(key_stream, '.ssh/authorized_keys')
103120

104121
key_stream.close()
105-
server_info_log(server['ip'], server['comment'], ', '.join(server_users))
122+
msg = server['ip'], server['comment'], ', '.join(server_users)
123+
server_info_log(*msg)
124+
messages.append((SUCCESS_STATUS, *msg))
106125

107126
except paramiko.ssh_exception.PasswordRequiredException:
108-
server_error_log(
109-
server['ip'],
110-
server['comment'],
111-
'The private key file is protected by a passphrase, which is currently not supported.'
112-
)
127+
msg = server['ip'], \
128+
server['comment'], \
129+
'The private key file is protected by a passphrase, which is currently not supported.'
130+
server_error_log(*msg)
131+
messages.append((ERROR_STATUS, msg))
113132
except paramiko.ssh_exception.AuthenticationException:
114-
server_error_log(
115-
server['ip'],
116-
server['comment'],
117-
'Cannot connect to server because of an authentication problem.'
118-
)
133+
msg = server['ip'],\
134+
server['comment'], \
135+
'Cannot connect to server because of an authentication problem.'
136+
server_error_log(*msg)
137+
messages.append((ERROR_STATUS, *msg))
119138
except scp.SCPException:
120-
server_error_log(server['ip'], server['comment'], 'Cannot send file to server.')
139+
msg = server['ip'], server['comment'], 'Cannot send file to server.'
140+
server_error_log(*msg)
141+
messages.append((ERROR_STATUS, *msg))
121142
except (paramiko.ssh_exception.NoValidConnectionsError, paramiko.ssh_exception.SSHException):
122-
server_error_log(server['ip'], server['comment'], 'Cannot connect to server.')
143+
msg = server['ip'], server['comment'], 'Cannot connect to server.'
144+
server_error_log(*msg)
145+
messages.append((ERROR_STATUS, *msg))
123146
except socket.timeout:
124-
server_error_log(server['ip'], server['comment'], 'Cannot connect to server because of a timeout.')
147+
msg = server['ip'], server['comment'], 'Cannot connect to server because of a timeout.'
148+
server_error_log(*msg)
149+
messages.append((ERROR_STATUS, *msg))
125150
else:
126-
server_error_log(server['ip'], server['comment'], 'No user mentioned in configuration file!')
151+
msg = server['ip'], server['comment'], 'No user mentioned in configuration file!'
152+
server_error_log(*msg)
153+
messages.append((ERROR_STATUS, *msg))
154+
create_result_csv_table(messages)
155+

0 commit comments

Comments
 (0)