通过 robotframework 中的 ssh 库执行命令

executing command via ssh library in robotframework

通过 robotframework 中的 ssh 库执行命令

我正在使用 robotframework 连接到远程服务器并在那里启动应用程序。我使用这些关键字:

Create SSH Link
    Open Connection     ${Ip}
    Login               ${Username}     ${Password}

Start Service
    ${cmd} =    "cd " + ${appAddr} + "; ./" + ${appName} + " " + ${appInputArg} + " > /tmp/" + ${appName} "-out.log 2>&1"
    log to console      Going to run command: ${cmd}
    execute command     ${cmd}
    log to console      \nConnected Successfully

Run App
    ${cmd} =    "netstat -putan | grep LISTEN | grep 8898 | awk '{print }' | cut -d '/' -f 1"
    ${stdout} =     execute command     ${cmd}
    Run Keyword If          ${stdout} == ${EMPTY}       log to console      \nCommand ${cmd} retruned Error
    Run Keyword Unless      ${stdout} == ${EMPTY}       Start Service    

这是我的测试用例:

Test Connecting to Remote Server
    Create SSH Link
    Run App

和输出:

==============================================================================
Scenarios                                                                     
==============================================================================
Scenarios.Test :: First Test                                                  
==============================================================================
Test Connecting to Remote Server                                      
Connected Successfully
| FAIL |
No keyword with name '"netstat -putan | grep LISTEN | grep 8898 | awk '{print }' | cut -d '/' -f 1"' found.
------------------------------------------------------------------------------
Scenarios.Test :: First Test                                          | PASS |
0 critical tests, 0 passed, 0 failed
1 test total, 0 passed, 1 failed
==============================================================================
Scenarios                                                             | PASS |
0 critical tests, 0 passed, 0 failed
1 test total, 0 passed, 1 failed
==============================================================================

为什么不在远程服务器上执行此命令并搜索关键字??

问题出在这条语句:

${cmd} =    "netstat -putan | grep LISTEN | grep 8898 | awk '{print }' | cut -d '/' -f 1"

这不是有效的机器人框架声明。 Robot Framework 正在查找关键字,在此语句中找不到任何关键字。

应该改为:

${cmd} =    set variable    netstat -putan | grep LISTEN | grep 8898 | awk '{print }' | cut -d '/' -f 1

注意:您的字符串不需要双引号。默认情况下变量是机器人中的字符串。