【问题标题】:SSH Tunneling with Robot Framework-SSH LIbrary使用 Robot Framework-SSH 库的 SSH 隧道
【发布时间】:2021-07-30 17:48:55
【问题描述】:

我正在使用 Robot Framework SSH 库进行自动化。

我需要使用 Robot Framework 建立到远程机器的 SSH 隧道,然后在远程机器上执行一组测试。

看起来 RF SSH 库不支持此功能。

有人可以指出任何其他可用的选项吗?

谢谢!

【问题讨论】:

    标签: robotframework


    【解决方案1】:

    您可以使用 Robot Framework 打开 SSH 连接、复制测试文件、运行它们并将结果复制回来。

    你需要两个文件,第一个是 ssh.txt

    *** Settings ***
    Library           SSHLibrary
    Suite Setup       Open Connection And Log In
    Suite Teardown    Copy And Delete Files And Close Connections
    
    *** Variables ***
    ${HOST}                your.server.here
    ${USERNAME}            username
    ${PASSWORD}            password
    
    *** Testcases ***
    Run Tests on remote server
        Put File                  real-test.txt
        Run Test In Server        real-test.txt
        Sleep    1s
    
    *** Keywords ***
    Open Connection And Log In
       Open Connection    ${HOST}
       Login              ${USERNAME}    ${PASSWORD}
    
    Copy And Delete Files And Close Connections
        Copy Results From Server
        Clean Up Server    real-text.txt
        Close All Connections
    
    Run Test In Server
        [Arguments]   ${filename}
        ${rc}=     Execute Command    pybot ${filename}    return_stdout=False  return_rc=True
        Should Be Equal As Numbers    ${rc}    0
    
    Copy Results From Server
        GetFile    log.html
        GetFile    output.xml
        GetFile    report.html
    
    
    Clean Up Server
        [Arguments]   ${filename}
        Write    rm log.html
        Write    rm output.xml
        Write    rm report.html
        Write    rm ${filename}
    

    第二个文件是真正的测试,real-test.txt:

    *** Settings ***
    Library     OperatingSystem
    
    *** Testcases ***
    Test 1
        FileShouldExist    real-test.txt
    

    通过指定输出文件来运行它以避免从服务器复制日志文件时文件名冲突:

    pybot -o conn.xml -r conn.html -l connlog.html ssh.txt

    【讨论】:

      【解决方案2】:

      我最近做了类似的事情,但在 Python 中创建了该功能,然后我将其作为关键字导入到 Robot。

      它的要点是:

      在SSHTunnel.py:

      from sshtunnel import SSHTunnelForwarder
      (other imports)
      
      class SSHTunnel:
          def create_ssh_tunnel(self):
              ssh_server = SSHTunnelForwarder(
                  (host, int(port)),
                  ssh_username=username,
                  ssh_password=pass,
                  remote_bind_address=(remote_bind_host, int(remote_bind_port))
              )
              ssh_server.start()
      
          def stop_ssh_tunnel(self):
              ssh_server.stop()
      

      现在您将SSHTunnel.py 文件添加到机器人库中,并在测试用例的开头调用关键字Create SSH Tunnel(也可以在Test Setup 或Suite Setup em>) 和Stop SSH Tunnel 在最后。

      希望对您有所帮助。

      【讨论】:

        【解决方案3】:
        Library    SSHLibrary
        
        Open Connection   host_server
        
        Login With Public Key test  /test.pem  test12345
        
        Create Local SSH Tunnel  9191  remote_host  remote_port  bind_address=127.0.0.1
        
        #Once the connection is made you can directly access the resources in remote_host, for eg : i am trying to connect mysql db in remote host
        
        Connect To Database Using Custom Params   pymysql database='testDb', user='testuser', password='test123', host='127.0.0.1', port=9191
        

        【讨论】:

          【解决方案4】:

          您可以借助“本地端口转发”功能实现此目的。

          参考:https://help.ubuntu.com/community/SSH/OpenSSH/PortForwarding.

          1. 在实例中的测试服务器(机器人运行的地方)上使用此命令:

            ssh -4 -L <listening_port/forwarding_port>:<destination_ip>:destination_port> username@jump_box.com

          例如) ssh -4 -L 50025:10.122.56.45:23 anishg@jumpbox.teski.com

          1. 在另一个实例中使用以下命令在目标服务器中工作:

            telnet localhost <listening port>

          例如) telnet localhost 50025

          在这里,您可以像在目标服务器上工作一样执行命令,因为会话已打开。

          在 Robot 中,这也可以通过以下方式实现:

          1. 步骤 (1) - 端口转发在 shell 脚本中完成,并且在您的机器人框架中调用此 shell 脚本。
          2. 在 localhost 中使用指定的端口进行 Telnet/其他操作

          sample.sh:

          ssh -4 -L 50025:10.122.56.45:23 anishg@jumpbox.teski.com

          机器人代码:

          *** Test Cases *** Port Forwarding Start Process ./sample.sh shell=True #Port is opened Log To Console SHELL IS Executing Telnet.Open Connection localhost port=50025 #Normal op as in destn server

          注意:我使用 jump_box 因为我无法从我的测试实例访问目标服务器。

          【讨论】:

            猜你喜欢
            • 2021-12-02
            • 2019-08-07
            • 2021-09-01
            • 2014-04-16
            • 2020-07-31
            • 2011-02-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多