{"id":6762,"library":"paramiko-expect","title":"paramiko-expect","description":"paramiko-expect is an expect-like extension for the Paramiko SSH library, enabling scripts to interact with remote hosts via a true SSH connection in an interactive, automated fashion. It's particularly useful for automating interactions with command-line applications, shell scripts, or network devices that require 'expect'-style responses to prompts. The current version is 0.3.5, and releases are infrequent, with the last update in late 2022.","status":"active","version":"0.3.5","language":"en","source_language":"en","source_url":"https://github.com/fgimian/paramiko-expect","tags":["ssh","expect","automation","remote execution","paramiko","interactive"],"install":[{"cmd":"pip install paramiko-expect","lang":"bash","label":"Install from PyPI"}],"dependencies":[{"reason":"paramiko-expect is a wrapper around the Paramiko SSH library and requires it for core SSH client functionality.","package":"paramiko"}],"imports":[{"symbol":"SSHClientInteraction","correct":"from paramiko_expect import SSHClientInteraction"}],"quickstart":{"code":"import paramiko\nimport os\nfrom paramiko_expect import SSHClientInteraction\n\nHOSTNAME = os.environ.get('SSH_HOSTNAME', 'localhost')\nUSERNAME = os.environ.get('SSH_USERNAME', 'testuser')\nPASSWORD = os.environ.get('SSH_PASSWORD', 'testpass')\nPROMPT = r'.*\\$ ' # Example prompt for a Linux shell ending with $ or #\n\nclient = paramiko.SSHClient()\nclient.load_system_host_keys()\nclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n\ntry:\n    client.connect(hostname=HOSTNAME, username=USERNAME, password=PASSWORD, timeout=10)\n    print(f\"SSH connection established to {HOSTNAME}\")\n\n    with SSHClientInteraction(client, timeout=10, display=True) as interact:\n        # Wait for the initial prompt after connection\n        interact.expect(PROMPT)\n        print(f\"Initial prompt: {interact.last_match}\")\n\n        # Send a command and wait for the prompt again\n        interact.send('uname -a')\n        interact.expect(PROMPT)\n        print(f\"Uname output:\\n{interact.current_output_clean}\")\n\n        # Send another command\n        interact.send('echo Hello paramiko-expect!')\n        interact.expect(PROMPT)\n        print(f\"Echo output:\\n{interact.current_output_clean}\")\n\nfinally:\n    client.close()\n    print(\"SSH connection closed.\")","lang":"python","description":"This quickstart demonstrates how to establish an SSH connection using Paramiko, then wrap the SSHClient object with SSHClientInteraction to send commands and expect specific prompts. It prints the cleaned output of each command."},"warnings":[{"fix":"Pin your Paramiko dependency to a version known to be compatible with paramiko-expect v0.3.5 (e.g., Paramiko <3.0). If you require newer Paramiko features, thoroughly test for compatibility or consider alternatives.","message":"Potential compatibility issues with newer Paramiko versions: paramiko-expect (v0.3.5) was last updated in November 2022. The underlying Paramiko library has had several major releases since then (e.g., Paramiko 3.x in December 2023), including dropping support for Python 2 and older Python 3 versions (<3.6). Using paramiko-expect with the very latest Paramiko releases might lead to unforeseen compatibility issues or unexpected behavior.","severity":"breaking","affected_versions":"<0.3.5, Paramiko >= 3.0"},{"fix":"Adjust the `tty_width` parameter in `SSHClientInteraction` to match or exceed the maximum expected line length, or ensure your `expect()` patterns are robust enough to handle potential line wrapping in the echoed command.","message":"Output cleaning can break with long commands due to TTY width: If a command sent via `send()` is long enough to wrap lines within the remote terminal's `tty_width`, the echoed command line may contain inserted spaces. This can cause `current_output_clean` to fail to correctly remove the echoed command, resulting in it appearing in your captured output or breaking subsequent `expect()` calls based on output patterns.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Simplify `expect()` patterns to target only the distinctive end-of-prompt or end-of-output string. Test your patterns thoroughly to ensure they reliably match the desired state without being overly strict.","message":"Misunderstanding `expect()` patterns for multi-line output/prompts: When expecting a prompt or output that spans multiple lines, users sometimes create complex regex patterns or lists expecting every line. Often, `interact.expect()` only needs to match the *final* line or a unique part of the prompt to correctly identify the state, and over-specifying can lead to timeouts or incorrect matches.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always initialize `SSHClientInteraction` with a fully connected `paramiko.SSHClient` object. Ensure the `SSHClient` object has already established a session with `client.connect()` before passing it to `SSHClientInteraction`.","message":"`SSHClientInteraction` requires a `paramiko.SSHClient` object: The `SSHClientInteraction` class is designed to wrap a `paramiko.SSHClient` instance. While the library's description mentions potential future support for `paramiko.Transport` objects, currently, passing a `Transport` object directly will result in an error or incorrect behavior.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}