This repository has been archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconnect.py
executable file
·75 lines (58 loc) · 1.87 KB
/
connect.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
#!/usr/bin/env python
import paramiko
import socket
import logging
import base64
from StringIO import StringIO
def sshfacts(host, port=22):
buffer = StringIO()
logHandler = logging.StreamHandler(buffer)
paramiko.common.logging.basicConfig(
level=paramiko.common.DEBUG,
format='%(name)s:%(message)s',
stream=buffer
)
s = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
tx = paramiko.Transport(s)
junk = tx.start_client()
serverkey = tx.get_remote_server_key()
md5key = serverkey.get_fingerprint()
so = tx.get_security_options()
authmethods = ['none']
try:
tx.auth_none('nothing')
except paramiko.BadAuthenticationType, e:
authmethods = e.allowed_types
keys = (
'kex-algos',
'server-encrypt',
'server-key',
'server-compress',
'server-mac',
'server-lang'
)
for line in buffer.getvalue().split("\n"):
if not line.startswith('paramiko.transport:'):
continue
parts = line.split(':',2)
if parts[1] == 'Banner':
yield ('ssh_banner', parts[2].strip())
elif parts[1] in keys:
algs = parts[2].split(',')
if len(algs) > 1 and algs[0] != '':
for i, alg in enumerate(algs):
yield('ssh_{0}-order_{1:02}'.format(parts[1], i+1), alg)
yield('ssh_{0}_{1}'.format(parts[1], alg), i+1)
for auth in authmethods:
yield ('ssh_auth_{0}'.format(auth), True)
yield ('ssh_server-fingerprint', serverkey.get_name() + ' ' + ":".join("{0:x}".format(ord(c)) for c in md5key))
if __name__ == '__main__':
import sys
import json
host = sys.argv[1]
facts = {}
for k in sshfacts(host):
facts[k[0]] = k[1]
print json.dumps(facts, indent=2, sort_keys=True)