-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh_checker.py
88 lines (74 loc) · 2.78 KB
/
ssh_checker.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
80
81
82
83
84
85
86
87
88
import paramiko
import logging
import io
filename = "devices.txt"
logger = logging.getLogger("paramiko.transport")
logger.setLevel(logging.DEBUG) # Set to DEBUG to capture all outputs
fh = logging.FileHandler('ssh_session.log')
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
def parse_log_data(log_content):
details = {
"Key Exchange Algorithms": "",
"Server Key Types": "",
"Encryption Algorithms": "",
"MAC Algorithms": "",
"Server Signature Algorithms": ""
}
lines = log_content.split('\n')
for line in lines:
if "kex algos:" in line:
details["Key Exchange Algorithms"] = line.split(': ')[1].split(', ')
elif "server key:" in line:
details["Server Key Types"] = line.split(': ')[1].split(', ')
elif "server encrypt:" in line:
details["Encryption Algorithms"] = line.split(': ')[1].split(', ')
elif "server mac:" in line:
details["MAC Algorithms"] = line.split(': ')[1].split(', ')
elif "Got EXT_INFO:" in line:
sig_algs = line.split("{'server-sig-algs': b'")[1].split("'}")[0]
details["Server Signature Algorithms"] = sig_algs.split(',')
return details
def fetch_ssh_details(hostname, port=22):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
log_stream = io.StringIO()
handler = logging.StreamHandler(log_stream)
logger.addHandler(handler)
try:
client.connect(hostname, port, username='admin', password='password.', look_for_keys=False, allow_agent=False)
return ['Connected successfully (unexpected)']
except paramiko.ssh_exception.SSHException as e:
return parse_log_data(log_stream.getvalue())
finally:
client.close()
logger.removeHandler(handler)
def read_ips(filename):
try:
with open(filename, 'r') as file:
return [line.strip() for line in file if line.strip()]
except FileNotFoundError:
with open(filename, 'w') as file:
file.write("127.0.0.1")
return read_ips(filename)
def main(ip_filename):
ips = read_ips(ip_filename)
results = []
for ip in ips:
print(ip)
details = fetch_ssh_details(ip)
results.append((ip, details))
for result in results:
print(f"IP Address: {result[0]}")
for key, values in result[1].items():
print(f"{key}:")
if isinstance(values, list):
for value in values:
print(f" - {value}")
else:
print(f" - {values}")
print("\n")
if __name__ == "__main__":
main(filename)