Robot Framework SSHLibrary
Robot Framework SSHLibrary is a test library for SSH and SFTP operations. It provides keywords for executing commands, interacting with interactive shells, transferring files and directories via SFTP, and asserting the existence of files and directories on remote machines. The library is operating system independent and currently supports Python 3.6 or newer (older versions like 3.8.0 also supported Python 2.7 and 3.4+). It maintains an active release cadence with regular updates and enhancements.
Common errors
-
SSHLibrary Exception: Timeout opening tunnel
cause The SSH connection could not be established or the tunnel creation timed out within the default or specified timeout period, often due to network issues or slow server response.fixIncrease the timeout values for both library import and the `Open Connection` keyword. Example: `Library SSHLibrary 30s` and `Open Connection ${HOST} port=${PORT} timeout=60s`. -
Authentication failed for user 'username'
cause Incorrect username/password, invalid public key, server configuration issues (e.g., password-less login not working as expected), or incompatible Paramiko version.fixDouble-check credentials. If using public key, verify key path and permissions. For password-less root logins, consider trying `pip uninstall paramiko` then `pip install paramiko-ng` as a workaround (as `paramiko` sometimes has issues with this specific scenario). Ensure the correct login keyword (`Login` vs. `Login With Public Key`) is used. -
Connection not open.
cause Attempting to use SSHLibrary keywords that require an active connection (e.g., `Execute Command`, `Put File`) before a successful `Login` has occurred, or after a connection has been explicitly closed.fixEnsure `Open Connection` and `Login` keywords are called successfully before any connection-dependent keywords. For example, `Open Connection ${HOST}` followed by `Login ${USERNAME} ${PASSWORD}`. -
gaierror: [Errno -2] Name or service not known
cause The hostname provided to `Open Connection` cannot be resolved to an IP address by the system's DNS resolver.fixVerify the hostname is correct and reachable from the test runner machine. Use an IP address instead of a hostname if DNS resolution is problematic. Check network configuration and DNS settings. -
Logging into 'host,port=XXXX:22' as 'user'
cause Incorrect syntax when specifying the port argument in `Open Connection`. Using a comma instead of spaces between arguments.fixRobot Framework uses multiple spaces as a separator for arguments. Change `Open Connection ${HOST},port=${PORT}` to `Open Connection ${HOST} port=${PORT}`.
Warnings
- breaking SSHLibrary 3.0.0 introduced Python 3 support (Python 2.7, 3.4+). Older versions of SSHLibrary may not be compatible with newer Python 3 versions, and newer SSHLibrary versions (e.g., 3.8.0 onwards) officially support Python 3.6+.
- breaking In SSHLibrary 3.0.0, the `Login with Public Key` keyword arguments changed. Specifically, the `delay` argument must now be provided as a named argument (e.g., `delay=1s`).
- breaking Prior to SSHLibrary 3.1.0, all non-empty strings, including 'no' and 'none', were considered `True` for boolean arguments. This behavior was aligned with Robot Framework 3.0.3, where 'none' is considered `False`.
- gotcha Connections or command executions can hang or time out (e.g., 'SSHLibrary Exception: Timeout opening tunnel') due to insufficient default timeouts or network latency.
- gotcha The `path_separator` argument (used by keywords like `Get File`, `Put Directory`) defaults to '/' (Unix-like). On Windows SSH servers, this may need to be explicitly set to '\\' (doubled backslash in Robot Framework data) if the server uses backslashes.
Install
-
pip install robotframework-sshlibrary -
apk add gcc make openssl-dev musl-dev libffi-dev && pip install robotframework-sshlibrary
Imports
- SSHLibrary
from SSHLibrary import SSHLibrary
*** Settings *** Library SSHLibrary
Quickstart
*** Settings ***
Documentation Demonstrates SSHLibrary connection and command execution.
Library SSHLibrary
*** Variables ***
${SSH_HOST} ${OS_ENVIRONMENT_SSH_HOST}
${SSH_PORT} ${OS_ENVIRONMENT_SSH_PORT}
${SSH_USERNAME} ${OS_ENVIRONMENT_SSH_USERNAME}
${SSH_PASSWORD} ${OS_ENVIRONMENT_SSH_PASSWORD}
*** Test Cases ***
Execute Remote Command
[Tags] SSH
Open Connection ${SSH_HOST} port=${SSH_PORT}
Login ${SSH_USERNAME} ${SSH_PASSWORD}
${output}= Execute Command ls -l /tmp
Log To Console Command Output: ${output}
Should Contain ${output} total
Close Connection