Skip to content

Commit 74e4948

Browse files
author
Pan
committed
Added embedded openssh server.
Updated example. Updated setup.py to work correctly without cython. Added integration tests with test key pair. Updated sources files with updated cython.
1 parent 4af641c commit 74e4948

File tree

13 files changed

+1120
-168
lines changed

13 files changed

+1120
-168
lines changed

embedded_server/__init__.py

Whitespace-only changes.

embedded_server/authorized_keys

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDOQrrFtUEF8owviZEAGHaitvHkmWg3Jyo+JF4i6CGIwXDDIuGIdCZK+dO7fyUHh72EW2Dt787okkXXx4dVWwmPzw0d/xE9ejkn9MCfYnA/tNBIEctDpnuXpiaIYDhxd9jRKVV+yppantFPTH4SR212HQYZBMXWM59aeNg/48OC8RlENyvJOo5kLeMCEza4ohiFylndeKlWVHHhC69fz63SeV2S8x2GJINKinmcjsH6actoGuZY2/iyzf7fdJhUgTI96GsyfvdESkzTFm/W5KQwgJqCZUt1tXupedkPHaUKN5ZTlc/dHdBWqwOFq1qgmUqrSwsXFGE0Fu7pFnK+JAvb

embedded_server/openssh.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# This file is part of ssh2-python.
2+
# Copyright (C) 2017 Panos Kittenis
3+
4+
# This library is free software; you can redistribute it and/or
5+
# modify it under the terms of the GNU Lesser General Public
6+
# License as published by the Free Software Foundation, version 2.1.
7+
8+
# This library is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
# Lesser General Public License for more details.
12+
13+
# You should have received a copy of the GNU Lesser General Public
14+
# License along with this library; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16+
17+
import os
18+
import socket
19+
20+
from subprocess import Popen
21+
from time import sleep
22+
23+
24+
SERVER_KEY = os.path.sep.join([os.path.dirname(__file__), 'rsa.key'])
25+
SSHD_CONFIG = os.path.sep.join([os.path.dirname(__file__), 'sshd_config'])
26+
27+
class OpenSSHServer(object):
28+
29+
def __init__(self, port=2222):
30+
self.port = port
31+
self.server_proc = None
32+
33+
def start_server(self):
34+
cmd = ['/usr/sbin/sshd', '-q', '-D', '-p', str(self.port),
35+
'-h', SERVER_KEY, '-f', SSHD_CONFIG]
36+
server = Popen(cmd)
37+
self.server_proc = server
38+
self._wait_for_port()
39+
40+
def _wait_for_port(self):
41+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
42+
while sock.connect_ex(('127.0.0.1', self.port)) != 0:
43+
sleep(.1)
44+
del sock
45+
46+
def stop(self):
47+
if self.server_proc is not None and self.server_proc.returncode is None:
48+
self.server_proc.terminate()
49+
self.server_proc.wait()
50+
51+
def __del__(self):
52+
self.stop()

embedded_server/rsa.key

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICXQIBAAKBgQDI7iK3d8eWYZlYloat94c5VjtFY7c/0zuGl8C7uMnZ3t6i2G99
3+
66hEW0nCFSZkOW5F0XKEVj+EUCHvo8koYC6wiohAqWQnEwIoOoh7GSAcB8gP/qaq
4+
+adIl/Rvlby/mHakj+y05LBND6nFWHAn1y1gOFFKUXSJNRZPXSFy47gqzwIBIwKB
5+
gQCbANjz7q/pCXZLp1Hz6tYHqOvlEmjK1iabB1oqafrMpJ0eibUX/u+FMHq6StR5
6+
M5413BaDWHokPdEJUnabfWXXR3SMlBUKrck0eAer1O8m78yxu3OEdpRk+znVo4DL
7+
guMeCdJB/qcF0kEsx+Q8HP42MZU1oCmk3PbfXNFwaHbWuwJBAOQ/ry/hLD7AqB8x
8+
DmCM82A9E59ICNNlHOhxpJoh6nrNTPCsBAEu/SmqrL8mS6gmbRKUaya5Lx1pkxj2
9+
s/kWOokCQQDhXCcYXjjWiIfxhl6Rlgkk1vmI0l6785XSJNv4P7pXjGmShXfIzroh
10+
S8uWK3tL0GELY7+UAKDTUEVjjQdGxYSXAkEA3bo1JzKCwJ3lJZ1ebGuqmADRO6UP
11+
40xH977aadfN1mEI6cusHmgpISl0nG5YH7BMsvaT+bs1FUH8m+hXDzoqOwJBAK3Z
12+
X/za+KV/REya2z0b+GzgWhkXUGUa/owrEBdHGriQ47osclkUgPUdNqcLmaDilAF4
13+
1Z4PHPrI5RJIONAx+JECQQC/fChqjBgFpk6iJ+BOdSexQpgfxH/u/457W10Y43HR
14+
soS+8btbHqjQkowQ/2NTlUfWvqIlfxs6ZbFsIp/HrhZL
15+
-----END RSA PRIVATE KEY-----

embedded_server/sshd_config

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Protocol 2
2+
UsePrivilegeSeparation no
3+
UsePAM no
4+
RhostsRSAAuthentication no
5+
HostbasedAuthentication no
6+
IgnoreUserKnownHosts yes
7+
GSSAPIAuthentication no
8+
9+
KeyRegenerationInterval 3600
10+
ServerKeyBits 1024
11+
12+
AcceptEnv LANG LC_*
13+
Subsystem sftp /usr/lib/openssh/sftp-server
14+
AuthorizedKeysFile ssh2-python/embedded_server/authorized_keys

examples/example_echo.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
session.agent_auth(user)
2525

2626
# Agent capabilities
27-
# agent = session.agent_init()
28-
# agent.connect()
29-
# identities = agent.get_identities(user)
30-
# print(identities)
31-
# print(identities[0].magic)
27+
agent = session.agent_init()
28+
agent.connect()
29+
identities = agent.get_identities(user)
30+
print(identities)
31+
print(identities[0].magic)
32+
del agent
3233

3334
# Public key blob available as identities[0].blob
3435

requirements_dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cython

setup.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import platform
44
import os
55
import sys
6+
from glob import glob
67

78
# import versioneer
89
from setuptools import setup, find_packages, Extension
@@ -16,11 +17,13 @@
1617
else:
1718
USING_CYTHON = True
1819

20+
names = 'ssh2/*'
1921
ext = 'pyx' if USING_CYTHON else 'c'
22+
sources = ['ssh2/*.%s' % (ext,)]
2023

2124
extensions = [
2225
Extension('ssh2/*',
23-
sources=['ssh2/*.pyx'],
26+
sources=sources,
2427
libraries=['ssh2'],
2528
# extra_compile_args=["-O3"],
2629
extra_compile_args=["-ggdb"],
@@ -30,6 +33,21 @@
3033
# for ext in extensions
3134
]
3235

36+
if ext == 'c':
37+
sources = glob(sources[0])
38+
# names = [ for s in sources]
39+
extensions = [
40+
Extension(sources[i].split('.')[0].replace('/', '.'),
41+
sources=[sources[i]],
42+
libraries=['ssh2'],
43+
# extra_compile_args=["-O3"],
44+
extra_compile_args=["-ggdb"],
45+
# For conditional compilation
46+
# pyrex_compile_time_env
47+
)
48+
for i in range(len(sources))]
49+
50+
3351
if USING_CYTHON:
3452
extensions = cythonize(
3553
extensions,

0 commit comments

Comments
 (0)