SSHLibrary 开源项目教程

SSHLibrary 开源项目教程

SSHLibrary Robot Framework test library for SSH and SFTP SSHLibrary 项目地址: https://gitcode.com/gh_mirrors/ss/SSHLibrary

1. 项目介绍

SSHLibrary 是一个用于 Robot Framework 的测试库,专门用于 SSH 和 SFTP 操作。该项目托管在 GitHub 上,可以从 PyPI 下载。SSHLibrary 是操作系统无关的,支持 Python 3.6 及以上版本。该库的主要用途包括:

  • 在远程机器上执行命令,支持阻塞和非阻塞行为。
  • 在交互式 shell 中进行读写操作。
  • 通过 SFTP 传输文件和目录。
  • 确保远程机器上存在文件和目录。

2. 项目快速启动

安装

推荐使用 pip 进行安装:

pip install --upgrade robotframework-sshlibrary

此命令还会安装最新的 Robot Framework、paramiko 和 scp 版本。

使用示例

以下是一个简单的示例,展示如何在 Robot Framework 测试中使用 SSHLibrary:

*** Settings ***
Documentation     This example demonstrates executing a command on a remote machine
...               and getting its output.
...
...               Notice how connections are handled as part of the suite setup and
...               teardown. This saves some time when executing several test cases.
Library           SSHLibrary
Suite Setup       Open Connection And Log In
Suite Teardown    Close All Connections

*** Variables ***
${HOST}           localhost
${USERNAME}       test
${PASSWORD}       test

*** Test Cases ***
Execute Command And Verify Output
    [Documentation]    Execute Command can be used to run commands on the remote machine.
    ...                The keyword returns the standard output by default.
    ${output}=    Execute Command    echo Hello SSHLibrary
    Should Be Equal    ${output}    Hello SSHLibrary

*** Keywords ***
Open Connection And Log In
    Open Connection    ${HOST}
    Login    ${USERNAME}    ${PASSWORD}

3. 应用案例和最佳实践

应用案例

SSHLibrary 常用于自动化测试环境中,特别是在需要与远程服务器进行交互的场景中。例如,测试团队可以使用 SSHLibrary 来验证远程服务器的配置是否正确,或者自动化部署脚本是否按预期工作。

最佳实践

  1. 连接管理:在测试套件的设置和拆卸阶段管理连接,以减少重复连接和断开的时间。
  2. 错误处理:使用 Run Keyword And Ignore Error 等关键字来处理可能的错误,确保测试的健壮性。
  3. 日志记录:在执行命令后,使用 Get ConnectionGet Last Command Log 等关键字来记录和分析执行日志。

4. 典型生态项目

Robot Framework

Robot Framework 是一个通用的自动化测试框架,支持关键字驱动的测试。SSHLibrary 是 Robot Framework 的一个扩展库,用于处理 SSH 和 SFTP 操作。

Paramiko

Paramiko 是一个纯 Python 实现的 SSHv2 协议库,提供了客户端和服务器功能。SSHLibrary 依赖于 Paramiko 来实现 SSH 连接和操作。

Scp

Scp 是一个用于通过 SSH 进行文件传输的 Python 库。SSHLibrary 使用 Scp 来实现 SFTP 文件传输功能。

通过这些生态项目的结合使用,SSHLibrary 能够提供强大的远程操作和文件管理功能,适用于各种自动化测试和部署场景。

SSHLibrary Robot Framework test library for SSH and SFTP SSHLibrary 项目地址: https://gitcode.com/gh_mirrors/ss/SSHLibrary

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

RobotFramework中 中SSHLibrary学习与总结 学习与总结 ⼀、安装SSHLibrary ⼆.关键字 1.与连接相关的 Open Connection Get Connection Get Connections Switch Connection Close Connection Close All Connections Login Login With Public Key Set Client Configuration Set Default Configuration Enable Ssh Logging 2.与⽂件/⽬录相关的 2.1 File Should Exist , File Should Not Exist, Directory Should Exist , Directory Should Not Exist 2.2 List Directory, List Files In Directory , List Directories In Directory 2.3 Put Directory ,Get Directory,Put File,Get File 3.与读写执⾏相关的 Write Write Bare Write Until Expected Output Read Read Until Read Until Prompt Read Until Regexp Execute Command Start Command Read Command Output ⼀、安装 ⼀、安装SSHLibrary 安装命令:pip install robotframework-sshlibrary ⼆ ⼆.关键字 关键字 1.与连接相关的 与连接相关的 Open Connection ⽤法: [ host " alias=None " port=22 " timeout=None " newline=None " prompt=None " term_type=None " width=None " height=None " path_separator=None " encoding=None ] 默认设置:timeout=3 seconds, newline=LF, prompt=None, loglevel=INFO, term_type=vt100, width=80,height=24, path_separator=/, encoding=UTF-8.其 中,newline=LF/CRLF(\n,\r\n) 更改默认设置: 1.导⼊库时: Library SSHLibrary 10 seconds prompt=$ 2.使⽤ Set Client Configuration/Set Default Configuration 3.调⽤Open Connection时: 结果: ${con1} =index=1 path_separator=/ prompt=# width=80 newline= height=24 encoding=UTF-8 alias=backend host=10.69.140.112 timeout=3 seconds term_type=vt100 port=2222 Get Connection ⽤法:[ index_or_alias=None " index=False " host=False " alias=False " port=False " timeout=False " newline=False " prompt=False " term_type=False " width=False " height=False " encoding=False ] 1.获取connection的信息,如果调⽤时没有加 index_or_alias,返回当前的conection信息。 2.获取connection的特定属性信息,后边加属性名=⾮false/False字符串。 结果: ${con1} =index=1 path_separator=/ prompt=# width=80 newline= height=24 encoding=UTF-8 alias=backend host=10.69.140.112 timeout=3 seconds term_type=vt100 port=2222 ${con2} = (2222, 3.0) Get Connections ⽤法:没有输⼊值,获取所有打开的connection的信息 结果: ${con1} = index=1 path_separator=/ prompt=$ width
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尚虹卿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值