• 5.远程服务器操作(SSHLibrary)


    一、SSHLibrary

    robotframework做自动化测试,在流程中可能需要远程连接机器做一些简单操作,比如连接linux服务器,平时测试的时候使用客户端工具去连接就是远程连接。
    远程连接有两种:
    一种是用户名密码登录连接;
    一种是通过密钥连接:比如工具输入用户名、密码去连接服务器,再比如连接git很多都是用密钥。
    在robotframework中要远程连接可以使用SSHLibrary库来做操作。

    1、安装并引入库

    pip install robotframework-SSHLibrary

    2、关键字介绍

    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( , )

    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字符串。

    Get Connections

    用法:没有输入值,获取所有打开的connection的信息

    Switch  Connection

    用法:[ index_or_alias ],跳转到另一个active的connection。

    Close Connection

    用法:没有输入值,关闭当前的connection

    Close All Connections

    用法:没有输入值,关闭所有打开的connection

    Login

    用法:[ username | password | delay=0.5 seconds ]

     

    Login With Public Key
    用法:[ username | keyfile | password= | delay=0.5 seconds ]
    Set Client Configuration
    用法:[ timeout=None | newline=None | prompt=None | term_type=None | width=None | height=None | path_separator=None | encoding=None ],设置当前connection的配置
    Set Default  Configuration
    用法:[ timeout=None | newline=None | prompt=None | term_type=None | width=None | height=None | path_separator=None | encoding=None ],设置默认的配置,但不影响已经open的connection。

    Enable Ssh Logging
    用法:[ logfile ],将SSH协议的日志输出到本地给定的“日志文件”中。Enables logging of SSH protocol output to given `logfile`. 
    note:
        1.文件存在时,文件内容被重写。
        2.没有指定路径时,该文件在RF文件夹中。

     

    与文件/目录相关的
    File Should Exist ,     
    File Should Not Exist,     
    Directory Should Exist ,   
    Directory Should Not Exist
    用法:[ path ] ,无返回值
    List Directory,   
    List Files In Directory ,   
    List Directories In Directory
    用法: [ path | pattern=None | absolute=False ],返回值为list。其中absolute=true ,返回绝对路径。

     

    Put File
    用法:[ source | destination=. | mode=0744 | newline= ],无返回值,将文件从本地上传到远程机器上。
    `newline` can be used to force the line break characters that are written to the remote files. Valid values are `LF` and `CRLF`. 
    Put Directory
    用法:[ source | destination=. | mode=0744 | newline= | recursive=False ],将目录由本地上传到远程机器上。如果recursive=True,将子目录中文件也上传至远程机器上。
    Get File
    用法:[ source | destination=. ],从远程机器中下载文件
    Get Directory
    用法:[ source | destination=. | recursive=False ],从远程机器中下载目录。如果recursive=True,将子目录中文件也下载至本地上。

     

    与读写执行相关的
    Write
    用法:[ text | loglevel=None ],将text写入到远端机器上并回车执行,返回值为text+换行符
    loglevel:TRACE, DEBUG, INFO and WARN
    Write Bare
    用法:[ text ],将text写入到远端机器上,没有执行没有返回值。

    Write Until Expected Output
    用法:[ text | expected | timeout | retry_interval | loglevel=None ],没有返回值,输入的 text 在设定的timeout内每隔retry_interval重复执行,直到终端输出的结果中包含期望的输出expected。

     

    关键字write 有返回值,执行命令后,用read读取终端输出时不会读取到输入的text; 关键字write bare没有返回值,执行命令后,用read读取终端输出时会读取到输入的text;

    Read
    用法: [ loglevel=None | delay=None ] ,读取终端输出
    Read Until
    用法:[ expected | loglevel=None ]
    Read Until Prompt
    用法:[ loglevel=None ]

     

    Read Until Regexp
    用法:[ regexp | loglevel=None ]

    Execute Command
    用法:[ command | return_stdout=True | return_stderr=False | return_rc=False ]
    在远端机器上执行命令,并返回执行结果,需要等待command执行完才返回结果。
    Start Command
    用法:[command]
    没有返回值,不等待command执行完成就返回。
    Read Command Output
    用法:[ return_stdout=True | return_stderr=False | return_rc=False ]
    与Start Command配合使用,执行Read Command Output前至少要先执行一次Start Command关键字,返回最近一次start command的返回值。

     

     

    SSHLibrary 官方文档:

    http://robotframework.org/SSHLibrary/SSHLibrary.html#Importing

     

     

     

     

     

  • 相关阅读:
    iframe
    服务器 开发机 linux docker
    git
    iframe because an ancestor violates the following Content Security Policy directive: "frameancestors 'self'
    @babel/pluginproposaloptionalchaining
    jest
    富文本编辑器
    thymeleaf+layui渲染错误
    springboot静态资源访问
    layui的树型组件的使用
  • 原文地址:https://www.cnblogs.com/zhangan/p/13042839.html
Copyright © 2020-2023  润新知