Skip to content

Base SSHCommand

Nicolas Couture edited this page Apr 19, 2026 · 1 revision

Base SSHCommand

The SSHCommand class is the foundational building block for all custom interactions in MockSSH.

Implementation

To create a custom command, inherit from MockSSH.SSHCommand and override the call or start methods.

import MockSSH

class MyCommand(MockSSH.SSHCommand):
    name = "hello"

    def call(self):
        # self.protocol provides access to the underlying SSH session
        # self.args contains any arguments passed to the command
        self.protocol.writeln("Hello, you passed: %s" % (self.args,))

Key Methods and Attributes

Attribute/Method Description
self.name The string used to trigger the command in the shell.
self.args A tuple of arguments passed to the command (e.g., cmd arg1 arg2).
self.protocol The SSHProtocol instance managing the terminal.
self.writeln(msg) Convenience method to write a string followed by a newline.
self.write(msg) Write raw data to the terminal without a newline.
self.exit() Removes the command from the stack and returns control to the shell.

Handling Input

If your command needs to handle user input after it has started, override lineReceived(self, line):

    def lineReceived(self, line):
        print(f"User typed: {line}")
        self.exit()

Clone this wiki locally