-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathexample_host_key_verification.py
58 lines (45 loc) · 1.58 KB
/
example_host_key_verification.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
"""Connect to localhost, verifying host by reading from ~/.ssh/known_hosts"""
from __future__ import print_function
import os
import socket
from ssh2.session import Session
from ssh2.session import LIBSSH2_HOSTKEY_HASH_SHA1, LIBSSH2_HOSTKEY_TYPE_RSA
from ssh2.knownhost import LIBSSH2_KNOWNHOST_TYPE_PLAIN, \
LIBSSH2_KNOWNHOST_KEYENC_RAW, LIBSSH2_KNOWNHOST_KEY_SSHRSA
# Connection settings
host = 'localhost'
user = os.getlogin()
known_hosts = os.sep.join([os.path.expanduser('~'), '.ssh', 'known_hosts'])
# Make socket, connect
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, 22))
# Initialise
session = Session()
session.handshake(sock)
host_key, key_type = session.hostkey()
server_key_type = LIBSSH2_KNOWNHOST_KEY_SSHRSA \
if key_type == LIBSSH2_HOSTKEY_TYPE_RSA \
else LIBSSH2_KNOWNHOST_KEY_SSHDSS
kh = session.knownhost_init()
_read_hosts = kh.readfile(known_hosts)
print("Read %s hosts from known hosts file at %s" % (_read_hosts, known_hosts))
# Verification
type_mask = LIBSSH2_KNOWNHOST_TYPE_PLAIN | \
LIBSSH2_KNOWNHOST_KEYENC_RAW | \
server_key_type
kh.checkp(host, 22, host_key, type_mask)
print("Host verification passed.")
# Verification passed, continue with authentication
session.agent_auth(user)
channel = session.open_session()
channel.execute('echo me')
channel.wait_eof()
channel.close()
channel.wait_closed()
# Get exit status
print("Exit status: %s" % channel.get_exit_status())
# Print output
size, data = channel.read()
while size > 0:
print(data)
size, data = channel.read()