Fakes an SSH Server
Project description
Mock SSH Server
Do you...
-
have a test that SSHs into a server and don't want the hassle of setting one up for testing?
-
think monkeypatching isn't as good as it sounds?
-
want to develop an application and need a fake server to return predefined results?
This package is for you!
Installation
pip install fake-ssh
Usage
Blocking Server
A blocking server is often used for development purposes.
Simply write yourself a server.py
file:
from typing import Optional
from fake_ssh import Server
def handler(command: str) -> Optional[str]:
if command.startswith("ls"):
return "file1\nfile2\n"
elif command.startswith("echo"):
return command[4:].strip() + "\n"
if __name__ == "__main__":
Server(command_handler=handler, port=5050).run_blocking()
And run it:
$ python3 server.py
In a separate terminal, run:
$ ssh root@127.0.0.1 -p 5050 echo 42
42
$ ssh root@127.0.0.1 -p 5050 ls
file1
file2
(if you are prompted for a password, you can leave it blank)
Non-Blocking Server
A non blocking server is often used in tests.
This server runs in a thread and allows you to run some tests in parallel.
import paramiko
import pytest
from fake_ssh import Server
def handler(command):
if command == "ls":
return "file1\nfile2\n"
@pytest.fixture
def server():
with Server(command_handler=handler) as server:
yield server
@pytest.fixture
def client(server):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(hostname=server.host,
port=server.port,
username="root",
password="",
allow_agent=False,
look_for_keys=False)
return c
def test_ls(client):
_stdin, stdout, stderr = client.exec_command("ls")
assert stdout.read().decode() == "file1\nfile2\n"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fake-ssh-0.1.0a2.tar.gz
(7.4 kB
view hashes)
Built Distribution
Close
Hashes for fake_ssh-0.1.0a2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2496655588faa5db659c21871c68e05f26f8001323a205587b613391e251f11a |
|
MD5 | 9e9f6c6d8a49d12aa956e6ad9b0583e9 |
|
BLAKE2b-256 | 267d9ecc17e911dc4b4e714ebff553baa13cfb5a9342ce91172bdab5a08fbf60 |