Skip to main content

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)

Note how you need to specify a non standard port (5050). Using the standard port (22) would require root permissions and is probably unsafe.

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


def my_ls(host, port):
    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.exec_command("ls")[1].read().decode().splitlines()


def test_ls(server):
    assert my_ls(server.host, server.port) == ["file1", "file2"]

Project details


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.0a3.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

fake_ssh-0.1.0a3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file fake-ssh-0.1.0a3.tar.gz.

File metadata

  • Download URL: fake-ssh-0.1.0a3.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.7

File hashes

Hashes for fake-ssh-0.1.0a3.tar.gz
Algorithm Hash digest
SHA256 fcaed1a29b6bc8225aa7463313f239051e8d7a4de0080f56e3749e884bf08280
MD5 211ee7f525b2eeb4563d60b0fd273b53
BLAKE2b-256 83cd49dcb760996bc72b1012b9445dc70116691672e4590a8d53e7b018e07815

See more details on using hashes here.

File details

Details for the file fake_ssh-0.1.0a3-py3-none-any.whl.

File metadata

  • Download URL: fake_ssh-0.1.0a3-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.7

File hashes

Hashes for fake_ssh-0.1.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 d894feeb4c8e6f5d47f0714ae31588b6594338e50cab4fdb4bd63e7305402221
MD5 57b2db1e231b58d1b36e429e937349ab
BLAKE2b-256 20bea4ea26fe498f37bdb13e979f7df2440476f8b01118be7cdf29d8d73ddf12

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page