RobotFramework使用SSHLibrary
liqiang3@sugon.com
【参考文档】:
- 【1】 https://pypi.org/project/robotframework-sshlibrary/#introduction
- 【2】 https://github.com/robotframework/SSHLibrary
在RobtoFramework自动化测试中,安装robotframework-sshlibrary模块后,即可使用SSH相关关键字,连接控制Linux或Windows完成自动化测试工作。
1. 安装加载
1.1 安装
pip install robotframework-sshlibrary |
1.2 RobotFramework加载
在Add Library时,输入“SSHLibrary”,注意大小写,即可在RobotFramework中加载robotframework-sshlibrary库。
2. 关键字
2.1 连接相关
Open Connection | Opens a new SSH connection to the given host and port. |
Close Connection | Closes the current connection. |
Switch Connection | Switches the active connection by index or alias. |
Close All Connections | Closes all open connections. |
Get Connection | Returns information about the connection. |
Get Connections | Returns information about all the open connections. |
Create Local Ssh Tunnel | The keyword uses the existing connection to set up local port forwarding (the openssh -L option) from a local port through a tunneled connection to a destination reachable from the SSH server machine. |
2.2 会话相关
Login | Logs into the SSH server with the given username and password. Connection must be opened before using this keyword. |
Login With Public Key | Logs into the SSH server using key-based authentication. |
Enable Ssh Logging | Enables logging of SSH protocol output to given logfile. |
Set Client Configuration | Update the `configuration` of the current connection. |
Set Default Configuration | Update the default `configuration`. Please note that using this keyword does not affect the already opened connections. Use `Set Client Configuration` to configure the active connection. |
2.3 命令相关
Start Command | Starts execution of the command on the remote machine and returns immediately. This keyword returns nothing and does not wait for the command execution to be finished. If waiting for the output is required, use `Execute Command` instead. |
Execute Command | Executes command on the remote machine and returns its outputs. This keyword executes the command and returns after the execution has been finished. Use `Start Command` if the command should be started in the background. |
2.4 交互相关
Read | Consumes and returns everything available on the server output. |
Read Command Output | Returns outputs of the most recent started command. At least one command must have been started using `Start Command` before this keyword can be used. |
Read Until | Consumes and returns the server output until expected is encountered. |
Read Until Prompt | Consumes and returns the server output until the prompt is found. |
Read Until Regexp | Consumes and returns the server output until a match to regexp is found. |
Write | Writes the given text on the remote machine and appends a newline. |
Write Bare | Writes the given text on the remote machine without appending a newline. |
Write Until Expected Output | Writes the given text repeatedly until expected appears in the server output. |
2.5 文件目录
Directory Should Exist | Fails if the given path does not point to an existing directory. |
Directory Should Not Exist | Fails if the given path points to an existing directory. |
File Should Exist | Fails if the given path does NOT point to an existing file. |
File Should Not Exist | Fails if the given path points to an existing file. |
List Directory | Returns and logs items in the remote path, optionally filtered with pattern. |
List Files In Directory | A wrapper for `List Directory` that returns only files. |
List Directories In Directory | A wrapper for `List Directory` that returns only directories. |
Get Directory | Downloads a directory, including its content, from the remote machine to the local machine. |
Get File | Downloads file(s) from the remote machine to the local machine. |
Put Directory | Uploads a directory, including its content, from the local machine to the remote machine. |
Put File | Uploads file(s) from the local machine to the remote machine. |
3. 简单使用
连接、登录、执行命令并关闭连接。
上传脚本并执行